以下是使用链表结构实现的贪吃蛇的 C 语言程序:

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

#define WIDTH 20
#define HEIGHT 10

typedef struct Node {
    int x;
    int y;
    struct Node *next;
} Node;

Node *head;
int direction;
int gameover;

void Draw()
{
    system("cls");
    for (int i = 0; i < WIDTH + 2; i++) {
        printf("#");
    }
    printf("\n");

    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (j == 0 || j == WIDTH - 1) {
                printf("#");
            } else if (i == head->y && j == head->x) {
                printf("O");
            } else {
                Node *temp = head->next;
                int isBody = 0;
                while (temp != NULL) {
                    if (temp->x == j && temp->y == i) {
                        printf("o");
                        isBody = 1;
                        break;
                    }
                    temp = temp->next;
                }
                if (!isBody) {
                    printf(" ");
                }
            }
        }
        printf("\n");
    }

    for (int i = 0; i < WIDTH + 2; i++) {
        printf("#");
    }
    printf("\n");
}

void Init()
{
    direction = 'd';
    gameover = 0;

    Node *node1 = (Node *)malloc(sizeof(Node));
    node1->x = WIDTH / 2;
    node1->y = HEIGHT / 2;
    node1->next = NULL;
    head = node1;

    Node *node2 = (Node *)malloc(sizeof(Node));
    node2->x = WIDTH / 2 - 1;
    node2->y = HEIGHT / 2;
    node2->next = NULL;
    node1->next = node2;

    Node *node3 = (Node *)malloc(sizeof(Node));
    node3->x = WIDTH / 2 - 2;
    node3->y = HEIGHT / 2;
    node3->next = NULL;
    node2->next = node3;
}

void Input()
{
    if (_kbhit()) {
        switch (_getch()) {
            case 'a':
                direction = 'a';
                break;
            case 'd':
                direction = 'd';
                break;
            case 'w':
                direction = 'w';
                break;
            case 's':
                direction = 's';
                break;
            case 'q':
                gameover = 1;
                break;
        }
    }
}

void Update()
{
    int prevX = head->x;
    int prevY = head->y;
    int nextX, nextY;

    switch (direction) {
        case 'a':
            nextX = prevX - 1;
            nextY = prevY;
            break;
        case 'd':
            nextX = prevX + 1;
            nextY = prevY;
            break;
        case 'w':
            nextX = prevX;
            nextY = prevY - 1;
            break;
        case 's':
            nextX = prevX;
            nextY = prevY + 1;
            break;
    }

    Node *temp = head->next;
    int tempX, tempY;
    while (temp != NULL) {
        tempX = temp->x;
        tempY = temp->y;
        temp->x = prevX;
        temp->y = prevY;
        prevX = tempX;
        prevY = tempY;
        temp = temp->next;
    }

    if (nextX < 0 || nextX >= WIDTH || nextY < 0 || nextY >= HEIGHT) {
        gameover = 1;
    }

    if (nextX == head->x && nextY == head->y) {
        gameover = 1;
    }

    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->x = nextX;
    newNode->y = nextY;
    newNode->next = head->next;
    head->next = newNode;

    head->x = nextX;
    head->y = nextY;
}

void Clean()
{
    Node *temp = head;
    while (temp != NULL) {
        Node *nextNode = temp->next;
        free(temp);
        temp = nextNode;
    }
}

int main()
{
    Init();

    while (!gameover) {
        Draw();
        Input();
        Update();
        Sleep(100);
    }

    Clean();
    printf("Game Over!\n");

    return 0;
}

这个程序使用链表来表示贪吃蛇的身体,每个节点的 x 和 y 坐标表示贪吃蛇身体的一个部分。在游戏的更新过程中,根据用户的输入改变蛇的移动方向,并根据当前的方向更新蛇的位置。程序还包括了游戏结束的判断,当蛇碰到边界或者自己的身体时,游戏结束。

注意:该程序使用了 Windows 系统特定的函数 conio.h 和 windows.h,如果你在其他操作系统上运行该程序可能会出现问题。

C 语言链表实现贪吃蛇游戏

原文地址: https://www.cveoy.top/t/topic/pZIr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录