aiaiask.com©

开花即结果 提问即答案

用c语言结构体链表,文件操作写一个贪吃蛇小游戏为其赋予颜色并写一个计分系统记录玩家最好成绩
原始问题:

用c语言结构体链表,文件操作写一个贪吃蛇小游戏为其赋予颜色并写一个计分系统记录玩家最好成绩

下面是一个使用C语言结构体链表、文件操作的贪吃蛇小游戏示例,同时包含了颜色和计分系统:

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

#define WIDTH 20
#define HEIGHT 20

// 定义方向
enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT
};

// 定义蛇的节点
typedef struct SnakeNode {
    int x;
    int y;
    struct SnakeNode* next;
} SnakeNode;

// 定义食物的位置
typedef struct Food {
    int x;
    int y;
} Food;

// 全局变量
SnakeNode* head;
Food food;
int score;

// 初始化蛇
void initSnake() {
    head = (SnakeNode*)malloc(sizeof(SnakeNode));
    head->x = WIDTH / 2;
    head->y = HEIGHT / 2;
    head->next = NULL;
}

// 初始化食物
void initFood() {
    food.x = rand() % WIDTH;
    food.y = rand() % HEIGHT;
}

// 在屏幕上绘制蛇和食物
void draw() {
    system("cls");  // 清屏

    // 绘制蛇
    SnakeNode* p = head;
    while (p != NULL) {
        COORD coord;
        coord.X = p->x;
        coord.Y = p->y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        printf("*");
        p = p->next;
    }

    // 绘制食物
    COORD coord;
    coord.X = food.x;
    coord.Y = food.y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    printf("#");

    // 绘制分数
    coord.X = 0;
    coord.Y = HEIGHT + 1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    printf("Score: %d", score);
}

// 处理键盘输入
void input() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'w':
                move(UP);
                break;
            case 's':
                move(DOWN);
                break;
            case 'a':
                move(LEFT);
                break;
            case 'd':
                move(RIGHT);
                break;
            case 'q':
                exit(0);
                break;
        }
    }
}

// 移动蛇
void move(enum Direction dir) {
    SnakeNode* newHead = (SnakeNode*)malloc(sizeof(SnakeNode));
    newHead->next = head;
    switch (dir) {
        case UP:
            newHead->x = head->x;
            newHead->y = head->y - 1;
            break;
        case DOWN:
            newHead->x = head->x;
            newHead->y = head->y + 1;
            break;
        case LEFT:
            newHead->x = head->x - 1;
            newHead->y = head->y;
            break;
        case RIGHT:
            newHead->x = head->x + 1;
            newHead->y = head->y;
            break;
    }
    head = newHead;

    // 判断是否吃到食物
    if (head->x == food.x && head->y == food.y) {
        score++;
        initFood();
    } else {
        // 删除尾部节点
        SnakeNode* p = head;
        while (p->next->next != NULL) {
            p = p->next;
        }
        free(p->next);
        p->next = NULL;
    }
}

// 判断游戏是否结束
int isGameOver() {
    // 判断是否撞墙
    if (head->x < 0 || head->x >= WIDTH || head->y < 0 || head->y >= HEIGHT) {
        return 1;
    }

    // 判断是否撞到自己
    SnakeNode* p = head->next;
    while (p != NULL) {
        if (p->x == head->x && p->y == head->y) {
            return 1;
        }
        p = p->next;
    }

    return 0;
}

// 保存最高分到文件
void saveScore() {
    FILE* file = fopen("score.txt", "w");
    if (file == NULL) {
        printf("无法保存最高分!");
        return;
    }
    fprintf(file, "%d", score);
    fclose(file);
}

// 从文件中读取最高分
void loadScore() {
    FILE* file = fopen("score.txt", "r");
    if (file == NULL) {
        printf("无法读取最高分!");
        return;
    }
    fscanf(file, "%d", &score);
    fclose(file);
}

int main() {
    // 设置控制台窗口大小
    system("mode con cols=40 lines=25");

    // 设置控制台字体颜色
    system("color 0A");

    // 初始化随机数种子
    srand((unsigned)time(NULL));

    // 初始化蛇和食物
    initSnake();
    initFood();

    // 读取最高分
    loadScore();

    // 游戏循环
    while (1) {
        draw();
        input();
        move(UP);  // 自动移动
        if (isGameOver()) {
            saveScore();
            break;
        }
        Sleep(200);  // 控制游戏速度
    }

    return 0;
}

这个示例使用了Windows的控制台窗口来实现游戏界面的绘制,使用了conio.h库来处理键盘输入,使用了windows.h库来设置控制台窗口大小和字体颜色。游戏中的蛇和食物都是用字符来表示,蛇的身体用*表示,食物用#表示。游戏中的分数保存在文件score.txt中,每次游戏结束时会将当前分数保存到文件中,并在下次游戏开始时读取最高分。

用c语言结构体链表,文件操作写一个贪吃蛇小游戏为其赋予颜色并写一个计分系统记录玩家最好成绩
AI / 模型DeepSeek
时间 / DATEJune 28, 2023, 2:02 PM
语言zh-CN
IP / 区域黑龙江 哈尔滨

Prev:中国GDP