C Language Practical Exercise: Snake Game

The C language implementation of the Snake game mainly includes the following steps:

 

 

1. Initialize the game interface:

 

Set window size, background color, etc.

 


2. Create the snake’s data structure:

 

 

Including the position of the snake's head, the length of the snake's body, the coordinates of the snake's body, etc.


3. Create the food data structure:

 

Including location of food etc.


4. Control the movement of the snake:

 

Change the position of the snake's head according to the user's input (up, down, left and right keys), and then update the coordinates of the snake's body.

 


5. Determine whether the game is over:

 

If the snake's head touches its own body or the window border, the game ends.

 

 


6. Draw the game interface:

 

After each game state update, the game interface, including snakes and food, needs to be redrawn.

 

 

 

 

 

The following is an example of C language code for a simple snake game:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

#define WIDTH 50
#define HEIGHT 25

int snake_x[WIDTH * HEIGHT], snake_y[WIDTH * HEIGHT];
int food_x, food_y;
int snake_length;
int direction; // 0:上, 1:右, 2:下, 3:左

void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void init() {
    srand((unsigned)time(NULL));
    snake_length = 3;
    direction = rand() % 4; // 随机生成初始方向
    for (int i = 0; i < snake_length; i++) {
        snake_x[i] = snake_length - i - 1;
        snake_y[i] = rand() % (HEIGHT - 2) + 1; // 蛇身不能出界
    }
    food_x = rand() % (WIDTH - 2) + 1;
    food_y = rand() % (HEIGHT - 2) + 1;
}

void draw() {
    system("cls"); // 清屏
    for (int i = 0; i < WIDTH; i++) {
        printf("#");
    }
    printf("
");
    for (int i = 1; i < HEIGHT - 1; i++) {
        printf("#");
        for (int j = 1; j < WIDTH - 1; j++) {
            int is_snake = 0;
            for (int k = 0; k < snake_length; k++) {
                if (snake_x[k] == j && snake_y[k] == i) {
                    printf("*"); // 蛇身用*表示
                    is_snake = 1;
                    break;
                }
            }
            if (!is_snake) {
                if (food_x == j && food_y == i) {
                    printf("@"); // 食物用@表示
                } else {
                    printf(" "); // 空白表示空地
                }
            }
        }
        printf("#
");
    }
    for (int i = 0; i < WIDTH; i++) {
        printf("#");
    }
    printf("
");
}

void update() {     int next_x = snake_x[0], next_y = snake_y[0];     switch (direction) {         case 0: next_y--; break; // Move up one space         case 1: next_x++; break; // Move one space right         case 2: next_y++; break; // Move one space down         case 3: next_x--; break; // Move one space left     }     if (next_x == food_x && next_y == food_y) { // If food is eaten, the length is increased by one and the food position is regenerated         snake_length++;         food_x = rand() % (WIDTH - 2) + 1;         food_y = rand() % (HEIGHT - 2) + 1;< /span>     } else { // If no food is eaten, remove the snake's tail, that is, the current position remains unchanged, other positions move forward one position, and the last position is set to a vacant position (i.e., open space) ``` }     }             use using ’ ’ s using ’ ’s ’ ’ s ’ t ‐ ‐ ‐ ‐ ‐ n n ‐ _ >                                                                    snake_x[0] =       next_x; next_y; // Because all other positions have been moved forward by one position, there is no need to move it here. Just set it to next_x, next_y (that is, the new coordinates of the snake head)             snake_x[i] = snake_x[i - 1];         for (int i = snake_length - 1; i > 0; i--) {




















 

Guess you like

Origin blog.csdn.net/2301_79368222/article/details/134762987