Skip to content

Instantly share code, notes, and snippets.

@AnanthVivekanand
Created March 15, 2019 18:30
Show Gist options
  • Save AnanthVivekanand/f8b444d41c9c5a1b8e723c7ad8789da5 to your computer and use it in GitHub Desktop.
Save AnanthVivekanand/f8b444d41c9c5a1b8e723c7ad8789da5 to your computer and use it in GitHub Desktop.
A basic implementation of the game "snake" on an arduboy.
#include <Arduboy2.h>
Arduboy2 arduboy;
boolean gameStarted = false;
int x, y;
byte snake[11][2];
byte last_move;
byte food_x, food_y;
boolean died = false;
byte counter = 0;
void setup() {
arduboy.begin();
arduboy.setFrameRate(1);
x = 20;
y = 10;
arduboy.clear();
arduboy.setTextSize(3);
arduboy.setCursor(x, y);
arduboy.print("Snake");
arduboy.setTextSize(0.1);
arduboy.setCursor(x - 15, y + 30);
arduboy.print("Snake it or break it");
arduboy.display();
}
//128 by 64
void loop() {
if (!arduboy.nextFrame())
{
return;
}
if (arduboy.pressed(B_BUTTON) && !gameStarted) {
arduboy.clear();
arduboy.display();
gameStarted = true;
/*
for (int i = 0; i <= 10; i++) {
snake[i][0] = -1;
snake[i][1] = -1;
}*/
snake[0][0] = 6;
snake[0][1] = 6;
snake[1][0] = 5;
snake[1][1] = 6;
snake[2][0] = 4;
snake[2][1] = 6;
food_x = random(0, 31);
food_y = random(0, 15);
last_move = 3;
}
if (!gameStarted) {
return;
}
for (int i = 10; i > 0; i--) {
/*
temp_x = snake[i + 1][0];
temp_y = snake[i + 1][1];
snake[i + 1][0] = snake[i][0];
snake[i + 1][1] = snake[i][1];
*/
int x = i - 1;
if ((snake[i][0] < 255) && (snake[i][0] > 0)) {
snake[i][0] = snake[x][0];
snake[i][1] = snake[x][1];
}
}
if (arduboy.pressed(UP_BUTTON) && !(last_move == 1)) {
snake[0][1] -= 1;
last_move = 0;
} else if (arduboy.pressed(DOWN_BUTTON) && !(last_move == 0)) {
snake[0][1] += 1;
last_move = 1;
} else if (arduboy.pressed(LEFT_BUTTON) && !(last_move == 3)) {
snake[0][0] -= 1;
last_move = 2;
} else if (arduboy.pressed(RIGHT_BUTTON) && !(last_move == 2)) {
snake[0][0] += 1;
last_move = 3;
} else if (last_move == 0) {
snake[0][1] -= 1;
last_move = 0;
} else if (last_move == 1) {
snake[0][1] += 1;
last_move = 1;
} else if (last_move == 2) {
snake[0][0] -= 1;
last_move = 2;
} else if (last_move == 3) {
snake[0][0] += 1;
last_move = 3;
}
arduboy.clear();
if ((snake[0][0] == food_x) && (snake[0][1] == food_y)) {
counter++;
food_x = random(0, 32);
food_y = random(0, 16);
//add on to the snake
int i = 10;
while (snake[i][0] <= 0) {
i--;
}
if ((snake[i][0] == snake[(i - 1)][0]) && (snake[i][1] == (snake[(i - 1)][1] + 1))) {
//arduboy.print("a");
snake[(i + 1)][0] = snake[i][0];
snake[(i + 1)][1] = snake[i][1] - 1;
} else if ((snake[i][0] == snake[(i - 1)][0]) && (snake[i][1] == (snake[(i - 1)][1] - 1))) {
//arduboy.print("b");
snake[(i + 1)][0] = snake[i][0];
snake[(i + 1)][1] = snake[i][1] + 1;
} else if ((snake[i][0] == (snake[(i - 1)][0] + 1)) && (snake[i][1] == snake[(i - 1)][1])) {
//arduboy.print("r");
snake[(i + 1)][0] = snake[i][0] + 1;
snake[(i + 1)][1] = snake[i][1];
}
else if ((snake[i][0] == snake[(i - 1)][0] - 1) && (snake[i][1] == snake[(i - 1)][1])) {
//arduboy.print("l");
snake[(i + 1)][0] = snake[i][0] - 1;
snake[(i + 1)][1] = snake[i][1];
}
}
arduboy.print(counter);
drawSnake();
for (int i = 1; i < 10; i++) {
if ((snake[0][0] == snake[i][0]) && (snake[0][1] == snake[i][1])) {
die();
}
}
if (!(arduboy.collide(Rect(snake[0][0] * 4, snake[0][1] * 4, 4, 4), Rect(0, 0, 128, 64)))) {
die();
}
arduboy.display();
/*
if (arduboy.pressed(UP_BUTTON)) {
arduboy.invert(true);
arduboy.audio.off();
} else if (arduboy.pressed(DOWN_BUTTON)) {
arduboy.invert(false);
arduboy.audio.on();
}
die();
*/
}
void die() {
x = 15;
y = 15;
gameStarted = false;
died = true;
arduboy.clear();
arduboy.invert(true);
arduboy.setTextSize(2);
arduboy.setCursor(x, y);
arduboy.print("You Died");
arduboy.setTextSize(1);
arduboy.setCursor(x + 25, y + 30);
arduboy.print("Snek sad");
arduboy.display();
for (int i = 0; i < 11; i++) {
snake[i][0] = 0;
snake[i][1] = 0;
}
counter = 0;
snake[0][0] = 6;
snake[0][1] = 6;
snake[1][0] = 5;
snake[1][1] = 6;
snake[2][0] = 4;
snake[2][1] = 6;
last_move = 3;
arduboy.invert(false);
if (arduboy.pressed(B_BUTTON)) {
died = false;
//setup code
x = 20;
y = 10;
arduboy.invert(false);
arduboy.clear();
arduboy.setTextSize(3);
arduboy.setCursor(x, y);
arduboy.print("Snake");
arduboy.setTextSize(0.1);
arduboy.setCursor(x - 15, y + 30);
arduboy.print("Snake it or break it");
arduboy.display();
}
}
void drawSnake() {
arduboy.drawRect(food_x * 4, food_y * 4, 4, 4, WHITE);
//arduboy.drawRect(x*4, y*4, 4, 4, WHITE);
for (int i = 0; i < 10; i++) {
if ((snake[i][0] == 255) || snake[i][0] == 0) {
return;
}
arduboy.drawRect(snake[i][0] * 4, snake[i][1] * 4, 4, 4, WHITE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment