In this project I will implement the classical Snake game using a matrix. The game will be controlled by a joystick and the speed can be changed using a potentiometer.
For each loop, 4 actions are performed: generate food, scan for joystick input, update the snake, and calculate if the game is over. Those actions are performed by 4 functions:
generateFood()
scanJoystick()
calculateSnake()
handleGameStates()
Each action was placed in a separate function in order to preserve code modularity, and allow for ease of expansion.
Also, in order to easily debug the code, in the loop, the function
dumpGameBoard()
can be called. This function prints the game board to the serial monitor.
To make the game easier to write, I created two structs: Point & Coordinate. The Point struct is used to store the coordinates of the snake's head and the coordinate of the food. At boot, the snake is initialized with a length of 3. The snake starts at a random position on the board, and the food is not placed anywhere.
The snake's speed can also be changed, by updating the snakeSpeed variable. The higher the speed, the faster the snake moves.
If the snake crosses an edge, it will appear on the other side of the screen. This is handled by the
fixEdge()
function.
3 default messages are provided: snake message, game over message, score message. Also, 10 (0-9) digits may be written to the LED matrix, using the digits map.
This was my first arduino project and I am proud that I completed it in a few weeks.
25/05/2022 - wiki page completion
20/05/2022 - finished project
18/05/2022 - finished writing the software part
23/04/2022 - wiki page
21/04/2022 - project selection