The Snake game

Matthew Holcomb

Overview

For this project, I wanted to create a snake game that utilized an LED dot matrix to display the game on an 8×8 grid. The score would be displayed on an LCD screen. The device will be powered by 4xAA batteries. For anyone unfamiliar with the game of snake, the snake moves in 4 possible directions and the objective of the game is to eat the fruit to make the snake longer and add to the score. If the snake collides with the tail or goes out of bounds then the game is ended.

General view

The idea for this project, algorithm, and code prototype are borrowed from here. We implemented the game on a different microcontroller and LCD and used a different approach for uploading the screen bitmap from memory to the dot matrix.

Hardware

For this project, I used a Freescale MKL05Z32VFK4 Kinetis microcontroller from the KL05 Kinetis family series because of it is efficient, and it has good low power mode capability. For the display, I used a 2x16 character Newhaven LCD display to display the score. For the dot matrix display I used a common cathode bicolor 8×8 display sold by Newegg, which only uses the green color. The device is designed to run off 4xAA batteries. There are 5 buttons which are used for the four possible directions and one to reset the game.

Schematic Assembly Back view

The batteries and the power switch are mounted on a plastic back panel. Their voltage is adjusted to a 3.3V level with a DC/DC converter IC4. The current consumed from batteries during the play depends on the amount of dots displayed and in average is about 40 mA. The dots current is controlled by IC3 driver and set with resistor R12. The driver displays one column on the display in a time. The pattern of column dots to display is loaded into it via SPI interface. The same interface is used to communicate with the LCD (with a separate CS line). The columns are switched with a 2ms period by the decoder IC2, whose outputs control high-level switches Q1 - Q4. The microcontroller is clocked at 4 MHz frequency and runs in the Low Power Run mode with the bus frequency of 800 KHz.

Software

The LCD is used to display the current score during the game i(in its bottom row) and the "GAME OVER" message (in its top one). The display uses the following coordinate system. Its columns are displayed from right to left and the top dot in a columns corresponds to the MSB of the corresponding byte in the screen bitmap.

coords

The game starts off with the snake at position x=y=8 and the default move direction to start off is to 0 which is not moving. The fruit is randomly placed anywhere where the snake is not located and within the bounds of the playing field.

The user input is taken every time the matrix is updated and if no new user input is detected the snake will continue to move in the previously set direction. If multiple buttons are pressed it when utilize the most recent button pressed in the keys scan order. Buttons by number: 1 right, 2 up, 3 down, 4 left, 5 center. Left button moves the snake left but only if the snake is not moving right. Up button moves the snake up but only if the snake is not moving down. Right button moves the snake right but only if the snake is not moving left. Down button moves the snake down but only if the snake is not moving up. The center button resets the game when the game over function has been called. Before restarting the game, it also allows the user to pick from 3 different speed difficulties.

As the snake moves the location of the head is moved to the most recent segment of the tail and this location is moved back one index along with every other tail element. The tail element at index 0 is then dropped by being overridden. The score is based off the index of the head which is the length of the tail segment, this value is then multiplied by 10 when difficulty is set to normal, or 5 when the difficulty is set to easy, or by 15 when set to insane.

When the player or snake head location matches the location of the tail or any location out of bounds the game is ended. When the player or snake head location matches the fruit location the score is incremented by 10 and a new egg is generated ensuring that its location does not match the location of the head or and tail segment.

The screen is updated every 2 milliseconds. Every .4 seconds (unless difficulty is changed) the led for the fruit is toggled either on or off to help distinguish the fruit from the snake. Every .8 seconds (unless difficulty is changed) the necessary tasks to run are: check move, do move, check wall collision, check fruit collision, print score.

When game over method is called the LED dot matrix is set to an "X" pattern and resulting score is sent to the LCD to print out. When the center button is pressed, it allows the user to select a difficulty number of 1 to 3. 1 is easy, 2 is normal, 3 is insane. To select the difficulty either up or down buttons are pressed which displays 1, 2, or 3 on the matrix and the center button confirms the choice.

Final Thoughts

I was thinking I would have enough time to develop some more simple games that would work on a dot matrix display such as the racing game done in another student project but due to the shipping delay I couldn't debug the code very well until later than desired. I would have liked to write some additional games but I am satisfied with my project functionality and design.

Downloads


Last modified:Mon, Jan 23, 2023.

00135