You are on page 1of 8

SnARM

a Snake game implentation for ARM

Alberto Grand, Vittorio Giovara


11/16/2007
Chapter 1

User Manual

1.1 Basic Rules


In this game you control a snake which is hunting for food. You can move
your snake in any direction and you just need to pass over the food in order
to eat it. However every time you eat something your body will grow and
you’ll have to be more careful not to touch the snake body or any of the
borders. Otherwise you are in for some very bad stomachache!

1.2 Controls
While in game, you can move around with 4 buttons.

• S7 - Moves the snake LEFT;

• S9 - Moves the snake RIGHT;

• S4 - Moves the snake UP;

• S8 - Moves the snake DOWN;

Moreover should you need to stop the game, but don’t want to lose your
score you can pause the game pressing S5.
If you wish to restart or start back after a gameover, you have to press
the S1 (RESET) button.

1.3 Levels
We prepared 8 different levels (easy 1 · · · 8 difficult) with different pattern of
walls. The speed of the snake will increase accordingly to the level difficulty
and to how much food was eaten.

1
1.4 Eating food
Food appears randomly in every part of the screen.
If you are enough lucky you might have the chance of eating a special
food; this kind of food will make your snake grow bigger than special food,
but it will slow down its speed. However you have to eat this special food
as soon as possibile since it goes away very quickly.

1.5 Your score


Every time you eat a piece of food you score increases accordingly with the
level difficulty, from 1 point in level one to 8 points in level 8. If you manage
to eat a special piece of food you will get ten times the points you would
receive with normal food!

2
Chapter 2

Reference Manual

In this chapter you will find all the details of the technical implementation
adopted in our game.

2.1 Board and Tools


The code is designed to run on a NXP LPC2292 uCdragon board and it is
programmed with Keil uVision 3.

Figure 2.1: The board adopted

3
2.2 Files Scheme
The game code is divided in four main files, each one created for dealing
with the display, the main algorithm and the input output. All the other
files present in our project are standard ARM libraries with the exception
of LCD_Data.c which has been modified to store pictures and number fonts
used in the game.

2.2.1 Main.c
This file contains the core loop of the algorithm which is executed at every
clock cycle. It provides initialization of the LCD, of the reference Matrix
and of the game itself (by selecting the level difficulty).

2.2.2 display.c
This file contains every function involved in writing or reading on the screen,
from the very low level, turning on and off single pixels, to the high level
operations, printing walls and food on the screen.

2.2.3 gamelib.c
This file contains general functions used in the game like level selection and
matrix intialization. It also contains the all-important function Move_Snake()
used for adjusting the snake length and behaviour to adopt at every move.

2.2.4 io.c
This file contains all the other I/O functions not related with the screen,
especially keyboard input and LED and PWM operations.

2.3 Reference Matrix


An abstract numerical representation of the display a reference matrix map
is used. The dimensions correspond to the one on the display (128x64) and
every pixel is associated to a single cell. Every cell can be commutatively
EMPTY, FOOD, FOODSP, WALL, or the body of the snake represented as the taken
directions, RIGHT, LEFT, UP, DOWN. The position of the snake is always known
thanks to the pointers to its head and tail.

4
2.4 Algorithm
2.4.1 Initialization
The code begins with printing the title picture and with playing the welcome
tune by calling Print_Pic() and PlayTune() while LEDs turn on and off
following different patterns (LedDisp()). Then it clears the screen and asks
to choose the level difficutly in Select_Level(); this function returns an
integer based on how much time the user took to make the selection and the
program uses this number to initialize the rand() seed.

2.4.2 Matrix management


In order for the game to start, the program initializes a reference matrix with
Init_Matrix() that is used to represent the contents of the display. The
Init_Matrix() prints on the matrix and on the screen the body of the snake
and saves the pointers of the cohordinates of the head and of the tail of it.
Then it proceeds to printing the walls (Print_Wall()) based on the level
variable (set up by Select_Level()), the food (Print_Food()) and the score
(Print_Score()). Print_Food() iterates until it finds an empty cell for each
of the five points is displayed with and every five times it is called there is a
50% chance that special food is printed (by Print_Special() which works
exactly like Print_Food()); the special food must be eaten in 100 moves
otherwise it gets deleted. Print_Special()is subsequently called and takes
care of updating the score in the right upper corner of the screen. Finally
Init_Matrix() sets up the LEDs to the same level number and returns the
speed of the game, which varies along with the level.

2.4.3 Snake Handling


After these initialization steps, the program goes in forever loop and executes
the contents of the while(1) at every clock cycle. To reduce the speed of
execution and to have different speeds according to the level, the variable
count is used and it is incremented at every clock cycle; only when count is
equal to the selected speed the program is actually executed (and count is
again reinitialized to 0).

2.4.4 Directions
Firstly there is a check whether to delete the special food or not. Then
the program calls Move_Snake() which moves the snake in the screen in
the direction selected before and performs the various operations based on
the content of the cell the snake moved into. Finally it polls if a button is
pressed; if it is, it updates the direction of the next move, if not the new
direction is the same as in the previous move.

5
2.4.5 Cell Content Handling
Move_Snake() updates the position of the snake by updating the pointers of
the head and tail and checks the content of the cell the snake just moved in.
If it is EMPTY and the tail must be updated, it does so; if it is FOOD, it clears
all the food positions, doesn’t delete the tail (which will not be updated for 3
moves) and increases the speed and the score; if it is FOODSP it replicates the
operation of the FOOD case with the difference of slowing down the speed,
incrementing the score ten times more than normal and not allowing tail
update for 8 moves; if it is WALL or a direction the program executes the
GameOver() function. In every case the LEDs or the PWM (or both) are
activated.

2.4.6 Game Over


The GameOver() function concludes the game by stopping the snake and
by catching the players’ attention with the blinking LEDs. The game over
picture is displayed and a conclusion tune is played. The program is stopped
with a while(1).

6
Contents

1 User Manual 1
1.1 Basic Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.4 Eating food . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Your score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Reference Manual 3
2.1 Board and Tools . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Files Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Main.c . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.2 display.c . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.3 gamelib.c . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.4 io.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Reference Matrix . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . 5
2.4.2 Matrix management . . . . . . . . . . . . . . . . . . . 5
2.4.3 Snake Handling . . . . . . . . . . . . . . . . . . . . . . 5
2.4.4 Directions . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4.5 Cell Content Handling . . . . . . . . . . . . . . . . . . 6
2.4.6 Game Over . . . . . . . . . . . . . . . . . . . . . . . . 6

You might also like