用c语言和链表编写贪吃蛇游戏
时间: 2023-08-10 07:39:53 浏览: 87
C语言链表实现的贪吃蛇小游戏.zip
5星 · 资源好评率100%
好的,这是一个有趣的项目!首先,我们需要定义一个蛇的结构体,包含蛇头和蛇身体的位置、当前的运动方向等信息。然后,我们需要定义一个链表结构体,用于表示蛇身体的每一个节点。接下来,我们可以按照以下步骤来实现该游戏:
1. 初始化蛇的结构体和链表结构体,将蛇头和蛇身体的位置添加到链表中。
2. 在屏幕上绘制蛇头和蛇身体的位置。
3. 接受用户的输入,根据输入更新蛇的方向。
4. 根据蛇的方向移动蛇头的位置,并更新链表中蛇身体的位置。
5. 检查蛇头是否碰到边缘或自身,如果是,游戏结束。
6. 检查蛇头是否碰到食物,如果是,添加一个新的节点到链表中,并在屏幕上绘制食物。
7. 重复步骤2到步骤6,直到游戏结束。
以下是一个简单的示例代码,可能需要根据实际情况进行修改和完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define WIDTH 60
#define HEIGHT 20
#define SNAKE_LEN 3
#define FOOD_CHAR '*'
typedef struct _snake_node {
int x;
int y;
struct _snake_node *next;
} SnakeNode;
typedef struct _snake {
int x;
int y;
int direction;
SnakeNode *body;
} Snake;
void init_snake(Snake *snake, int x, int y)
{
snake->x = x;
snake->y = y;
snake->direction = 'a';
snake->body = (SnakeNode *)malloc(sizeof(SnakeNode));
snake->body->x = x;
snake->body->y = y;
snake->body->next = NULL;
}
void add_node_to_snake(Snake *snake, int x, int y)
{
SnakeNode *new_node = (SnakeNode *)malloc(sizeof(SnakeNode));
new_node->x = x;
new_node->y = y;
new_node->next = snake->body;
snake->body = new_node;
}
void draw_snake(Snake *snake)
{
SnakeNode *node = snake->body;
while (node) {
printf("%c[%d;%dH#", 27, node->y, node->x);
node = node->next;
}
}
void move_snake(Snake *snake)
{
int dx = 0, dy = 0;
switch (snake->direction) {
case 'a': dx = -1; break;
case 'd': dx = 1; break;
case 'w': dy = -1; break;
case 's': dy = 1; break;
}
int x = snake->x + dx;
int y = snake->y + dy;
add_node_to_snake(snake, x, y);
snake->x = x;
snake->y = y;
SnakeNode *node = snake->body;
while (node->next->next) {
node = node->next;
}
free(node->next);
node->next = NULL;
}
void reset_console()
{
system("cls");
printf("%c[?25l", 27); // hide cursor
for (int i = 1; i <= WIDTH; i++) {
printf("%c[%d;%dH#", 27, 1, i);
printf("%c[%d;%dH#", 27, HEIGHT, i);
}
for (int i = 2; i <= HEIGHT - 1; i++) {
printf("%c[%d;%dH#", 27, i, 1);
printf("%c[%d;%dH#", 27, i, WIDTH);
}
}
void draw_food(int x, int y)
{
printf("%c[%d;%dH%c", 27, y, x, FOOD_CHAR);
}
int main()
{
srand(time(NULL));
Snake snake;
init_snake(&snake, WIDTH / 2, HEIGHT / 2);
draw_snake(&snake);
int food_x = rand() % (WIDTH - 2) + 2;
int food_y = rand() % (HEIGHT - 2) + 2;
draw_food(food_x, food_y);
while (1) {
if (_kbhit()) {
int ch = _getch();
if (ch == 0xE0 || ch == 0) {
ch = _getch();
switch (ch) {
case 75: snake.direction = 'a'; break;
case 77: snake.direction = 'd'; break;
case 72: snake.direction = 'w'; break;
case 80: snake.direction = 's'; break;
}
} else if (ch == 'q') {
break;
}
}
move_snake(&snake);
reset_console();
draw_snake(&snake);
draw_food(food_x, food_y);
if (snake.x == 1 || snake.x == WIDTH || snake.y == 1 || snake.y == HEIGHT) {
printf("%c[%d;%dHGame over!", 27, HEIGHT / 2, WIDTH / 2 - 5);
break;
} else if (snake.x == food_x && snake.y == food_y) {
food_x = rand() % (WIDTH - 2) + 2;
food_y = rand() % (HEIGHT - 2) + 2;
draw_food(food_x, food_y);
} else {
Sleep(100);
}
}
printf("%c[?25h", 27); // show cursor
return 0;
}
```
在 Windows 系统上编译运行该代码,即可看到贪吃蛇游戏的效果。注意,该代码仅供参考,可能存在一些问题和不足之处,需要根据实际情况进行调整和改进。
阅读全文