首先,让我们来看一下游戏的基本要素:
-
贪吃蛇:玩家控制的主角,需要不断地吃食物变长,并且不能碰到自己或边界。
-
食物:随机出现在游戏画面中,被贪吃蛇吃掉后会增加其长度和分数。
-
得分系统:贪吃蛇每吃掉一个食物就会增加一定的得分。
-
边界检测:如果贪吃蛇碰到边界,则游戏结束。
-
自身碰撞检测:如果贪吃蛇碰到自己,则游戏结束。
接下来,我们可以通过C语言实现上述要素。
-
贪吃蛇的实现
我们可以定义一个结构体表示贪吃蛇的每一个节点,每个节点包含了其当前位置以及指向下一个节点的指针。然后,我们可以使用动态内存分配来创建一个由多个节点组成的链表来表示整个贪吃蛇。
当贪吃蛇移动时,我们只需要改变链表头部节点的位置,并将其插入到链表的头部即可。而当贪吃蛇吃到食物时,我们只需要在链表尾部添加一个新的节点即可。
-
食物的实现
我们可以使用rand()函数来生成随机数,然后根据游戏画面的大小计算出食物的随机位置。
-
得分系统的实现
我们可以定义一个变量表示当前玩家得分,每次贪吃蛇吃掉食物时,将得分增加对应的值即可。
-
边界检测的实现
我们可以在游戏画面的边缘设置一条不可穿过的边界线,每次贪吃蛇移动时,判断其头部是否碰到了边界线即可。
-
自身碰撞检测的实现
对于自身碰撞检测,我们只需要遍历整个贪吃蛇的链表,判断头部是否与其他节点重叠即可。
最后,在主程序中循环调用上述函数,并且使用终端输出来实现游戏画面即可。
代码:
#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;
}