c语言开发贪吃蛇游戏

首先,让我们来看一下游戏的基本要素:

  1. 贪吃蛇:玩家控制的主角,需要不断地吃食物变长,并且不能碰到自己或边界。

  2. 食物:随机出现在游戏画面中,被贪吃蛇吃掉后会增加其长度和分数。

  3. 得分系统:贪吃蛇每吃掉一个食物就会增加一定的得分。

  4. 边界检测:如果贪吃蛇碰到边界,则游戏结束。

  5. 自身碰撞检测:如果贪吃蛇碰到自己,则游戏结束。

接下来,我们可以通过C语言实现上述要素。

  1. 贪吃蛇的实现

    我们可以定义一个结构体表示贪吃蛇的每一个节点,每个节点包含了其当前位置以及指向下一个节点的指针。然后,我们可以使用动态内存分配来创建一个由多个节点组成的链表来表示整个贪吃蛇。

    当贪吃蛇移动时,我们只需要改变链表头部节点的位置,并将其插入到链表的头部即可。而当贪吃蛇吃到食物时,我们只需要在链表尾部添加一个新的节点即可。

  2. 食物的实现

    我们可以使用rand()函数来生成随机数,然后根据游戏画面的大小计算出食物的随机位置。

  3. 得分系统的实现

    我们可以定义一个变量表示当前玩家得分,每次贪吃蛇吃掉食物时,将得分增加对应的值即可。

  4. 边界检测的实现

    我们可以在游戏画面的边缘设置一条不可穿过的边界线,每次贪吃蛇移动时,判断其头部是否碰到了边界线即可。

  5. 自身碰撞检测的实现

    对于自身碰撞检测,我们只需要遍历整个贪吃蛇的链表,判断头部是否与其他节点重叠即可。

最后,在主程序中循环调用上述函数,并且使用终端输出来实现游戏画面即可。

代码:

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

#define WIDTH 40
#define HEIGHT 20

struct position {
    int x;
    int y;
};

struct snakeNode {
    struct position pos;
    struct snakeNode* next;
};

struct snake {
    struct snakeNode* head;
    int length;
};

struct food {
    struct position pos;
};

void initSnake(struct snake* s) {
    s->head = (struct snakeNode*)malloc(sizeof(struct snakeNode));
    s->head->pos.x = WIDTH / 2;
    s->head->pos.y = HEIGHT / 2;
    s->head->next = NULL;
    s->length = 1;
}

void destroySnake(struct snake* s) {
    struct snakeNode* cur = s->head;
    while (cur != NULL) {
        struct snakeNode* tmp = cur;
        cur = cur->next;
        free(tmp);
    }
    s->length = 0;
}

void drawSnake(struct snake* s) {
    struct snakeNode* cur = s->head;
    while (cur != NULL) {
        printf("o");
        cur = cur->next;
    }
    printf("\n");
}

void moveSnake(struct snake* s, struct position dir) {
    struct snakeNode* newHead = (struct snakeNode*)malloc(sizeof(struct snakeNode));
    newHead->pos.x = s->head->pos.x + dir.x;
    newHead->pos.y = s->head->pos.y + dir.y;
    newHead->next = s->head;
    s->head = newHead;
    s->length++;
}

int checkCollision(struct snake* s) {
    struct snakeNode* cur = s->head->next;
    while (cur != NULL) {
        if (cur->pos.x == s->head->pos.x && cur->pos.y == s->head->pos.y) {
            return 1;
        }
        cur = cur->next;
    }
    return 0;
}

void initFood(struct food* f) {
    srand(time(NULL));
    f->pos.x = rand() % WIDTH;
    f->pos.y = rand() % HEIGHT;
}

int checkFood(struct snake* s, struct food* f) {
    if (s->head->pos.x == f->pos.x && s->head->pos.y == f->pos.y) {
        return 1;
    }
    return 0;
}

void drawFood(struct food* f) {
    printf("F\n");
}

void updateFood(struct food* f) {
    srand(time(NULL));
    f->pos.x = rand() % WIDTH;
    f->pos.y = rand() % HEIGHT;
}

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

void clearScreen() {
    system("cls");
}

void displayScore(int score) {
    printf("Score: %d\n", score);
}

int main() {
    struct snake s;
    struct food f;
    struct position dir = { 1, 0 }; // 初始方向为向右
    int score = 0;

    initSnake(&s);
    initFood(&f);

    while (1) {
        clearScreen();
        drawBorder();
        drawSnake(&s);
        drawFood(&f);
        displayScore(score);

        if (_kbhit()) {
            char c = _getch();
            switch (c) {
            case 'w':
                dir.x = 0;
                dir.y = -1;
                break;
            case 's':
                dir.x = 0;
                dir.y = 1;
                break;
            case 'a':
                dir.x = -1;
                dir.y = 0;
                break;
            case 'd':
                dir.x = 1;
                dir.y = 0;
                break;
            default:
                break;
            }
        }

        moveSnake(&s, dir);

        if (s.head->pos.x < 0 || s.head->pos.x >= WIDTH || s.head->pos.y < 0 || s.head->pos.y >= HEIGHT) {
            printf("Game over!\n");
            break;
        }

        if (checkCollision(&s)) {
            printf("Game over!\n");
            break;
        }

        if (checkFood(&s, &f)) {
            updateFood(&f);
            score++;
        }
        else {
            struct snakeNode* cur = s.head;
            while (cur->next != NULL) {
                cur = cur->next;
            }
            free(cur);
            cur = NULL;
            s.length--;
        }

        Sleep(100);
    }

    destroySnake(&s);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛马程序员24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值