利用C语言写一个贪吃蛇的代码,每行解释说明,并把运用了说明数据结构类型说出来
时间: 2024-02-19 17:03:38 浏览: 33
以下是用 C 语言实现贪吃蛇的代码,每行都有注释说明,其中运用了数组这种数据结构类型:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 20 // 地图宽度
#define HEIGHT 20 // 地图高度
#define INIT_LENGTH 3 // 初始蛇身长度
int map[WIDTH][HEIGHT] = {0}; // 地图数组,用 0 表示空地,1 表示蛇身,2 表示食物
int snake[WIDTH*HEIGHT][2] = {0}; // 蛇身数组,存储每个方块的位置
int length = INIT_LENGTH; // 蛇身长度
int direction = 'd'; // 蛇头方向,初始向右
int score = 0; // 得分
int food_x = 0, food_y = 0; // 食物位置
// 生成随机食物
void generate_food() {
srand(time(NULL)); // 设置随机数种子
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (map[x][y] != 0); // 如果随机位置不是空地,则重新随机
food_x = x;
food_y = y;
map[x][y] = 2; // 标记食物位置为 2
}
// 初始化游戏
void init_game() {
// 初始化蛇头位置
int x = WIDTH / 2;
int y = HEIGHT / 2;
snake[0][0] = x;
snake[0][1] = y;
map[x][y] = 1; // 标记蛇头位置为 1
// 初始化蛇身位置
for (int i = 1; i < INIT_LENGTH; i++) {
snake[i][0] = x - i;
snake[i][1] = y;
map[x-i][y] = 1; // 标记蛇身位置为 1
}
generate_food(); // 生成随机食物
}
// 更新地图,绘制游戏界面
void update_map() {
system("cls"); // 清屏
// 绘制地图
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
if (map[i][j] == 0) {
printf(" ");
} else if (map[i][j] == 1) {
printf("*");
} else if (map[i][j] == 2) {
printf("#");
}
}
printf("\n");
}
printf("score: %d\n", score); // 显示得分
}
// 更新蛇的位置
void update_snake() {
int dx = 0, dy = 0; // 蛇头移动距离
switch (direction) {
case 'w':
dy = -1;
break;
case 's':
dy = 1;
break;
case 'a':
dx = -1;
break;
case 'd':
dx = 1;
break;
}
// 移动蛇头
int new_x = snake[0][0] + dx;
int new_y = snake[0][1] + dy;
// 判断是否撞墙或撞到蛇身
if (new_x < 0 || new_x >= WIDTH || new_y < 0 || new_y >= HEIGHT || map[new_x][new_y] == 1) {
printf("Game over!\n");
exit(0);
}
// 判断是否吃到食物
if (new_x == food_x && new_y == food_y) {
score += 10;
length++;
generate_food();
}
// 更新蛇身位置
for (int i = length-1; i > 0; i--) {
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
snake[0][0] += dx;
snake[0][1] += dy;
// 更新地图
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
}
}
}
for (int i = 0; i < length; i++) {
int x = snake[i][0];
int y = snake[i][1];
map[x][y] = 1;
}
map[food_x][food_y] = 2;
}
// 获取用户输入
void get_input() {
if (_kbhit()) { // 判断是否有按键按下
char ch = _getch(); // 获取按下的键
if (ch == 'w' || ch == 's' || ch == 'a' || ch == 'd') {
direction = ch;
}
}
}
int main() {
init_game(); // 初始化游戏
while (1) {
update_map(); // 更新地图,绘制游戏界面
update_snake(); // 更新蛇的位置
get_input(); // 获取用户输入
Sleep(100); // 等待 100 毫秒
}
return 0;
}
```
该代码运用了数组这种数据结构类型来存储地图、蛇身和食物位置等信息。其中,`map` 数组用于存储地图信息,`snake` 数组用于存储蛇身信息,`generate_food()` 函数用于生成随机食物,`update_map()` 函数用于更新地图并绘制游戏界面,`update_snake()` 函数用于更新蛇的位置,`get_input()` 函数用于获取用户输入。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)