C language low match greedy snake game

Character Game-Snake


How to write a small snake game in C language?
The first is the scene setting of the game, first of all initialization and related presets and variable declarations:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>r BL_CHA ' ';
char WALL_CHAR = '*';
char SNAKE_HEAD_CHAR = 'H';
char SNAKE_BODY_CHAR = 'X';
char FOOD_CHAR = '$';
char map[12][13] = {
    "************",
    "*XXXXH     *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "************",
};

int snakeBodyX[100] = { 1,1,1,1 }, snakeBodyY[100] = { 4,3,2,1 };
int snakeBodyLen = 4;
int snakeTailIndex = 3;
int willBeLonger = 0;

int foodX = 0, foodY = 0;

int gameRunning = 1;

Then refresh the interface for every input symbol:


void printMap() {
system("cls");
for (int i = 0; i < 12; ++i) {
printf("%s\n", map[i]);
}
}

Randomly create food:

void spaood() {
    // Random food position
    foodX = rand() % 10 + 1;
    foodY = rand() % 10 + 1;
    while (map[foodX][foodY] != BLANK_CHAR) {
        foodX = rand() % 10 + 1;
        foodY = rand() % 10 + 1;
    }
    map[foodX][foodY] = FOOD_CHAR;
}

Enter the characters to move the snake and generate related events, eat food and determine whether the game fails:

void snakeMove(char control) {
    map[snakeHeadX][snakeHeadY] = BLANK_CHAR;
    // record the previous snake head position
    int prevSnakeHeadX = snakeHeadX;
    int prevSnakeHeadY = snakeHeadY;
    switch (control) {
    case 'w':
        snakeHeadX--;
        break;
    case 'a':
        snakeHeadY--;
        break;
    case 's':
        snakeHeadX++;
        break;
    case 'd':
        snakeHeadY++;
        break;
    default:
        return;
    }
    if (map[snakeHeadX][snakeHeadY] != BLANK_CHAR && map[snakeHeadX][snakeHeadY] != FOOD_CHAR) {
        // DIED
        gameOver();
    }
    map[snakeHeadX][snakeHeadY] = SNAKE_HEAD_CHAR;

    int moved = 0;
    // If willBeLonger, then make it longer
    if (willBeLonger) {
        willBeLonger = 0;
        moved = 1;
        // make space
        for (int i = snakeBodyLen - 1; i > snakeTailIndex; --i) {
            snakeBodyX[i + 1] = snakeBodyX[i];
            snakeBodyY[i + 1] = snakeBodyY[i];
        }
        snakeBodyX[snakeTailIndex + 1] = prevSnakeHeadX;
        snakeBodyY[snakeTailIndex + 1] = prevSnakeHeadY;
        if (snakeTailIndex < 0) snakeTailIndex = 0;
        map[prevSnakeHeadX][prevSnakeHeadY] = SNAKE_BODY_CHAR;
        snakeBodyLen++;
    }

    // Check if ate food
    if (snakeHeadX == foodX && snakeHeadY == foodY) {
        willBeLonger = 1;
        spawnFood();
    }

    // Head has already moved, **Move the body**
    // if "no body" here or added tail just now, then it needn't move
    if ( moved) return;
    map[snakeBodyX[snakeTailIndex]][snakeBodyY[snakeTailIndex]] = BLANK_CHAR;
    snakeBodyX[snakeTailIndex] = prevSnakeHeadX;
    snakeBodyY[snakeTailIndex] = prevSnakeHeadY;
    map[prevSnakeHeadX][prevSnakeHeadY] = SNAKE_BODY_CHAR;
    // Move tail index
    snakeTailIndex = (snakeTailIndex - 1 + snakeBodyLen) % snakeBodyLen;
}

Game over interface:

void gameOver() {
    printf("GAME OVER!!\n");
    gameRunning = 0;
}

Main program: start and run the game:

int main() {
    // Initialize random seed
    srand(time(NULL));

    initGame();

    char control = 0;
    while (gameRunning) {
        scanf(" %c", &control); // " %c": the space is used to skip blank character, like '\n'
        snakeMove(control);
        printMap();
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/jessiaflora/article/details/78907475