31-01-2013, 03:23 PM
Snake Game
Snake Game[.docx (Size: 82.14 KB / Downloads: 66)
ABSTRACT
At the game start the Snake is two segments long with a head and a tail. Both can point in 4 directions. For North the head is index 3, tail is 7, East head is 4, tail is 8, South head is 5, tail is 9 and for West the head is 6 and tail is 10. While the snake is two segments long the head and tail are always 180 degrees apart but after the snake grows they can be 90 or 270 degrees.
The game starts with the head facing north at location 120 and the tail facing south at 136, roughly central. At a slight cost of some 1,600 bytes of storage, we can gain a discernible speed improvement in the game by holding the snake's locations in the snake[] ring buffer mentioned above. It's a block of memory used for storing a queue that is fixed size and must be big enough to hold all data. In this case it's just for the Snake. The data is pushed on the front of the queue and taken off the back. If the front of the queue hits the end of the block then it wraps round. So long as the block is big enough, the front of the queue will never catchup with the back.
Every location of the Snake (ie the single int coordinate) from the tail to the head (i.e. backwards) is stored in the ring buffer. This gives speed benefits because no matter how long the snake gets, only the head, tail and the first segment after the head (if it exists) need to be changed as it moves.
Storing it backwards is also beneficial because when the snake gets food the snake will grow when it's next moved. This is done by moving the head one location in the ring buffer and changing the old head location to become a segment. The snake is made up of a head, 0-n segments) and then a tail.
When the snake eats food, the atefood variable is set to 1 and checked in the function DoSnakeMove()
Moving the Snake
We use two index variables, headindex and tailindex to point to the head and tail locations in the ring buffer. These start at 1 (headindex) and 0. So location 1 in the ring buffer holds the location (0-255) of the snake on the board. Location 0 holds the tail location. When the snake moves one location forward, both the tailindex and headindex are incremented by one, wrapping round to 0 when they reach 256. So now the location that was the head is where the tail is.
Even with a very long snake that is winding and convoluted in say 200 segments. only the headindex, segment next to the head and tailindex change each time it moves.
Note because of the way SDL works, we have to draw the entire snake every frame. Every element is drawn into the frame buffer then flipped so it's displayed. This has one advantage though in that we could draw the snake smoothly moving a few pixels, not an entire grid position.his program simulates the game “snake”.
A string of characters moves on the screen according to arrow keys pressed by user. If it touches itself or screen boundary, the program terminates. When the snake moves, in arbitrary screen positions some digit (1-9) appears.
The objective of the game is to make the snake eat the said digit, so that it is added to the score. When a digit is eaten, the size of the snake increases by the number of characters equal to the value of the digit.
In this version of the game there are provisions for saving / loading too.