Snake Game on MBED LPC1768

There is a link to the Github page for this project at the bottom of this page.

This project was a part of my ECE 2035 class. We were tasked to create a Snake game using the MBED LPC1768 and various breakout accessories on a circuit. This project involved using C to code a hash table alongside some peripherals in the MBED online compiler in order to play Snake on an LCD screen connected to the LPC1768.

The breadboard with the MBED LPC1768, LCD, and other breakout accessories.

The hash table was coded in tandem with our lecture on heap memory; creating the table reinforced my understanding of how the heap works. Also, in order to test our hash tables, we used tools such as Make in the Linux Bash Shell and GTest. These tools tested the code with the ht_tests.cpp file that I also created, and also would test the hashtable.c code for any memory leaks. This is an important test as any memory leaks would make way for the game to glitch and crash seemingly without explanation. I wrote a test code in C++ to ensure my hash table worked as expected as the table would be used in the code to create the map for the game, which is bigger than the area shown on the LCD.

The first step in this project was creating the hash table and then creating a basic implementation of the Snake game. The basic performance included moving the snake, eating and removing the chests, having a game over screen, boost-up and boost-down items, an invincibility button, one sprite, and a status bar.

The hardest part about these features was actually moving the snake with the nav switch. This sounds like it should have been easy but creating a loop that loops through the length of the viper and saves the previous body’s location to be used on the next body was harder than I though it was going to be. I had many failed attempts at this, one such being that as soon as the viper started moving it would decapitate itself and leave its body behind. Another instance was when I forgot to erase the previous tail after moving, and the viper would just continue to get bigger and bigger as it moved.

A video depicting the basic implementation is shown below.

After having the basic implementation finished, I moved on to add advanced features to the Snake Game. These features included additional boost-up and boost-down objects, more sprites, a pause button, and a portal to another map.

The Additional Boost-Up and Boost-Down

The additional boost-down item increased the length of the snake by 3 immediately after it was eaten, and similarly, the boost-up object decreased the length by 3. The boost-up object may seem trivial as it is a mirror of the boost-down object; however, it was significantly more difficult to code than the boost-down. This is due to how I implemented it in the code, the object reduces the length by 3 but always keeps the length at a minimum of 3; this means that if the snake is length 4 and eats this boost-up, it will become length 3, not 1. Furthermore, the bits of the snake that get cut off must also get erased; without erasing them, the parts of the snake that get cut off remain on the map and become an inconvenience to the player as they can run into them and die. I had to save the last three bits of the viper in temporary variables and loop through them and use a map_erase function to clear them.

Regulating Memory Usage

Another difficulty I faced whilst making the advanced features was regulating memory usage. The MBED LPC1768 does not have much memory, 512 kb of FLASH and 32 kb of RAM. The sprites take up quite a bit of memory all together, which became more apparent as I was creating my second map. At first I wanted to map to be 105×3, and trying to create this map lead to all sorts of frustration and confusion. The code to add the walls around the border was not working as it should have been and adding the chests or the snake would duplicate itself twice in different parts of the map even though the code would not call for multiple snakes. It was weird and the code started working as it should once I changed the map size to be 7×52.

However, this would not be the end of memory issues. With these new map dimensions, I tried printing chests in a checkerboard pattern throughout the entirety of the map, but the memory had different plans. With the code adding as many chests as it could in the map, the game would crash when I entered the portal. Fortunately, I thought of adding print statement to make sure the map was printing correctly beforehand, and these statements showed me that the MBED board was running out of memory before it could finish the border after adding the chests. I limited the amount of chests that could be added and it finally started working correctly.

A video depicting the advanced features is shows below.

Click here to be taken to the github page for this project