Snake code (novice)

Time:2021-5-12

The general production process is as follows:

In the drawing of the entrance interface, the defined gotoxy function is used to control the cursor position, and the color function is used to change the color

Draw the game interface, use the loop to draw the border and interior three times, pay attention to the horizontal spacing of 2, vertical spacing of 1

The snake body is drawn one by one with linked list

For the production of food, the header file is used, and the srand function is used to change the seed. Every time the food is generated, the snake body is traversed to determine whether it overlaps with the snake body. If it overlaps, the food is generated again

The keyboardcontrol function is used to interact with the keyboard. At the same time, according to each interaction, whether the food is eaten is judged. The length of the food is + 1, otherwise it moves normally

The score and speed are displayed in real time for each movement, which is realized by global variables

ESC exit confirmation requires the user to input a string to ensure more secure input_ Gets function

The end condition of the game is to hit the wall or bite yourself or exit by yourself. The first two are realized by coordinates

Free the memory after the game

Blogger himself is also a novice, if there is something wrong, let’s refuel together!

//Necessary header files

include

include

include

include

include

include

//Define the explicit constants above, below, left, right of the tag

define UP 1

define DOWN 2

define LEFT 3

define RIGHT 4

define ESC 5

define FOOD 10

//Defines the type of structure that represents the location

typedef struct snake{

int x;

int y;

struct snake *next;

}snake;

//Define global variables

int score = 0; // Current score

int speed = 200; // Store current speed

int status;

snake *tail,*head; // Storage snakehead and snaketail

snake *food,*q;// Q is used to traverse the linked list

HANDLE hOUT;

void gotoxy(int x,int y); // Set cursor position

int choice(void); // Load game interface

int color(int c); // Set font color

void printGame(void); // Print game interface

void printSnake(void); // Print snake body

void printFood(void); // Print food

void printTips(void); // Print tips

void snakeMove(void); // Main operation function

int biteSelf(void); // Judge if you bite yourself

int encounterWall(void); // Judge whether to hit the wall

void keyboardControl(void); // Get keystrokes

void speedUp(void); // accelerate

void speedDown(void); // Slow down

int endGame(void); // End function;

char *s_ gets(char *st,int n); // Read characters

void frees(snake *); // Free memory

int main(int argc, char *argv[]){

while(1)

{

if(choice() == 1)

keyboardControl();

else

{

gotoxy(5,15);

Printf (“press any key to return”);

getchar(); // Remove previous leading wrap

while(1)

{

if(getchar())

{

system(“cls”);

break;

}

}

}

}

frees(head);

return 0;

}

void gotoxy(int x,int y)

{

COORD c;

c.X=x;

c.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);

}

int choice(void)

{

int yourchoice;

//Draw the interface

gotoxy(35,5);

color(11);

Printf (“[t] snake wars'”);

printf(“\n\n”);

color(13);

printf(“\t\t★★★★★★★★ Snake!”);

printf(“\t\t★★★★★★★★ Snake!”);

gotoxy(25,15);

color(12);

Printf (“1. Enter the game, T2. View the instructions, T3. Exit the game, n”);

color(11);

Printf (“please select:);

scanf(“%d”,&yourchoice);

switch(yourchoice)

{

case 1:

system(“cls”);

//Initialization

printGame();

printSnake();

printFood();

break;

case 2:

system(“cls”);

printTips();

break;

case 3:

system(“cls”);

gotoxy(30,10);

color(11);

printf(“Bye!”);

exit(0);

default:

system(“cls”);

Printf (“if you don’t have this serial number, please enter 1, 2 or 3”);

Sleep(2000);

system(“cls”);

}

return yourchoice;

}

int color(int c)

{

SetConsoleTextAttribute(GetStdHandle(STD_ OUTPUT_ HANDLE), c); // Change text color

return 0;

}

void printGame()

{

int i,j;

gotoxy(5,5);

Printf (“game loading… Please wait”);

Sleep(2000);

system(“cls”);

//Print up and down interface

for(i = 0;i <= 50;i += 2)

{

gotoxy(i,0);

printf(“□”);

gotoxy(i,25);

printf(“□”);

}

//Print left and right interface

for(i = 0;i <= 25;i += 1)

{

gotoxy(0,i);

printf(“□”);

gotoxy(50,i);

printf(“□”);

}

//Print middle grid
Zhihui agency application http://www.kaifx.cn/broker/th…

for(i = 1;i <= 24;i += 1)

{

for(j = 2;j <= 48;j += 2)

{

gotoxy(j,i);

color(11);

printf(“■”);

}

}

//Print the rule and score column on the right

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

gotoxy(60,15);

Printf (“control the snake’s movement respectively with ↑ ↓ ←];

gotoxy(60,18);

Printf (“every time you get food plus 10 points, press F1 to accelerate, F2 to decelerate, space pause”);

gotoxy(60,20);

Printf (“don’t hit the wall or bite yourself!”);

gotoxy(60,22);

Printf (“speed not less than 100, not more than 300”);

}

void printSnake(void)

{

int i;

//Set snaketail (16,13), insert head, initially right

tail = (snake*)malloc(sizeof(snake));

tail->x = 16;

tail->y = 13;

tail->next = NULL;

//Set the initial snake length to 4

for(i = 1;i <= 4;i++)

{

head = (snake*)malloc(sizeof(snake));

head->next = tail;

head->x = 16+2*i;

head->y = 13;

tail = head; // The head becomes the tail

}

//Output snake body

while(tail->next)

{

gotoxy(tail->x,tail->y);

color(14);

printf(“★”);

tail = tail->next;

}

}

void printFood(void)

{

srand((unsigned)time(NULL)); // Using clock to modify seed

food = (snake*)malloc(sizeof(snake));

food->x = 1; // Initialize X coordinate

while(food->x%2 && food->x)

{

food->x = rand()%46+2;// 2-48

}

food->y = rand()%23+1; // 1-24

q = head; // Traversing the linked list without changing the header

while(q->next)

{

if(q->x == food->x && q->y == food->y)

{

free(food);

printFood();

}

else

{

gotoxy(food->x,food->y);

color(12);

printf(“●”);

break;

}

}

}

void printTips(void)

{

color(11);

printf(“***********Tips************\n”);

Using reasonable speed can get higher score\ n”);

Don’t hit yourself or the walls on both sides\ n”);

Press ESC to exit the game\ n”);

}

void snakeMove(void)

{

snake *snakenext;

snakenext = (snake*)malloc(sizeof(snake));

if(biteSelf())

{

gotoxy(60,11);

Printf (“bite yourself!”);

free(snakenext);

Sleep(1500);

system(“cls”);

exit(0);

}

else if(encounterWall())

{

gotoxy(60,11);

Printf (“hit the wall!”);

free(snakenext);

Sleep(1500);

system(“cls”);

exit(0);

}

else

{

//The first two conditions are judged before moving

Sleep(350-speed);

if(status == UP)

{

snakenext->x = head->x;

snakenext->y = head->y-1;

snakenext->next = head;

head = snakenext;

q = head;

if(snakenext->x == food->x && snakenext->y == food->y)

{

while(q)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

score += FOOD;

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

printFood();

}

else

{

while(q->next->next)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

gotoxy(q->next->x,q->next->y);

color(11);

printf(“■”);

free(q->next);

q->next = NULL;

}

}

else if(status == DOWN)

{

snakenext->x = head->x;

snakenext->y = head->y+1;

snakenext->next = head;

head = snakenext;

q = head;

if(snakenext->x == food->x && snakenext->y == food->y)

{

while(q)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

score += FOOD;

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

printFood();

}

else

{

while(q->next->next)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

gotoxy(q->next->x,q->next->y);

color(11);

printf(“■”);

free(q->next);

q->next = NULL;

}

}

else if(status == LEFT)

{

snakenext->x = head->x-2;

snakenext->y = head->y;

snakenext->next = head;

head = snakenext;

q = head;

if(snakenext->x == food->x && snakenext->y == food->y)

{

while(q)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

score += FOOD;

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

printFood();

}

else

{

while(q->next->next)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

gotoxy(q->next->x,q->next->y);

color(11);

printf(“■”);

free(q->next);

q->next = NULL;

}

}

else if(status == RIGHT)

{

snakenext->x = head->x+2;

snakenext->y = head->y;

snakenext->next = head;

head = snakenext;

q = head;

if(snakenext->x == food->x && snakenext->y == food->y)

{

while(q)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

score += FOOD;

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

printFood();

}

else

{

while(q->next->next)

{

gotoxy(q->x,q->y);

color(14);

printf(“★”);

q = q->next;

}

gotoxy(q->next->x,q->next->y);

color(11);

printf(“■”);

free(q->next);

q->next = NULL;

}

}

}

}

int biteSelf(void)

{

int x = 0; // Not biting myself by default

q = head->next;

//Traversing the snake

while(q->next)

{

if(q->x == head->x && q->y == head->y)

{

x = 1;

}

q = q->next;

}

return x;

}

int encounterWall(void)

{

int x = 0; // By default, it doesn’t hit the wall

if(head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)

x = 1;

return x;

}

void keyboardControl(void)

{

status=RIGHT; // The initial snake moves to the right

while(1)

{

if(GetAsyncKeyState(VK_ UP) && status != Down) / / the GetAsyncKeyState function is used to determine the state of the specified virtual key when the function is called

{

status=UP; // If the snake is not moving downward, press the up key to move upward

}

else if(GetAsyncKeyState(VK_ DOWN) && status != Up) / / if the snake is not moving up, press the key to move down

{

status=DOWN;

}

else if(GetAsyncKeyState(VK_ LEFT)&& status != Right) / / if the snake is not moving to the right, press the left key to move to the left

{

status=LEFT;

}

else if(GetAsyncKeyState(VK_ RIGHT)&& status != Left) / / if the snake is not moving to the left, press the right button to move to the right

{

status=RIGHT;

}

if(GetAsyncKeyState(VK_ Space)) / / space pause

{

while(1)

{

Sleep(300);

if(GetAsyncKeyState(VK_ Space)) / / press the space again to change the status

{

break;

}

}

}

else if(GetAsyncKeyState(VK_ESCAPE))

{

status = ESC; // Press ESC to go directly to the end interface

if(endGame())

{

Sleep(500);

system(“cls”);

break;

}

}

else if(GetAsyncKeyState(VK_ F1)) / / press F1 to speed up

{

speedUp();

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

}

else if(GetAsyncKeyState(VK_ F2)) / / press F2 to slow down

{

speedDown();

gotoxy(60,13);

Printf (“current score% D, current speed% d”, score, speed) “;

}

snakeMove();

}

}

void speedUp(void)

{

if(speed <= 280)

speed += 20;

}

void speedDown(void)

{

if(speed >= 120)

speed -= 20;

}

int endGame(void)

{

char x = 0;

char judge[5];

getchar();

gotoxy(60,9);

Printf (“are you sure you want to exit( Yes/No)”);

gotoxy(60,11);

s_gets(judge,5);

if(strcmp(judge,”Yes”) == 0)

{

Sleep(250);

system(“cls”);

gotoxy(40,11);

printf(“\tBye!”);

x = 1;

}

else

x = 0;

return x;

}

char *s_gets(char *st,int n)

{

char *ret_val;

char *find;

gotoxy(60,11);

ret_val = fgets(st,n,stdin);

if(ret_val)

{

find = strchr(st,’\n’);

if(find)

*find = ‘\0’;

else

while(getchar() != ‘\n’)

continue;

}

return ret_val;

}

void frees(snake *s)

{

snake *current = s;

while(current)

{

current = s;

s = current->next;

free(current);

}

}

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]