Snake 1
Snake 1
//
---------------------------------------------------------------
//
// ------------------------- user config
------------------------- //
//
---------------------------------------------------------------
//
void setup() {
Serial.begin(115200); // set the same baud rate on your
Serial Monitor
initialize(); // initialize pins & LED matrix
calibrateJoystick(); // calibrate the joystick home (do not
touch it)
showSnakeMessage(); // scrolls the 'snake' message around the
matrix
}
void loop() {
generateFood(); // if there is no food, generate one
scanJoystick(); // watches joystick movements & blinks with
food
calculateSnake(); // calculates snake parameters
handleGameStates();
struct Point {
int row = 0, col = 0;
Point(int row = 0, int col = 0): row(row), col(col) {}
};
struct Coordinate {
int x = 0, y = 0;
Coordinate(int x = 0, int y = 0): x(x), y(y) {}
};
// construct with default values in case the user turns off the
calibration
Coordinate joystickHome(500, 500);
// snake parameters
int snakeLength = initialSnakeLength; // choosed by the user in
the config section
int snakeSpeed = 1; // will be set according to potentiometer
value, cannot be 0
int snakeDirection = 0; // if it is 0, the snake does not move
// direction constants
const short up = 1;
const short right = 2;
const short down = 3; // 'down - 2' must be 'up'
const short left = 4; // 'left - 2' must be 'right'
//
---------------------------------------------------------------
//
// -------------------------- functions
-------------------------- //
//
---------------------------------------------------------------
//
case down:
snake.row++;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
case left:
snake.col--;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
void handleGameStates() {
if (gameOver || win) {
unrollSnake();
showScoreMessage(snakeLength - initialSnakeLength);
if (gameOver) showGameOverMessage();
else if (win) showWinMessage();
void unrollSnake() {
// switch off the food LED
matrix.setLed(0, food.row, food.col, 0);
delay(800);
delay(20);
// invert it back
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
matrix.setLed(0, row, col, gameboard[row][col] == 0 ?
0 : 1);
}
}
delay(50);
delay(600);
void initialize() {
pinMode(Pin::joystickVCC, OUTPUT);
digitalWrite(Pin::joystickVCC, HIGH);
pinMode(Pin::joystickGND, OUTPUT);
digitalWrite(Pin::joystickGND, LOW);
matrix.shutdown(0, false);
matrix.setIntensity(0, intensity);
matrix.clearDisplay(0);
randomSeed(analogRead(A5));
snake.row = random(8);
snake.col = random(8);
}
void dumpGameBoard() {
String buff = "\n\n\n";
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (gameboard[row][col] < 10) buff += " ";
if (gameboard[row][col] != 0) buff += gameboard[row][col];
else if (col == food.col && row == food.row) buff += "@";
else buff += "-";
buff += " ";
}
buff += "\n";
}
Serial.println(buff);
}
//
---------------------------------------------------------------
//
// -------------------------- messages
--------------------------- //
//
---------------------------------------------------------------
//
matrix.clearDisplay(0);
matrix.clearDisplay(0);
[&] {
for (int d = 0; d < sizeof(scoreMessage[0]) + 2 *
sizeof(digits[0][0]); d++) {
for (int col = 0; col < 8; col++) {
delay(messageSpeed);
for (int row = 0; row < 8; row++) {
if (d <= sizeof(scoreMessage[0]) - 8) {
matrix.setLed(0, row, col,
pgm_read_byte(&(scoreMessage[row][col + d])));
}
matrix.clearDisplay(0);