Snake game C++ command line version of the example code

  • 2020-04-02 02:45:36
  • OfStack

This example tells the snake game C++ command line version of the implementation code, is a very classic game. Share with you for your reference. The specific implementation method is as follows:

As we all know, snake game is a classic computer game.

The game description is as follows:

1. Snake can automatically move forward in a straight line, or the player can control snake to move forward up, down, left, right, one space at a time by the direction key.
2. The snake moves within the prescribed area when:

When the snake touches the wall;

When the head of a snake touches the body or tail of a snake;

The player's keyboard input is not the direction of the key;

The command line says "Game Over!" And quit the game.

3. A "dou dou" is generated randomly in the area where the snake is active. When the snake eats the "dou dou", the snake body increases by one bar, and the automatic advance time decreases by 100ms(the default is 1000ms and no less than 100ms). The snake's length should Improve a Level.

C++ code is as follows:


#include <bios.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

inline void display(char gsDomain[][22], int level, int moveSpeed)
{
system("cls"); //Clear the screen
cout << endl << endl;
for (int i = 0; i < 22; i++)
{
cout << "t";
for (int j = 0; j < 22; j++)
cout << gsDomain[i][j] << " ";
if (i == 0)
{
cout << "tLevel : " << level;
}
else if (i == 3)
{
cout << "t Automatic advance time ";
}
else if (i == 5)
{
cout << "t Interval: " << moveSpeed << " ms";
}

cout << endl;
}
}

int main()
{
char gsDomain[22][22]; //Snake area (including walls)
//Initialize snake activity area (excluding walls)
for (int i = 1; i <= 21; i++)
{
for (int j = 1; j <= 21; j++)
gsDomain[i][j] = ' ';
}
//Initializes the upper and lower walls of the snake area
for (int i = 0; i < 22; i++)
gsDomain[0][i] = gsDomain[21][i] = '-';
//Initializes the left and right walls of the snake area
for (int i = 1; i < 21; i++)
gsDomain[i][0] = gsDomain[i][21] = '|';
//Initializes the snake body
for (int i = 1; i <= 3; i++)
gsDomain[1][i] = '*';
//Initializes the snake head
gsDomain[1][4] = '#';

int snake[2][100]; //Record the coordinates of the position of each snake
for (int i = 0; i < 4; i++)
{
snake[0][i] = 1; //Record the x position of the snake
snake[1][i] = i + 1; //Record the position of the snake in the y-coordinate
}
int head = 3, tail = 0, length = 4;

int beanX, beanY; //Where the beans appear
srand(time(0));
do
{
beanX = rand() % 20 + 1;
beanY = rand() % 20 + 1;
} while (gsDomain[beanX][beanY] != ' ');
gsDomain[beanX][beanY] = '*'; //doug

cout << "nntt The snake game is about to begin !n";
long start;
int level = 1, moveSpeed = 1000;
for (int i = 3; i >= 0; i--)
{
start = clock();
while (clock() - start <= 1000){}
system("cls");
if (i)
{
cout << "nntt Enter the game countdown: " << i << endl;
}
else
display(gsDomain, level, moveSpeed);
}

char direction = 77; //The snake automatically goes straight to the right by default
while (true)
{
bool timeFlag = true;
int x, y;
start = clock();

//If the time exceeds the automatic forward time or a key is pressed on the keyboard, the loop is terminated
while ((timeFlag = (clock() - start <= moveSpeed)) && !kbhit()){}

if (timeFlag)
{
//Keyboard input is read when a key is pressed on the keyboard
getch();
direction = getch();
}

switch (direction)
{
//upward
case 72: x = snake[0][head] - 1, y = snake[1][head];
break;
//down
case 80: x = snake[0][head] + 1, y = snake[1][head];
break;
//To the left
case 75: x = snake[0][head], y = snake[1][head] - 1;
break;
//To the right
case 77: x = snake[0][head], y = snake[1][head] + 1;
break;
default: cout << "tGame Over!n";
return 0;
}

if (x == 0 || x == 21 || y == 0 || y == 21)
{
//The greedy snake touched the wall
cout << "tGame Over!n";
return 0;
}

if (gsDomain[x][y] != ' ' && !(x == beanX && y == beanY))
{
//The head of a snake touches the body or tail of a snake
cout << "tGame Over!n";
return 0;
}

if (x == beanX && y == beanY)
{
// eat doug
length++; //The length of the 1
if (length >= 8)
{
//Game upgrade processing
length -= 8;
level++;
if (moveSpeed > 100)
moveSpeed -= 100;
}
gsDomain[snake[0][head]][snake[1][head]] = '*';
gsDomain[x][y] = '#';
head = (head + 1) % 100;
snake[0][head] = x;
snake[1][head] = y;
do
{
beanX = rand() % 20 + 1;
beanY = rand() % 20 + 1;
} while (gsDomain[beanX][beanY] != ' ');
gsDomain[beanX][beanY] = '*';

display(gsDomain, level, moveSpeed); //On-screen display
}
else
{
// Don't eat doug
gsDomain[snake[0][tail]][snake[1][tail]] = ' '; //The snake's tail moves forward by one bar
tail = (tail + 1) % 100;
gsDomain[snake[0][head]][snake[1][head]] = '*';
head = (head + 1) % 100;
snake[0][head] = x;
snake[1][head] = y;
gsDomain[x][y] = '#'; //The head of the snake moves forward one space
display(gsDomain, level, moveSpeed); //On-screen display
}
}

return 0;
}

I hope that the examples described in this paper will be helpful to the study of C programming.


Related articles: