Snake
The simple snake game is a universally recognized game and one of the earliest games to ever come out on a mobile device. Nokia famously put snake on most of their mobile phones and so there's a certain irony to me coding snake to be played on a Nokia 5110 LCD. It is a reasonably simple game where the user controls a 'snake' in 4 directions and tries to collect pixels that represent food whilst trying not to crash into the walls or itself. Once the snake consumes the food it increases in length which increases the difficulty of the game. Knowing this we can make a list of the different building blocks we require to make up this game.
Firstly the snake itself can easily be though of as a series of coordinates in an array, the food can also just be though of as a coordinate that is randomly chosen to be at any point that isn't on the snake or border. So using this approach we need to consider the following points and building blocks of our code:
- 1 pixel per snake or food coordinate is clearly not enough so how many pixels per cell will we use?
- How can we apply a difficulty setting?
- Checking for game over conditions, i.e. Crashing into wall or self
- Checking for when the snakes head coordinate is the same as food coordinate
- Code block that creates a random food coordinate each time one is needed
- Code block to handle the drawing on LCD
Here are some of the solutions to these aspects of the coding:
- From very quick prototyping on how many pixels should be used per 'cell' of the snake the optimum number of pixels seems to be 2x2. This is because any smaller becomes hard to see but any larger restricts the game map since we only have 84 pixels wide by 48 pixels high to work with.
- Difficulty in snake can be altered by either putting obstacles in the path or altering the in game speed. Using obstacles would make for a more interesting game but sadly since the LCD allows only one colour (black) it would become difficult for the user to differentiate between obstacles and the food they are supposed to be collecting. For this reason I will be altering the speed of the snake to increase or decrease the difficulty.
- To check for game over conditions we simply have to check whether the first snake coordinate (The head) is greater or less than the number of allowable pixels and that would check for the boundaries. To check whether the snake has crashed into itself we have to iterate through the snake array and check for if the head coordinate is the same as any of the other coordinates and this would constitute a game over.
- Checking for food collection simply requires checking whether the head coordinate is the same as the food coordinate every game tick.
- Creating a random food coordinate will just require a random x value and random y value but then the essential step of checking that this new value does not have the same coordinates as a snake coordinate. If it does then a new food coordinate must be created.
- Drawing on the LCD display only needs the screen to be cleared before plotting the border, food and snake again.
A ticker interrupt will be used to create game ticks and in every tick each element of the snake array will be moved backwards one element as far as the length of the snake allows (which means the last element will disappear) and a new coordinate will be added to the front of the array. This new coordinate will be calculated depending on the current snake direction which in turn will depend on the direction of the joystick. All of the checks will also be done every game tick to detect game over conditions and food collection.