Projekt SNAKE, Snake Game, Snake Game


Gliwice, 18.01.2012

Semester: I

Group: III

Section: 1

Computer Programming Laboratory

Author: Przemysław Kozieł

E-mail: miltiades11@wp.pl

Tutor: dr inż. Michał Kawulok

Snake Game

Report contains:

1) General description of the program with pictures.

2) Stages of program development.

3) Problems during creating the program.

4) Testing.

4) Applications.

1). General description of the program with pictures.

First of all my task is "Write a Snake". When I start work, I think that it is hard question to do, because I start learn C language.

The main action of the game is to move the head, tail elements footsteps move as giving the position of the head. Transfer of the position lies in the fact that the last element takes before last position of the element, and so in turn. Element behind the head assumes its position then the head is moved in the direction that indicates a player. First of all I must create structures, which holds position of head, tail and heart (heart this is point when the snake that eats its length increases, this is alternative for standard apple). I create additionally structure, which holds information about snake length, direction, game status and player score. When snake's head position is the same as the position of the generated hearts, length of snake is increased by one element of the tail. Player select direction of snake's move when press keys (W- up, S- down, A- left, D- right), when the snake has a conflict with the edge of the map or their own tail, the player loses. When player lose, there is a screen on which a player gives their name, which together with the score is saved to a text file "Highscores.txt".

Head is shown as a point, the snake's tail consists of the elements, so I created arrays of structures, each structure contains the location of one element of the tail. Heart is also presented as a point on the board. Another method of presenting the tail is to create a dynamic list which is also holds the location of the elements the tail.

In addition to the main algorithm of the game, I had to create additional functions diversifying and giving the game play of colors. The basis of any game is the menu in the menu that I created to decide what we want to do, first we can start to play, next see the control, view or delete the results and exit the game. When turn on the game see the string "Snake" and loading bar and covering the screen with the addition of timing. Selecting in the menu "Controls" on the screen to show us the controls of a snake in the game. Selecting the "Highscores" get access to a submenu where you can see the results of the players or delete them. Selecting the "Exit" close our game. When we choose "Start Game" we can play in Snake game. Additional function the duration of the game is able to stop it, pressing any key pauses the game appeals.

The source code contains comments that describe the function also describes the use of structures, the main game algorithm describes the steps that are carried out there. Anyone who wants to change or modify the game code, it should easily can found necessary thing. For the description added screenshots from the presentation of the function of the game.

0x08 graphic
This is screen, which we can see when turn on the game. We can see the use of functions to colorize strings. The loading bar is created only to the appearance of the game.

0x08 graphic
This image shows the function that covers the screen with colorful characters.

This image presents design of function Men0x08 graphic
u. In this function we can choose what we want do.

0x08 graphic
This image shows the function which showing the player control.

0x08 graphic
This image shows function in which we can see the results of the players or delete them.

I think that this image is most important because it show us the action of the main game loop, which contains the entire game algorithm using other functions.0x08 graphic
We can see how the snake is drawn and the point(heart) is presented. In the upper right corner we see the score.

0x08 graphic
The image shows what we can see when player loses.

0x08 graphic
When player loses and see string Game Over, is asked to provide their names to save it to the list of results. After this, we return to Menu.

0x08 graphic
The image shows the final screen of the game which we can see. We can see the slow string printing and flashing nickname author of the project.

2) Stages of program development.

1. Create main algorithm to moving snake's head:

- at first I want create algorithm only to moving snake's head,

- first of all I created structure that will hold the snake's head location,

- next I set location of head in starting position,

- next I must create controls to move head,

- for each key, I had to create a separate condition, when to hit UP, decreasing our parameter Y and the head moves to up, when you press down parameter Y is growing,

- for horizontal movement when hit right parameter X increases, when we hit left this parameter decreasing,

- I checked the validation control action in the preview how the variables are changing,

- I used kbhit() function to our head kept choice which pointed direction a player, I made this function in a loop that executes all the time,

- I created a function that draws the head, but this isn't easy, when program drawing head, trace of the previous position is drawn,

- additionally I add to structure temporary position of head, and in position of our head is printed character and in temporary position is printed space,

- when I finish creating this function I start create function which moves tail.

2. Create algorithm to move snake's tail.

- First of all I delete temporary position of head from structure, and create new structure which hold position of snake's tail,
- this structure must be array, because snake's composed of elements,

- each element of array hold position only one element of tail,

- first element of tail takes temporary position of head and is printed as character, next element of tail is previous position of first element of tail and is printed as space,

3. Create game board.

- I create two dimensional array,

- first and last elements are located character which set game boards,

4. Increasing length of snake, increase as a result.

- I create new structure which holds information about player score, length of snake, and moved into this structure information about the direction of the snake,

- I create function which generate position of point and drawing it into game board, when snake head is in the same position, snake's length increasing and player score increasing.

5. Checking collision.

- this is easy to do, I add to my program condition, when position of snake's head is the same as boundaries of game board, player loses,

- when head collide with element of tail, player loses.

6. Create menu, and additionally colors screens.

- I create menu, where player choose what he want to do.

7. Create saving scores into text file, and showing this scores.

- I create function, which create new text file, and when player loses, his scores is saved into file, and player is asked to provide their nick,

- when player want see scores, he had to choose this option in menu and in his screen are printed all scores,

- additionally player can remove text file contains scores.

8. Last revision.
- I added a condition for moving snake horizontally and vertically to have the same speed of motion,

- I added condition for creating a point to position of point isn't the same that position of element of snake,

- Testing the program.

3) Problems during creating the program.

- my first problem is simple error, when I create function which drawing game board, I want set character out of range the array,

- I had a problem when I created controls when the controls are arrow set on a snake speed decreased, I had change this to AWSD,

- sometimes in boundaries space is written, I remove this problem, when I set position of cursor outside the game board,

- very big problem was the wrong assignment of the positions of each element of the tail snake, solution turned out very simple, set position in wrong way,

- menu when the menu clicked on unassigned key which transported us to the random option, it was enough to add the appropriate conditions,

- when we wanted to play next time, then in the menu created a never endless loop, this was caused by the a bad function call,

- sometimes point is generated outside game board, because is bad set range of generation,

- when I testing function which saving scores, I forgot close a file.

4) Testing.

The program tested only observing how variables are changing. Then I added drawing and there was a couple of problems with whom I managed to cope with. When I tried to test the program of observing its operation by drawing on the screen the results of operations, did not notice some bugs which contain inside some function. Therefore, so I think the test by observing the changes of variables is a very good way to check the correctness action of the program.

5) Applications.

At the very beginning I thought that writing this program will be a huge problem for me but it was not that hard. The bottom line is that after each step carefully posed to test whether the program is working as we wanted. at the beginning of the program tested only observing how variables are changing. Then I added drawing and there was a couple of problems with whom I managed to cope with. Creating the program was an interesting experience for me, mainly looking at the fact this is my first such project. When testing a program operation error when I saw the program, was looking for the cause and tried to remove it. For variety action of the program I added color to the DOS window. When creating a program, I have not encountered major problems with whom I could not cope with.



Wyszukiwarka