Flowchart
Explanation of Flowchart:
Start: The process begins here.
Initialize game: Initialize Snake Direction, food, snake, window, variables, and others
Start Game: Start gameplay.
Calculate Snake Next Position: For the first time, snake will start at a fix location. It will calculate the next position based on snake directions (up, down, left or right) provided by the user.
Check Snake Eating Food: After calculating next position, check whether the snake's next location and food location collide.
Increase length of snake and draw food: If yes, then food is eaten by the snake. Increase the length of the snake and draw food in another random location.
Check Collision: Check the snake's collision with snake body and window walls.
Draw next frame (snake): Based on snake next position and length, draw the snake.
Check Key Press: Check keyboard key presses by the user.
Change Snake Direction: Based on key press (up, down, left and right arrows) by the user, change the direction of snake.
Exit Game: Exit game if user presses ESC key or the snake collides with window or its body.
Data Structure
This is a 2D game played on flat window. So, each point on the window is (x, y) coordinate. The initial size of the game window is fixed, defined by the x-axis length and y-axis length. I am using an x-axis length 40 and y-axis length 20 of window. So, the maximum size of the snake is 40*20=800. The maximum number of foods required to display is also 800. Also, based on the widow size and starting position, we will calculate the coordinates of all four corners of the window.
#define winXLen 40 //Snake game play window x-coordinate size
#define winYLen 20 //Snake game play window y-coordinate size
#define startPosX 11 //Game play window starting x-coordinate
#define startPosY 1 //Game play window starting y-coordinate
#define startLen 3 //Snake starting length
//Max size of the snake must be always >= winXLen * winYLen
#define sSize 800
#define UP 0 //Up direction array index
#define DW 1 //Down direction array index
#define RT 2 //Right direction array index
#define LT 3 //Left direction array index
static const int winBorderX = winXLen + startPosX;
static const int winBorderY = winYLen + startPosY;
//First Corner of the Window
static const int wX1 = startPosX-1, wY1 = startPosY-1;
//Second Corner of the Window
static const int wX2 = winBorderX, wY2 = startPosY-1;
//Third Corner of the Window
static const int wX3 = winBorderX, wY3 = winBorderY;
//Fourth Corner of the Window
static const int wX4 = startPosX-1, wY4 = winBorderY;
We must always represent points (x, y) coordinates using single variable. It is done by defining struct variable Point2D with two int x and int y variables inside it. Using this structure variable, we can define each point of the snake, snake direction and food. The body of snake is an array of (x,y) coordinates. So, we can define snake by array of Point2D struct Point2D body[size].
To move snake in player's given direction, display moving snake and check for collision, we need snake characteristics such as snake's body, head, tail, length, and direction in each frame of the game. We must bundle all these characteristics in struct variable Snake. The structure variable struct Snake s define the snake in any given frame. Here, I am refreshing window in every 150 milliseconds so there will be 6-7 frames per second.
struct Point2D{
int x;
int y;
};
struct Snake{
struct Point2D body[sSize];
struct Point2D head;
struct Point2D tail;
struct Point2D dir;
int length;
};
struct Snake s;
//array for snake direction vector
struct Point2D dir[4];
struct Point2D food[sSize];
Algorithm
- Step 1: Initialize global variables such as window length, height, snake starting position, snake initial length, total number of moving coordinates inside the window.
- Step 2: Create C structure for 2D points, and the snake, and create global variables for snake, direction, and food.
- Step 3: Initialize direction, food, snake, and hide mouse cursor
- Step 4: Draw window and draw snake in initial starting position
- Step 5: Generate random value range 0 to maximum size of available coordinates and draw food in a random position
- Step 6: Start game and capture game status flag.
- Step 7: Initialize game status flag to zero and variable to capture user input
- Step 8: Start do-while loop and break loop when user presses a key.
- Step 8.1: Delay by 150 milliseconds or as needed so that there will be 6-7 frame per second.
- Step 8.2: Calculate Snake next position and check collision with food. Initialize snake head by next snake position. If snake do not collide with food, initialize tail variable by snake tail coordinates and swap the value of snake body array so that the next position will be the snake head, and the second last position will be the snake tail.
- Step 8.3: If the snake collides with food, draw food in random location, and increase snake length and game score. Otherwise, go to step 8.4.
- Step 8.4: Check for collisions of the snake with the window walls and within the snake body.
- Step 8.5: If the snake collides, then exit the game and go to step 9.
- Step 8.6: Draw the snake head in next position and delete the snake tail.
- Step 8.7: Check for key presses by user. If a key is entered, go to step 8.8; Otherwise, do nothing and continue loop from step 8.1.
- Step 8.8: If an arrow key (up, down, left, right) is entered, change the direction of the snake. If ESC key entered, exit the game. Otherwise, start the game again from step 8.
- Step 9: Display the game over message and request user to start game again by pressing any key or exit the game by pressing the ESC key.
- Step 10: If user press any key, start the game from step 3; Otherwise, exit the game.
Please find video of this article in YouTube.
No comments:
Post a Comment