This is a classical snake game which has been played by everyone in their childhood. The game uses C++ libraries and user defined class 'snake' to implement it.
The game is based on two basic rules i.e. the snake has to eat the food and will grow in length. Secondly the head of snake can't touch it's own body, if it does the game will finish. Every time the consumption of food molecule by the snake gives 10 points to the player.
To implement this in C++ programming language we need to include three header files i.e iostream, conio.h, cstdlib.
Created a box of length and breadth 40 units each. Snake declaration has been done using two variables x(coordinate of snake's head), y(coordinate of snake's tail), array tail_X and tail_Y to store the postion of x and y respectively. Lastly ntail to count the length of snake.
For food's position which is generated randomly variable foodX (x-coordinate of food molecule) and foodY(y-coordinate of food molecule) is declared.
To count on score, point variable as been declared.
Functionalities of snake game has been declared using class snake inside which we have following functions in public mode:
void Setup(); //initial setup before the game starts
void Box(); //creation of box
void Play(); //declaring user input
void Logic(); //logic used
Control buttons for snake movement:
a: left direction
d: right direction
s: down direction
w: up direction
The speed of snake movement can also be controlled using _sleep() in the main() function.
The main logic which has been used for this game is, as soon as the head of the snake hits it's body the game terminates else the game will continue and randomly generate the food molecule which can be consumed by the snake. Incase if the snake crosses the left boundary of box , then it will appear from right side of the box and it applies for all the direction i.e. the snake just appears from the opposite side of the exit direction in the box.