C Language greedy snake complete code
#include <stdio.h> #include <stdlib.h> #include <Windows.h>//windows Programming header file
#include <time.h> #include <conio.h>// Console I / O header file #ifndef __cplusplus typedef
char bool; #define false 0 #define true 1 #endif // Move the cursor to the (x,y) Coordinate point void
gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } #define
SNAKESIZE 100// The maximum number of segments of a snake's body #define MAPWIDTH 78// width #define MAPHEIGHT 24// height
// Coordinates of food struct { int x; int y; }food; // Snake related attributes struct { int speed;// The speed at which the snake moves int
len;// The length of the snake int x[SNAKESIZE];// In each of the small squares that make up the body of the snake x Coordinates of int
y[SNAKESIZE];// In each of the small squares that make up the body of the snake y Coordinates of }snake; // Draw game borders void drawMap(); // Randomly generated food void
createFood(); // Key operation void keyDown(); // The state of the snake bool snakeStatus(); // Move cursor from console void
gotoxy(int x, int y); int key = 72;// Indicates the direction in which the snake moves ,72 For pressing “↑” Number represented
// It is used to determine whether the snake has eaten the food , This is an important step , It involves whether there will be the effect of snake body movement and the effect of snake body growth int changeFlag = 0; int sorce =
0;// Record the player's score int i; void drawMap() { // Print top and bottom borders for (i = 0; i <= MAPWIDTH; i +=
2)//i+=2 It's because it occupies two positions horizontally { // Move the cursor to (i,0) Print top border at gotoxy(i, 0); printf("■");
// Move the cursor to (i,MAPHEIGHT) Print bottom border at gotoxy(i, MAPHEIGHT); printf("■"); } // Print left and right borders for
(i = 1; i < MAPHEIGHT; i++) { // Move the cursor to (0,i) Print left border at gotoxy(0, i); printf("■");
// Move the cursor to (MAPWIDTH, i) Print left border at gotoxy(MAPWIDTH, i); printf("■"); } // Randomly generated food
while (1) { srand((unsigned int)time(NULL)); food.x = rand() % (MAPWIDTH - 4) +
2; food.y = rand() % (MAPHEIGHT - 2) + 1;
// The parity of the food abscissa generated must be consistent with the parity of the coordinates of the snake head in the initial test , Because one character occupies two byte positions , If not // It can lead to eating only half of the food if (food.x %
2 == 0) break; } // Move the cursor to the coordinates of the food to print the food gotoxy(food.x, food.y); printf("*");
// Initialized snake property snake.len = 3; snake.speed = 200; // Generate snake heads in the middle of the screen snake.x[0] = MAPWIDTH /
2 + 1;//x The coordinates are even snake.y[0] = MAPHEIGHT / 2; // Print snakehead gotoxy(snake.x[0],
snake.y[0]); printf("■"); // Generating the first trial snake body for (i = 1; i < snake.len; i++) {
// Printing of snake body , The ordinate remains unchanged , The abscissa is the coordinate value of the previous snake body +2 snake.x[i] = snake.x[i - 1] + 2; snake.y[i] =
snake.y[i - 1]; gotoxy(snake.x[i], snake.y[i]); printf("■"); }
// After printing the snake, move the cursor to the top of the screen , Avoid the cursor flashing all the time at the snake body gotoxy(MAPWIDTH - 2, 0); return; } void
keyDown() { int pre_key = key;// Record the direction of the previous key if (_kbhit())// If the user presses a key on the keyboard {
fflush(stdin);// Empty character buffer //getch() When reading the direction key , It will return twice , First call return 0 perhaps 224, The second call returns the actual value
key = _getch();// The first call did not return the actual value key = _getch();// The second call returns the actual value } /* * When the snake moves, wipe off a segment of its tail
*changeFlag by 0 It indicates that there is no food at this time , So every step you take is to erase the tail , To create a moving effect
* by 1 Indicates that food has been eaten , You don't need to erase the tail , To create a Snake growth effect */ if (changeFlag == 0) {
gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]); printf("
");// Output the space at the tail of the snake to erase the tail } // Move each segment of the snake forward one section in turn ( Except for snakeheads ) for (i = snake.len - 1; i > 0; i--)
{ snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; }
// The snake cannot move in the opposite direction from the previous one , For example, when the snake goes to the left, it can't press the right button to go right // If the current movement direction is opposite to the previous direction , Change the current direction of movement to the previous direction
if (pre_key == 72 && key == 80) key = 72; if (pre_key == 80 && key == 72) key =
80; if (pre_key == 75 && key == 77) key = 75; if (pre_key == 77 && key == 75)
key = 77; /** * The number represented by the console key *“↑”:72 *“↓”:80 *“←”:75 *“→”:77 */ // Determine which direction the snake head should move
switch (key) { case 75: snake.x[0] -= 2;// To the left break; case 77: snake.x[0] +=
2;// To the right break; case 72: snake.y[0]--;// Up break; case 80: snake.y[0]++;// down
break; } // Print the snake head gotoxy(snake.x[0], snake.y[0]); printf("■"); gotoxy(MAPWIDTH -
2, 0); // As there is no food to eat at present ,changFlag The value is 0 changeFlag = 0; return; } void createFood() {
if (snake.x[0] == food.x && snake.y[0] == food.y)// Snake head meets food {
// The snake's head touches the food to eat it , So you need to make a food again while (1) { int flag = 1; srand((unsigned
int)time(NULL)); food.x = rand() % (MAPWIDTH - 4) + 2; food.y = rand() %
(MAPHEIGHT - 2) + 1; // Randomly generated food cannot be on the snake's body for (i = 0; i < snake.len; i++) { if
(snake.x[i] == food.x && snake.y[i] == food.y) { flag = 0; break; } }
// Randomly generated food cannot have an odd abscissa , It can't be in the snake , Otherwise regenerate if (flag && food.x % 2 == 0) break; } // Drawing food
gotoxy(food.x, food.y); printf("*"); snake.len++;// Eating food , Snake length plus 1 sorce +=
10;// Every food gets 10 branch snake.speed -= 5;// As more and more food is being eaten , It's going to get faster and faster changeFlag =
1;// Very important , Because of the food , You don't have to erase the snake tail , To create a snake body growth effect } return; } bool snakeStatus() {
// Snake head meets upper and lower boundary , game over if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT) return false;
// The snake head hits the left and right borders , game over if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH) return false;
// The head of the snake touches the body of the snake , game over for (i = 1; i < snake.len; i++) { if (snake.x[i] == snake.x[0] &&
snake.y[i] == snake.y[0]) return false; } return true; } int main() {
drawMap(); while (1) { keyDown(); if (!snakeStatus()) break; createFood();
Sleep(snake.speed); } gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2); printf("Game
Over!\n"); gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2 + 1); printf(" The score of this game is :%d\n",
sorce); Sleep(5000); return 0; }

Technology