snake算法c语言
时间: 2023-11-25 22:50:44 浏览: 130
以下是一个简单的贪吃蛇游戏的C语言实现,包含了基本的游戏逻辑和控制台界面的绘制。代码中使用了结构体来表示蛇和食物,使用了链表来存储蛇的身体。具体实现细节可以参考代码注释。
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define WIDTH 40 // 游戏界面宽度
#define HEIGHT 20 // 游戏界面高度
// 定义方向常量
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
// 定义蛇的结构体
typedef struct Snake {
int x, y; // 蛇头坐标
struct Snake *next; // 指向下一个节点的指针
} Snake;
// 定义食物的结构体
typedef struct Food {
int x, y; // 食物坐标
} Food;
// 初始化蛇
Snake *initSnake() {
Snake *head = (Snake *)malloc(sizeof(Snake));
head->x = WIDTH / 2;
head->y = HEIGHT / 2;
head->next = NULL;
return head;
}
// 在控制台上绘制游戏界面
void drawGame(Snake *snake, Food *food) {
system("cls"); // 清屏
// 绘制上边界
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++) {
if (i == snake->y && j == snake->x) { // 绘制蛇头
printf("O");
} else if (food->x == j && food->y == i) { // 绘制食物
printf("*");
} else { // 绘制空格
int flag = 0;
Snake *p = snake->next;
while (p != NULL) {
if (p->x == j && p->y == i) {
printf("o"); // 绘制蛇身
flag = 1;
break;
}
p = p->next;
}
if (!flag) {
printf(" ");
}
}
}
printf("#\n");
}
// 绘制下边界
for (int i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
}
// 生成食物
void generateFood(Food *food, Snake *snake) {
srand((unsigned int)time(NULL)); // 设置随机数种子
while (1) {
int flag = 0;
food->x = rand() % WIDTH;
food->y = rand() % HEIGHT;
Snake *p = snake;
while (p != NULL) {
if (p->x == food->x && p->y == food->y) {
flag = 1;
break;
}
p = p->next;
}
if (!flag) {
break;
}
}
}
// 判断蛇是否吃到食物
int isEatFood(Snake *snake, Food *food) {
if (snake->x == food->x && snake->y == food->y) {
return 1;
}
return 0;
}
// 判断蛇是否撞墙或撞到自己
int isGameOver(Snake *snake) {
if (snake->x < 0 || snake->x >= WIDTH || snake->y < 0 || snake->y >= HEIGHT) { // 撞墙
return 1;
}
Snake *p = snake->next;
while (p != NULL) { // 撞到自己
if (p->x == snake->x && p->y == snake->y) {
return 1;
}
p = p->next;
}
return 0;
}
// 更新蛇的位置
void updateSnake(Snake *snake, int direction, int isEat) {
Snake *newHead = (Snake *)malloc(sizeof(Snake));
newHead->x = snake->x;
newHead->y = snake->y;
if (direction == UP) {
newHead->y--;
} else if (direction == DOWN) {
newHead->y++;
} else if (direction == LEFT) {
newHead->x--;
} else if (direction == RIGHT) {
newHead->x++;
}
newHead->next = snake;
snake = newHead;
if (!isEat) { // 如果没有吃到食物,则删除蛇尾
Snake *p = snake;
while (p->next->next != NULL) {
p = p->next;
}
free(p->next);
p->next = NULL;
}
}
int main() {
Snake *snake = initSnake(); // 初始化蛇
Food *food = (Food *)malloc(sizeof(Food)); // 初始化食物
generateFood(food, snake); // 生成食物
int direction = RIGHT; // 初始方向向右
while (1) {
drawGame(snake, food); // 绘制游戏界面
if (_kbhit()) { // 判断是否有键盘输入
char ch = _getch();
if (ch == 'w' || ch == 'W' || ch == 72) { // 上
if (direction != DOWN) {
direction = UP;
}
} else if (ch == 's' || ch == 'S' || ch == 80) { // 下
if (direction != UP) {
direction = DOWN;
}
} else if (ch == 'a' || ch == 'A' || ch == 75) { // 左
if (direction != RIGHT) {
direction = LEFT;
}
} else if (ch == 'd' || ch == 'D' || ch == 77) { // 右
if (direction != LEFT) {
direction = RIGHT;
}
}
}
int isEat = isEatFood(snake, food); // 判断是否吃到食物
if (isEat) {
generateFood(food, snake); // 生成新的食物
}
updateSnake(snake, direction, isEat); // 更新蛇的位置
if (isGameOver(snake)) { // 判断游戏是否结束
printf("Game Over!\n");
break;
}
Sleep(100); // 控制游戏速度
}
return 0;
}
```
阅读全文