用c语言编写游戏:贪吃蛇: 要求: 1、设定游戏显示界面及边界;用键盘按键移动贪吃蛇; 2、初始状态下,用‘*’表示蛇头,用‘。’表示食物,食物在游戏界面区域内随机出现,必须是蛇头吞食食物后才改变食物位置,每次出现1个 3、根据规则,只能蛇头触碰食物,蛇头触碰食物后,身体变化,用’。’表示,其余郎分触碰食物,游戏结束; 4、蛇头触碰身体,游戏结束。 5、显示吞食食物个数,表示游戏分值. 代码给我
时间: 2024-03-10 14:48:59 浏览: 82
好的,以下是用 C 语言编写的贪吃蛇游戏代码,实现了上述要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
// 游戏界面大小
#define WIDTH 30
#define HEIGHT 20
// 蛇的最大长度
#define MAX_LENGTH (WIDTH * HEIGHT)
// 定义方向枚举类型
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
// 定义节点结构体
struct Node {
int x;
int y;
};
// 定义蛇结构体
struct Snake {
struct Node *body;
int length;
enum Direction direction;
};
// 定义食物结构体
struct Food {
int x;
int y;
};
// 定义游戏状态结构体
struct GameState {
int score;
int food_eaten;
struct Snake snake;
struct Food food;
};
// 定义函数,用于初始化游戏状态
void init_game(struct GameState *state) {
// 初始化随机种子
srand((unsigned int)time(NULL));
// 初始化蛇的位置和方向
state->snake.body = (struct Node *)malloc(sizeof(struct Node) * MAX_LENGTH);
state->snake.body[0].x = WIDTH / 2;
state->snake.body[0].y = HEIGHT / 2;
state->snake.length = 1;
state->snake.direction = RIGHT;
// 初始化食物的位置
state->food.x = rand() % WIDTH;
state->food.y = rand() % HEIGHT;
// 初始化分数和食物数量
state->score = 0;
state->food_eaten = 0;
}
// 定义函数,用于绘制游戏界面
void draw_game(struct GameState *state) {
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 == state->snake.body[0].x && y == state->snake.body[0].y) {
printf("*");
} else if (x == state->food.x && y == state->food.y) {
printf(".");
} else {
int flag = 0;
for (int i = 1; i < state->snake.length; i++) {
if (x == state->snake.body[i].x && y == state->snake.body[i].y) {
printf(".");
flag = 1;
break;
}
}
if (!flag) {
printf(" ");
}
}
}
printf("\n");
}
// 显示分数和食物数量
printf("Score: %d, Food: %d\n", state->score, state->food_eaten);
}
// 定义函数,用于更新游戏状态
void update_game(struct GameState *state) {
// 处理按键输入
if (_kbhit()) {
switch (_getch()) {
case 'w':
case 'W':
if (state->snake.direction != DOWN) {
state->snake.direction = UP;
}
break;
case 's':
case 'S':
if (state->snake.direction != UP) {
state->snake.direction = DOWN;
}
break;
case 'a':
case 'A':
if (state->snake.direction != RIGHT) {
state->snake.direction = LEFT;
}
break;
case 'd':
case 'D':
if (state->snake.direction != LEFT) {
state->snake.direction = RIGHT;
}
break;
default:
break;
}
}
// 移动蛇的位置
for (int i = state->snake.length - 1; i > 0; i--) {
state->snake.body[i].x = state->snake.body[i - 1].x;
state->snake.body[i].y = state->snake.body[i - 1].y;
}
switch (state->snake.direction) {
case UP:
state->snake.body[0].y--;
break;
case DOWN:
state->snake.body[0].y++;
break;
case LEFT:
state->snake.body[0].x--;
break;
case RIGHT:
state->snake.body[0].x++;
break;
default:
break;
}
// 判断是否吃到食物
if (state->snake.body[0].x == state->food.x && state->snake.body[0].y == state->food.y) {
state->food.x = rand() % WIDTH;
state->food.y = rand() % HEIGHT;
state->snake.length++;
state->score += 10;
state->food_eaten++;
}
// 判断是否游戏结束
if (state->snake.body[0].x == 0 || state->snake.body[0].x == WIDTH - 1 ||
state->snake.body[0].y == 0 || state->snake.body[0].y == HEIGHT - 1) {
printf("Game Over!\n");
exit(0);
}
for (int i = 1; i < state->snake.length; i++) {
if (state->snake.body[0].x == state->snake.body[i].x &&
state->snake.body[0].y == state->snake.body[i].y) {
printf("Game Over!\n");
exit(0);
}
}
}
// 定义主函数
int main() {
struct GameState state;
init_game(&state);
while (1) {
draw_game(&state);
update_game(&state);
Sleep(100); // 控制帧率
}
return 0;
}
```
这个代码实现了一个简单的贪吃蛇游戏,游戏界面大小为 30x20,使用 W、A、S、D 键控制蛇的移动方向,吃到食物后蛇的长度加 1,当蛇碰到边界或者自己时游戏结束。
阅读全文