© ArduTV 2025

In this simple simple example we use one Arduino UNO connected to the ArduTV and a Joystick to implement a simple snake game.This game will be presented in the MakerFaire of Rome from 25th to 27th October 2024 - Come to visit us !
NOTE: The example is an ArduTV modification of the original code published here: https://github.com/Elecrow-RD/short_eletronics/tree/main/snake
WATCH THE VIDEO AT THE END OF THE PAGE.
#include <ArduTV.h> //ArduTV Library to be included
ArduTV HDMI1(10); //Set the CS pin for ArduTV
#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3
//STICKBOX Pin
#define pinX A0
#define pinY A1
int valueX = 0;
int valueY = 0;
int outBound=0;
unsigned char keyValue = 0;
//Snake Value
//u8 head_snake[8] PROGMEM = {
u8 head_snake[8] = {
0x18,
0x3C,
0X5C,
0XFB,
0XDF,
0X6A,
0X3C,
0X18
};
//u8 apple[8] PROGMEM = {
u8 apple[8] = {
0x20,
0x10,
0X08,
0X3C,
0XFF,
0X7E,
0X7E,
0X18
};
int snake_head_x = 200;
int snake_head_y = 200;
int x[100];
int y[100];
int snake_len = 2;
int snake_dir = RIGHT;
int food_x;
int food_y;
int old_x=199;
int old_y=199;
bool food_eaten = true;
bool game_over = false;
bool Player = true;
int score = 0;
int level = 1;
int snake_speed = 100;
int i;
void keyScan(void)
{
// static unsigned char keyUp = 1;
valueX = analogRead(pinX);
valueY = analogRead(pinY);
//if (keyUp && ((valueX <= 150) || (valueX >= 850) || (valueY <= 150) || (valueY >= 850)))
if ((valueX <= 150) || (valueX >= 850) || (valueY <= 150) || (valueY >= 850))
{
delay(5);
if (valueX <= 150)
{
if (snake_dir != UP)
{
snake_dir = DOWN;
}
}
else if (valueX >= 850)
{
if (snake_dir != DOWN)
{
snake_dir = UP;
}
}
else if (valueY <= 150)
{
if (snake_dir != RIGHT)
{
snake_dir = LEFT;
}
}
else if (valueY >= 850)
{
if (snake_dir != LEFT)
{
snake_dir = RIGHT;
}
}
}
return ;
}
//if (snake_head_x < 14 || snake_head_x > 503 || snake_head_y < 63 || snake_head_y > 455)
void AutoScan(void)
{
uint8_t Auto;
Auto = (uint8_t)(random(0, 15));
if (snake_head_x < 24 )
{
if (snake_dir==LEFT)
Auto=0;
else
Auto=3;
} else
if (snake_head_x > 493 )
{
if (snake_dir==RIGHT)
Auto=1;
else
Auto=2;
} else
if (snake_head_y < 73 )
{
if (snake_dir==UP)
Auto=2;
else
Auto=3;
}else
if (snake_head_y > 445)
{
if (snake_dir==DOWN)
Auto=3;
else
Auto=1;
}
delay(5);
switch (Auto) {
case 0:
if (snake_dir != UP)
{
snake_dir = DOWN;
}
break;
case 1:
if (snake_dir != DOWN)
{
snake_dir = UP;
}
break;
case 2:
if (snake_dir != RIGHT)
{
snake_dir = LEFT;
}
break;
case 3:
if (snake_dir != LEFT)
{
snake_dir = RIGHT;
}
break;
default:
break;
}
}
void draw_snake(int x, int y)
{
//oled.drawBitmap(x, y, block, 4, 4, 1);
//HDMI1.bitmap(block, x, y, 4, 4);
HDMI1.printchar('#', x, y, 1);
}
void draw_snake_food(int x, int y)
{
//oled.drawBitmap(x, y, block, 4, 4, 1);
//HDMI1.bitmap(block, x, y, 4, 4);
HDMI1.PenColor(0,31,0); //RED
HDMI1.printchar('$', x, y, 1);
HDMI1.PenColor(25,10,31); //RED
}
void show_score(int x, int y, int data)
{
//oled.setCursor(x, y);
//oled.println(data);
HDMI1.printchar(data, x, y, 1);
}
void screen(void)
{
//HDMI1.Clear();
HDMI1.PenColor(27, 27, 31);
HDMI1.Rect(10, 60, 500, 400, 0);
HDMI1.Rect(11, 61, 498, 398, 0);
HDMI1.printString("LEVEL", 520, 62, 1);
HDMI1.printString("SCORE", 520, 90, 1);
HDMI1.BGColor(31, 31, 31);
HDMI1.PenColor(31, 0, 0);
HDMI1.printString("ArduTV", 40, 15, 2);
//HDMI1.PenColor(0, 31, 31);
//HDMI1.printString("ArduTV", 470, 15, 2);
HDMI1.BGColor(0, 0, 0);
HDMI1.PenColor(7, 7, 25);
HDMI1.Rect(6, 10, 185, 22, 0);
HDMI1.Rect(5, 9, 187, 24, 0);
HDMI1.PenColor(31, 31, 16);
HDMI1.printString("SNAKE CHALLENGE", 200, 15, 2);
HDMI1.PenColor(31, 31, 31);
HDMI1.printString("Partecipa e vinci la penna di ArduTV !", 15, 43, 1);
//HDMI1.PenColor(31, 16, 0);
show_score(568, 62, level+48);
show_score(568, 90, score+48);
/*
for (i = 0; i < snake_len; i++)
{
draw_snake(x[i], y[i]);
}
draw_snake(food_x, food_y);
//oled.display();
*/
}
void draw_snake(void)
{
/*
for (i = 0; i < snake_len; i++)
{
draw_snake(x[i], y[i]);
}*/
draw_snake(x[0], y[0]);
//HDMI1.bitmap(block_clr, old_x, old_y, 4, 4);
HDMI1.printchar(' ', old_x, old_y, 1);
old_x=x[snake_len-1];
old_y=y[snake_len-1];
draw_snake_food(food_x, food_y);
}
void draw_food(void)
{
int food_out = 0;
if (food_eaten)
{
while (food_out == 0)
{
food_out = 1;
food_x = (uint8_t)(random(17, 503) / 8) * 8;
food_y = (uint8_t)(random(64, 454) / 8) * 8;
for (int i = snake_len - 1; i > 0; i--)
{
if (food_x == x[i] && food_y == y[i])
{
food_out = 0;
}
}
}
}
food_eaten = false;
}
void snake_move(void)
{
switch (snake_dir) {
case RIGHT:
snake_head_x += 8;
break;
case UP:
snake_head_y -= 8;
break;
case LEFT:
snake_head_x -= 8;
break;
case DOWN:
snake_head_y += 8;
break;
}
if ((snake_head_x == food_x) && (snake_head_y == food_y))
{
food_eaten = true;
//snake_len++;
snake_len=snake_len+3;
score++;
if (score>5){
level=level+1;
score=0;
snake_speed=snake_speed-10;
}
//level = score / 5 + 1;
//snake_speed -= level;
}
for (i = snake_len - 1; i > 0; i--)
{
x[i] = x[i - 1];
y[i] = y[i - 1];
}
x[0] = snake_head_x;
y[0] = snake_head_y;
check_snake_die();
}
void draw_game_over()
{
HDMI1.printString("GAME OVER", 200, 200, 2);
}
void check_snake_die(void)
{
if (snake_head_x < 14 || snake_head_x > 503 || snake_head_y < 63 || snake_head_y > 455)
{
game_over = true;
}
if (snake_len > 4)
{
for (int i = 1; i < snake_len; i++)
{
if (snake_head_x == x[i] && snake_head_y == y[i])
{
game_over = true;
}
}
}
}
void setup()
{
//oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//oled.setTextColor(WHITE);
randomSeed(analogRead(3));
HDMI1.begin();//Initialize ArduTV
HDMI1.BGColor(0, 0, 0);//ArduTV Background color setting (blsck)
HDMI1.Clear(); //ArduTV Command Clear the screen
screen();
HDMI1.PenColor(25,10,31); //RED
HDMI1.changeFont(35, head_snake); //change "#" font with the head of the snake.
HDMI1.changeFont(36, apple); //change "$" font with the food of the snake.
}
void loop()
{
if (game_over)
{
draw_game_over();
delay (10000);
Player=false;
game_over=false;
snake_len = 2;
setup();
} else {
if (Player)
keyScan();
else
AutoScan();
snake_move();
draw_food();
draw_snake();
show_score(568, 62, level+48);
show_score(568, 90, score+48);
//screen();
}
delay(snake_speed);
}

Joystick is connected for the Power, GND and A1+A2 as inputs.
In the video the running example (sorry for the "home made" :), but we are not a company!).
© ArduTV 2025