1. Design ideas

in general , The little game Snake doesn't involve much , But logical thinking is more basic .

Gluttonous snake , There are three parts shown to us : snake , food , Map boundary .

We can mark these parts with a two-dimensional array :

For example, here I created a 5*10 Two dimensional array of , among -1 Table food , 1 Superficial snakehead ,2,3,4,5 Watch snake body ,0 Indicates open space .

If we want the snake to move , Just put Snaketail 0 , The rest +1 That's it .

In addition, it should be noted that , We use a s d w To control the direction , The integers corresponding to these four directions are :3,2,1,0.

2. Specific process

3. source file ( Main function )main.c

#include "head.h" int main() { int map[SIZE][SIZE] = { 0 };// Define a 2D array int len =
0;// Define length int state = 0;// Define game status int direc = 0;// Define direction Init_map(map, &len,
&direc);// Initialize map while (1) { direc = get_direc(direc);// Keyboard reading direction state = move(map,
direc, &len);// Snake Movement , Judge the snakehead status inside the function and return the value if (state == 0)// return 0 Table game end break; else// Continue the game
print(map);// Print Sleep(100); } return 0; }

4. Header file head.h
#include <stdio.h> #include <conio.h> #include <windows.h> #include <math.h>
#define SIZE 19 void Init_map(int map[SIZE][SIZE], int* len, int* direc); int
get_direc(int direc); int move(int map[SIZE][SIZE], int direc, int* len); void
print(int map[SIZE][SIZE]);
5. source file ( Function internal )func.c
void Init_map(int map[SIZE][SIZE], int* len, int* direc) { *len = 3;// Initialization length
*direc = 2;// Initialize direction map[5][5] = -1;// Initialize food // Initialize snake map[3][3] = 3; map[4][3] = 2;
map[5][3] = 1;// Initialize snakehead } int get_direc(int direc) { int tmp =
-1;// Define a variable to receive keyboard values if (_kbhit())// If the keyboard is struck ,kbhit() Returns a non 0 value { switch
(_getch())// Read from keyboard { case 'W':case 'w':tmp = 0; break; case 'D':case 'd':tmp =
1; break; case 'S':case 's':tmp = 2; break; case 'A':case 'a':tmp = 3; break; }
// The keyboard read in can only be w,d,s,a, And the snake can't turn around if (tmp != -1 && abs(direc - tmp) != 2)//abs Function is to find the absolute value
direc = tmp; } return direc; } int move(int map[SIZE][SIZE], int direc, int*
len) { int i = 0; int j = 0; // Define snakehead position int head_x = 0; int head_y = 0; // Loop through array
for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (map[i][j] ==
*len)// If it's snake tail map[i][j] = 0; if (map[i][j] > 1)// If it's a snake map[i][j] += 1; if
(map[i][j] == 1)// If it's a snake head { map[i][j] += 1; // At this time, snake head +1 Yes , There are no snakeheads , Now we need to find a new snake head //
// Find the original location of the original snake head head_x = i; head_y = j; // Find the position of the snake head after it moves switch (direc) { case 0:head_x
= i - 1; head_y = j; break; case 1:head_x = i; head_y = j + 1; break; case
2:head_x = i + 1; head_y = j; break; case 3:head_x = i; head_y = j - 1; break;
} } } } if (map[head_x][head_y] == -1)// If it passes the snake head, it is food { *len = *len + 1;// Eat food , length +1
map[head_x][head_y] = 1;// Eat food , Food turns into snake head // Regenerate food while (1) { i = rand() % SIZE; j
= rand() % SIZE; if (map[i][j] == 0) { map[i][j] = -1; break; } } }
// If the snake head touches itself or the wall else if (map[head_x][head_y] > 0 || head_x < 0 || head_x == SIZE
|| head_y < 0 || head_y == SIZE) return 0;// return 0 else map[head_x][head_y] =
1;// Didn't touch the food , wall , own return 1;// Didn't touch the food , wall , own } void print(int map[SIZE][SIZE]) {
// Print borders , food , Snakes use symbols according to their preferences , Freedom is unlimited system("cls"); int i = 0; int j = 0; // Print top border
for (i = 0; i <= SIZE; i++) { if (i == 0) printf("┏"); else if (i == SIZE)
printf(" ┓"); else printf(" ━"); } printf("\n"); // Print left , border-right , Print snakehead , Snake body , food for (i =
0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (j == 0) printf("┃ "); else
if (map[i][j] == -1) printf("★"); else if (map[i][j] > 0) printf("○"); else if
(map[i][j] == 1) printf("●"); else printf(" "); if (j == SIZE - 1) printf("┃");
} printf("\n"); } // Print bottom border for (i = 0; i <= SIZE; i++) { if (i == 0)
printf("┗"); else if (i == SIZE) printf(" ┛"); else printf(" ━"); }
printf("\n"); }
6. Concluding remarks

Can see , We get rid of useless line breaks , Comment Section , The real code is less than 200 that 's ok . This is just a snake game with basic functions . I hope you can add some fancy functions to this version . 

Finally, a screenshot of the game effect is attached .

 

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?