(Here the screenshot of snake game in C)
Prerequisite
Basic knowledge of C/C++ programming (up to array minimum required)
About the Game:
This game originated in 1976 arcade game and has taken several forms over the year. The goal of game is to attain maximum score by eating food without hitting to itself as each time the food snake eats will results in increase the size of snake
Implementation:
This game is a simple console application without graphics. But before going to deep into this lets first understand the basic game loop concept.
This is a basic game loop
In terms of simple C code
while(Game) do
processInput();
Update();
render();
end
Here we initialize the Game = 1 then if game-over state or condition is called then it takes value 0 and result in terminating of loop.
In whole loop first we process the input enter by user like any key pressed then based on that we update our game then after we draw or render the changes. This is a basic idea to develop any game.
void print()
void ResetScreenPosition()
void Movement()
void Foods()
void get_key()
void debugTail()
void Print()
void GameOver()
while(Game) do
processInput();
Update();
render();
end
Here we initialize the Game = 1 then if game-over state or condition is called then it takes value 0 and result in terminating of loop.
In whole loop first we process the input enter by user like any key pressed then based on that we update our game then after we draw or render the changes. This is a basic idea to develop any game.
Functions used in Snake Game:
Many functions have been used in this Snake mini project. Here, I will just list them below and describe the functions gotoxy, ResetScreenPosition() as they are some of the most important functions used in this and many mini projects in C
void snakeInitialisation()void print()
void ResetScreenPosition()
void Movement()
void Foods()
void get_key()
void debugTail()
void Print()
void GameOver()
void gotoxy (int x, int y) – You need to understand this function as it is one of the most important one used in Snake Game mini project in C. This function allows you to print text in any place of screen. Using this function in Code::Blocks requires coding, but it can be directly used in Turbo C. Here is a code for this function in Code::Blocks.
void ResetScreenPosition (int x, int y) – Here is the code for this function in Code::Blocks
Remember what our target was: we want to make a game in which there is a snake which has a head and a tail.the snake has to eat the food or any other thing to increase the score,,,as the score increases,,,the size of the snake should also increases.if it hits the boundary,,it should comes out from the opposite side of the boundary.if the snake hit its tail.the game will over.
Remember what our target was: we want to make a game in which there is a snake which has a head and a tail.the snake has to eat the food or any other thing to increase the score,,,as the score increases,,,the size of the snake should also increases.if it hits the boundary,,it should comes out from the opposite side of the boundary.if the snake hit its tail.the game will over.
SnakeInitialisation() function:
We define an array static(keyword in C) Snake[N][M] N,M rows and column. This function makes the whole frame using nested for loops and ASCII keys. Then we define the snake body of length 5, having one head and tail.
For rendering it we store its X,Y coordinate A[N][M] = 1 (for tail) A[N][M] =2(for head).
Foods Function():
Foods can render using math.random() function which is in math library of C for providing foods at any random position.
Movement function():
get_key() function gives the ASCII value of keys (w,a,s,d) with help of this we increase snake coordinates by 1 every time the key is pressed.
Score function():
Whenever the coordinates of foods and head coordinate of tail matches then we increase the score and tail length.
GameOver function():
Whenever the snake head hits by his tale the Game loop will calls for end
main function():
int main()
{
snakeInitialisation();
while(Game)
{
print();
ResetScreenPosition();
Foods();
Movement();
debugTail();
}
}
0 Comments