用 c语言编写一个贪吃蛇游戏
时间: 2023-12-07 18:03:24 浏览: 72
好的,下面是用C语言编写的简单贪吃蛇游戏的代码。注释已经加得很详细了,希望能帮到你。
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define WIDTH 50 // 地图宽度
#define HEIGHT 20 // 地图高度
#define SNAKE_MAX_LENGTH 100 // 蛇的最大长度
// 蛇的结构体
struct Snake {
int x[SNAKE_MAX_LENGTH]; // 蛇身的x坐标
int y[SNAKE_MAX_LENGTH]; // 蛇身的y坐标
int length; // 蛇的长度
int direction; // 蛇的移动方向
int is_eating; // 蛇是否正在吃食物
};
// 食物的结构体
struct Food {
int x; // 食物的x坐标
int y; // 食物的y坐标
int is_eaten; // 食物是否被吃掉了
};
// 游戏状态的枚举类型
enum GameState {
GAME_RUNNING, // 游戏正在运行
GAME_OVER // 游戏结束
};
// 绘制地图
void draw_map(struct Snake snake, struct Food food) {
int i, j;
system("cls"); // 清屏
// 绘制上边界
for (i = 0; i < WIDTH + 2; i++) {
printf("-");
}
printf("\n");
// 绘制内部地图
for (i = 0; i < HEIGHT; i++) {
printf("|"); // 绘制左边界
for (j = 0; j < WIDTH; j++) {
if (i == snake.y[0] && j == snake.x[0]) { // 绘制蛇头
printf("O");
} else if (i == food.y && j == food.x) { // 绘制食物
printf("*");
} else {
int k;
int is_body = 0;
for (k = 1; k < snake.length; k++) {
if (i == snake.y[k] && j == snake.x[k]) {
printf("+"); // 绘制蛇身
is_body = 1;
break;
}
}
if (!is_body) {
printf(" "); // 绘制空格
}
}
}
printf("|"); // 绘制右边界
printf("\n");
}
// 绘制下边界
for (i = 0; i < WIDTH + 2; i++) {
printf("-");
}
printf("\n");
}
// 初始化蛇
void init_snake(struct Snake *snake) {
snake->x[0] = 10;
snake->y[0] = 10;
snake->length = 3;
snake->direction = 'a';
snake->is_eating = 0;
}
// 初始化食物
void init_food(struct Food *food) {
food->x = rand() % WIDTH;
food->y = rand() % HEIGHT;
food->is_eaten = 0;
}
// 更新蛇的位置
void update_snake(struct Snake *snake) {
int i;
int prev_x = snake->x[0];
int prev_y = snake->y[0];
int next_x, next_y;
// 根据方向更新蛇头的位置
switch (snake->direction) {
case 'a':
snake->x[0]--;
break;
case 'd':
snake->x[0]++;
break;
case 'w':
snake->y[0]--;
break;
case 's':
snake->y[0]++;
break;
}
// 更新蛇身的位置
for (i = 1; i < snake->length; i++) {
next_x = snake->x[i];
next_y = snake->y[i];
snake->x[i] = prev_x;
snake->y[i] = prev_y;
prev_x = next_x;
prev_y = next_y;
}
}
// 检查是否吃到食物
void check_eating(struct Snake *snake, struct Food *food) {
if (snake->x[0] == food->x && snake->y[0] == food->y) {
snake->is_eating = 1;
food->is_eaten = 1;
} else {
snake->is_eating = 0;
}
}
// 更新食物的位置
void update_food(struct Food *food) {
if (food->is_eaten) {
food->x = rand() % WIDTH;
food->y = rand() % HEIGHT;
food->is_eaten = 0;
}
}
// 检查游戏状态
int check_game_state(struct Snake snake) {
// 撞墙
if (snake.x[0] < 0 || snake.x[0] >= WIDTH || snake.y[0] < 0 || snake.y[0] >= HEIGHT) {
return GAME_OVER;
}
// 撞自己
int i;
for (i = 1; i < snake.length; i++) {
if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) {
return GAME_OVER;
}
}
return GAME_RUNNING;
}
int main() {
struct Snake snake;
struct Food food;
enum GameState game_state = GAME_RUNNING;
srand(time(NULL)); // 初始化随机数
init_snake(&snake);
init_food(&food);
while (game_state == GAME_RUNNING) {
if (kbhit()) { // 检测是否有按键按下
int key = getch();
if (key == 'a' || key == 'd' || key == 'w' || key == 's') {
snake.direction = key;
}
}
update_snake(&snake);
check_eating(&snake, &food);
update_food(&food);
game_state = check_game_state(snake);
draw_map(snake, food);
Sleep(100); // 休眠100毫秒,控制游戏速度
}
printf("Game Over!\n");
return 0;
}
```
阅读全文