帮我写一个贪吃蛇的C代码
时间: 2023-08-20 09:17:21 浏览: 36
好的,以下是一个简单的贪吃蛇游戏的C代码示例,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define ROW 20
#define COL 40
int map[ROW][COL];
int snake[ROW * COL][2];
int food[2];
int snake_len = 2;
int direction = 1; // 1: up, 2: down, 3: left, 4: right
void init_map()
{
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (i == 0 || i == ROW - 1 || j == 0 || j == COL - 1) {
map[i][j] = -1; // 边界
} else {
map[i][j] = 0; // 空地
}
}
}
snake[0][0] = ROW / 2;
snake[0][1] = COL / 2;
snake[1][0] = ROW / 2 + 1;
snake[1][1] = COL / 2;
map[snake[0][0]][snake[0][1]] = 1; // 蛇头
map[snake[1][0]][snake[1][1]] = 2; // 蛇身
snake_len = 2;
food[0] = rand() % (ROW - 2) + 1;
food[1] = rand() % (COL - 2) + 1;
map[food[0]][food[1]] = -2; // 食物
}
void draw_map()
{
system("cls");
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (map[i][j] == -1) {
printf("#"); // 边界
} else if (map[i][j] == 0) {
printf(" "); // 空地
} else if (map[i][j] > 0) {
printf("*"); // 蛇
} else {
printf("@"); // 食物
}
}
printf("\n");
}
}
int move_snake()
{
int i;
int next[2];
next[0] = snake[0][0];
next[1] = snake[0][1];
switch (direction) {
case 1: // up
next[0]--;
break;
case 2: // down
next[0]++;
break;
case 3: // left
next[1]--;
break;
case 4: // right
next[1]++;
break;
}
if (map[next[0]][next[1]] == -1 || map[next[0]][next[1]] > 0) {
return 0; // 越界或碰到蛇身
}
if (map[next[0]][next[1]] == -2) {
snake_len++;
food[0] = rand() % (ROW - 2) + 1;
food[1] = rand() % (COL - 2) + 1;
map[food[0]][food[1]] = -2; // 重新生成食物
} else {
map[snake[snake_len - 1][0]][snake[snake_len - 1][1]] = 0; // 移动前清空蛇尾
}
for (i = snake_len - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = next[0];
snake[0][1] = next[1];
map[next[0]][next[1]] = 1;
for (i = 1; i < snake_len; i++) {
map[snake[i][0]][snake[i][1]] = i + 1;
}
return 1;
}
void update_direction(char key)
{
switch (key) {
case 'w':
case 'W':
if (direction != 2) {
direction = 1;
}
break;
case 's':
case 'S':
if (direction != 1) {
direction = 2;
}
break;
case 'a':
case 'A':
if (direction != 4) {
direction = 3;
}
break;
case 'd':
case 'D':
if (direction != 3) {
direction = 4;
}
break;
}
}
int main()
{
init_map();
while (1) {
if (_kbhit()) {
char key = _getch();
if (key == 27) {
break; // ESC
}
update_direction(key);
}
if (!move_snake()) {
break; // 游戏结束
}
draw_map();
Sleep(100); // 控制帧率
}
printf("Game over!\n");
system("pause");
return 0;
}
```
以上代码实现了一个简单的贪吃蛇游戏,通过控制台输出地图,利用键盘监听实现蛇的移动,使用随机数生成食物,实现了基本的游戏逻辑。注意这只是一个简单的示例,实际的贪吃蛇游戏逻辑和画面会更加复杂,需要更多的代码实现和优化。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)