Project - Snake Game
Project - Snake Game
Attention:
● Make sure that you read and understand each and every instruction. If you have any
questions or comments you are encouraged to discuss your problems with your
colleagues (and instructors).
● Plagiarism is strongly forbidden and will be very strongly punished. If we find that you
have copied from someone else or someone else has copied from you (with or without
your knowledge) both of you will be punished. You will be awarded (straight zero in the
project which can eventually result in your failure) and appropriate action as
recommended by the Disciplinary Committee (DC can even award a straight F in the
subject) will be taken.
● Try to understand and do the project yourself even if you are not able to complete the
project so that you will be awarded according to your effort.
● Divide and conquer: since you have around 13 days so you are recommended to divide
the complete task in manageable subtasks. We recommend to complete the drawing
and design (i.e. number of functions and their order of calling) phase as quickly as
possible and then focus on the intelligence phase.
● Before writing even one line of code, you must design your final project. This process will
require you to break down and outline your program into functionality of your program
and pseudocode important methods. After designing your program you will find that
writing the program is a much simpler process.
● Imagination Powers: Use your imaginative powers to make this interesting and appealing
as you can think of. An excellent solution can get you bonus marks.
Game Description:
Most of you have played a very interesting game “Snake” on your old Nokia phones
(Black & White). Now it is your time to create it with more interesting colors and features. When
the game is started a snake is controlled by up, down, left and right keys to eat food which
appears on random locations. By eating food snake’s length increases one unit and player’s
score increases by 5 points. Food disappears after 15 seconds and appears again on some
other random location. The game also has hurdles placed at the random locations. The snake
dies if it collides with itself or the hurdle.
1. Create board (hint: a 2D array) and snake. Snake first block (mouth) must be a circle
and have at least 2 squares as the tail.
2. Move snake
3. Show player score on progress bar at top of the game.
4. Display five foods at a particular time at random locations on a board by following the
following rules
a. Not two food items are on same row.
b. Not two food items are on the same columns.
c. Not two food are along the same diagonal (primary and secondary).
d. When the snake eats the food, the food item again appear by following the same
conditions described above.
e. The length of snake increases when it eats food.
f. Each food disappears after 15 seconds of its placement.
5. Introduce the hurdles on board.
a. You can have at least one hurdle and at most three.
b. The snake should avoid the hurdles while navigating to eat food item.
c. The hurdles disappear after 30 seconds and reappear again at different locations
on the board.
d. New hurdles should be placed such as they do not collide with already placed
food items and snake.
e. Hurdles can take “L-shape”, “U-shape”,”Z-shape”, and horizontal or vertical lines
of variable lengths.
f. You can increase or decrease the difficulty level using hurdles.
6. Game must terminate when snake collide with itself or with hurdles by showing a
message game over and your score is “XYZ”. If score is highest show a message “you
have broken all previous records and now highest score is xyz”. After this, start the game
once again.
7. Display power food after each minute and it should disappear after 15 seconds. If player
eats a power food its score is increased by 20.
8. Save highest score and players history in files and display corresponding information
when user selects an option from them (figure 2).
How to Start:
In this project you will be provided code of some functions to draw circle, rectangle, print string
and code to control keys (up, down, left and right). You are not required to understand the given
functions implementation, you only have to call these functions by passing appropriate
arguments. For example if you want to draw a circle you will call a function “DrawCircle( int
starting_x_axis, int starting_y_axis, float radius , float color_of_circle )” and it will draw a circle
depending upon the arguments. Same for remaining functions you will only call them and they
will do your job. Details of these functions are given in code you can see them from there.
Instructions
We provide complete skeleton with all the basic drawing functions (can be located in util.h and
util.cpp) needed in project with detailed instructions and documentation. In other words, all you
need to know for building the game is provided. Your main task will be to understand the main
design of game and implement it. However, before proceeding with code writing you will need to
install some required libraries.
To compile the game (skeleton) each time you will be using “g++”. However to automate the
compilation and linking process we use a program “make”. Make takes as an input a file
containing the names of files to compile and libraries to link.
This file is named as “Makefile” in the game folder and contains the detail of all the libraries that
game uses and need to linked. So each time you need to compile and link your program (game)
you will be simply calling the “make” utility in the game directory on the terminal to perform the
compilation and linking.$: make
That’s it if there are no errors you will have your game executable (on running you will see three
shapes on your screen). Otherwise try to remove the pointed syntax errors and repeat the make
procedure.
You only have to modify the code in "game-release.cpp" file and particularly in "void Display()"
function which is called by graphics library automatically. After each modification you need to
run $: make command to compile your code and link binary libraries. This will create an exe. file
named game-release which will be executable. Run that game-release file to verify the
modification in your game by $: ./game-release command. If you run the skeleton code that we
have provided you, a window like below will appear on your screen (figure 3).
Figure 3: Shows skeleton code output
/* Function sets canvas size (drawing area) in pixels. that is what dimensions (x and y) your
game will have. Note that the bottom-left coordinate has value (0,0) and top-right coordinate has
value (width-1,height-1). To draw any object you will need to specify its location */
void SetCanvasSize(int width, int height)
// Function draws a line between point P1(x1,y1) and P2(x2,y2) of given width and colour
void DrawLine(int x1, int y1, int x2, int y2, int lwidth = 3, float *color =NULL);
Drawing Primitives
Once we have defined the canvas our next goal will be to draw the game board and its
elements using basic drawing primitives. For drawing each object we will need to specify its
elementary point’s locations (x & y coordinates) in 2D canvas space and its size. You will only
need squares as drawing primitives to draw the complete board, and its pieces. For this
purpose, skeleton code already include functions for drawing lines, squares circles, curves,
rounded rectangles (see below), triangles at specified location.
Recall that a line needs two vertices (points) where as a triangle needs three vertices so to draw
these primitives we will need to provide these vertices (points) locations along with shape’s
color – c.f . Skeleton already provides a list of ≈ 140 colors which can be used for coloring
different shapes note that each color is combinations of three individual components red, green
and blue.
Drawing Board
Initially it might seem drawing and managing the board is extremely difficult. However this
difficulty can be overcome using a very simple trick of divide and conquer. The trick revolve
around the idea of the board being split into tiles. “Tile” or “cell” in this context refers to an 10 ×
10 pixels square box, you can use any tile size as you wish – pixel square on the screen.
Snake’s screen resolution is 650 × 650, so this gives us a total board size of 65x65 tiles. Since
we will be working independent of pixel units, so we can define tile size in our coordinates units.
So drawing and managing the board will require these two steps:
1. Splitting the board in tiles.
2. Finding and storing what part of piece to draw in each tile.
Snake board divided into a grid of tiles or cells. Note that once we have divided the board into a
grid we can build a 2D table in which we can record the contents corresponding to each tile, i.e.
what shape it contains and at what location. Given the table of contents we can loop over this
table and draw the complete board.
Remember that you can do your drawing only in the Display() function, that is only those objects
will be drawn on the canvas that are mentioned inside the Display function. This Display
function is automatically called by the graphics library whenever the contents of the canvas
(window) will need to be drawn i.e. when the window is initially opened, and likely when the
window is raised above other windows and previously obscured areas are exposed, or when
glutPostRedisplay() is explicitly called. In short, Display function is called automatically by the
library and all the things inside it are drawn. However whenever you need to redraw the canvas
you can explicitly call the Display() function by calling the function glutPostRedisplay(). For
instance, you will calling the Display function whenever you wanted to animate (move) snake;
where first you will set the new positions of your objects and then call the glutPostRedisplay() to
redraw the objects at their new positions. Also see the documentation of Timer function.
/*This function is called (automatically by library) whenever any non-printable key (such as
up-arrow, down-arraw) is pressed from the keyboard. You will have to add the necessary code
here
when the arrow keys are pressed or any other key is pressed...
* This function has three argument variable key contains the ASCII of the key pressed, while x
and y tells the program coordinates of mouse pointer when key was pressed.*/
void NonPrintableKeys(int key, int x, int y)
/* This function is called (automatically by library) whenever any printable key (such as x,b,
enter, etc.) is pressed from the keyboard. This function has three argument variable key
contains the ASCII of the key pressed, while x and y tells the program coordinates of mouse
pointer when key was pressed.*/
void PrintableKeys(unsigned char key, int x, int y)
Collision Detection
Finally, once you have done the drawing and animation of objects (snake , board, food) on your
canvas. Your final goal will be to detect collisions (collision test) between objects (snake and
food , snake and hurdles) and take necessary actions, e.g. to check whether a snake and a food
collide if collide disappear food from that location and display it on some other random location
increase length of snake, score and progress bar. If collide with hurdle or itself terminate the
game.
Acknowledgments:
We acknowledge that attention-instructions, libraries to draw basic primitives and code-skeleton
are taken from Computer Programing course that was taught in spring 2015 at FAST University
Islamabad by faculty members: Sibt ul Hussain, Fareed Ahmed, Usman Farrokh.