帮我写一个c语言贪吃蛇的游戏
时间: 2023-08-20 08:36:51 浏览: 42
好的,下面是一个简单的C语言贪吃蛇游戏的代码,代码中使用了Windows API函数,需要在Windows环境下编译运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#define WIDTH 50 // 地图宽度
#define HEIGHT 20 // 地图高度
// 定义方向
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
// 定义游戏状态
enum GameState {
RUNNING,
OVER
};
// 定义蛇的结构体
struct Snake {
int x, y; // 蛇头坐标
int length; // 蛇的长度
enum Direction direction; // 蛇的移动方向
COORD body[WIDTH*HEIGHT]; // 蛇的身体,最多有WIDTH*HEIGHT个节点
};
// 定义食物的结构体
struct Food {
int x, y; // 食物坐标
};
// 定义游戏对象
struct Game {
struct Snake snake; // 蛇
struct Food food; // 食物
enum GameState state; // 游戏状态
};
// 初始化蛇
void init_snake(struct Snake *snake)
{
snake->x = WIDTH / 2;
snake->y = HEIGHT / 2;
snake->length = 3;
snake->direction = LEFT;
// 初始化蛇的身体
for (int i = 0; i < snake->length; i++) {
snake->body[i].X = snake->x + i;
snake->body[i].Y = snake->y;
}
}
// 初始化食物
void init_food(struct Food *food, struct Snake *snake)
{
srand(time(NULL));
do {
food->x = rand() % WIDTH;
food->y = rand() % HEIGHT;
} while (food->x == snake->x && food->y == snake->y); // 食物不能与蛇头重合
}
// 打印地图
void print_map(struct Game *game)
{
// 清屏
system("cls");
// 打印地图
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1) {
printf("#");
} else if (x == game->snake.x && y == game->snake.y) {
printf("@");
} else if (x == game->food.x && y == game->food.y) {
printf("$");
} else {
int flag = 0;
for (int i = 0; i < game->snake.length; i++) {
if (x == game->snake.body[i].X && y == game->snake.body[i].Y) {
printf("*");
flag = 1;
break;
}
}
if (!flag) {
printf(" ");
}
}
}
printf("\n");
}
// 打印得分
printf("Score: %d\n", game->snake.length - 3);
}
// 移动蛇
void move_snake(struct Game *game)
{
// 记录蛇尾的坐标
COORD tail = game->snake.body[game->snake.length - 1];
// 移动蛇身
for (int i = game->snake.length - 1; i > 0; i--) {
game->snake.body[i] = game->snake.body[i-1];
}
// 移动蛇头
switch (game->snake.direction) {
case UP:
game->snake.y--;
break;
case DOWN:
game->snake.y++;
break;
case LEFT:
game->snake.x--;
break;
case RIGHT:
game->snake.x++;
break;
}
game->snake.body[0].X = game->snake.x;
game->snake.body[0].Y = game->snake.y;
// 判断是否吃到食物
if (game->snake.x == game->food.x && game->snake.y == game->food.y) {
// 增加蛇的长度
game->snake.length++;
game->snake.body[game->snake.length - 1] = tail;
// 重新生成食物
init_food(&game->food, &game->snake);
}
// 判断是否撞到墙或者蛇身
if (game->snake.x == 0 || game->snake.x == WIDTH - 1
|| game->snake.y == 0 || game->snake.y == HEIGHT - 1) {
game->state = OVER;
}
for (int i = 1; i < game->snake.length; i++) {
if (game->snake.x == game->snake.body[i].X && game->snake.y == game->snake.body[i].Y) {
game->state = OVER;
break;
}
}
}
// 处理按键事件
void handle_input(struct Game *game)
{
if (_kbhit()) {
switch (_getch()) {
case 'w':
case 'W':
case 72:
if (game->snake.direction != DOWN)
game->snake.direction = UP;
break;
case 's':
case 'S':
case 80:
if (game->snake.direction != UP)
game->snake.direction = DOWN;
break;
case 'a':
case 'A':
case 75:
if (game->snake.direction != RIGHT)
game->snake.direction = LEFT;
break;
case 'd':
case 'D':
case 77:
if (game->snake.direction != LEFT)
game->snake.direction = RIGHT;
break;
case 27: // ESC键
game->state = OVER;
break;
}
}
}
int main()
{
struct Game game;
game.state = RUNNING;
// 初始化蛇和食物
init_snake(&game.snake);
init_food(&game.food, &game.snake);
while (game.state == RUNNING) {
print_map(&game);
handle_input(&game);
move_snake(&game);
Sleep(100); // 延时100毫秒
}
printf("Game Over!\n");
return 0;
}
```
这个游戏中,玩家需要控制蛇移动,吃到食物增加长度,撞到墙或者蛇身则游戏结束。玩家可以使用WASD或者方向键来控制蛇的移动方向,按ESC键可以退出游戏。
希望能对你有所帮助!