简单的贪吃蛇代码C语言
时间: 2023-10-15 08:16:25 浏览: 79
以下是一个简单的贪吃蛇代码,使用C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 40
#define HEIGHT 20
int snake[WIDTH * HEIGHT][2]; // 存储蛇身坐标
int food[2]; // 食物坐标
int snakeLength; // 蛇身长度
void setCursorPosition(int x, int y) // 设置控制台光标位置
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void drawSnake() // 绘制蛇身
{
setCursorPosition(snake[0][0], snake[0][1]);
printf("O");
for (int i = 1; i < snakeLength; i++)
{
setCursorPosition(snake[i][0], snake[i][1]);
printf("o");
}
}
void drawFood() // 绘制食物
{
setCursorPosition(food[0], food[1]);
printf("*");
}
void generateFood() // 生成食物
{
food[0] = rand() % WIDTH;
food[1] = rand() % HEIGHT;
}
void moveSnake(int direction) // 移动蛇身
{
int newHead[2];
newHead[0] = snake[0][0];
newHead[1] = snake[0][1];
switch (direction)
{
case 72: // 上
newHead[1]--;
break;
case 80: // 下
newHead[1]++;
break;
case 75: // 左
newHead[0]--;
break;
case 77: // 右
newHead[0]++;
break;
}
if (newHead[0] < 0 || newHead[0] >= WIDTH || newHead[1] < 0 || newHead[1] >= HEIGHT) // 判断是否撞墙
{
printf("\nGame over!\n");
exit(0);
}
for (int i = 1; i < snakeLength; i++) // 判断是否撞到自己
{
if (newHead[0] == snake[i][0] && newHead[1] == snake[i][1])
{
printf("\nGame over!\n");
exit(0);
}
}
if (newHead[0] == food[0] && newHead[1] == food[1]) // 判断是否吃到食物
{
snakeLength++;
generateFood();
}
else
{
setCursorPosition(snake[snakeLength - 1][0], snake[snakeLength - 1][1]);
printf(" ");
}
for (int i = snakeLength - 1; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = newHead[0];
snake[0][1] = newHead[1];
}
int main()
{
snake[0][0] = WIDTH / 2;
snake[0][1] = HEIGHT / 2;
snakeLength = 1;
generateFood();
while (1)
{
drawSnake();
drawFood();
Sleep(100); // 控制速度
if (_kbhit()) // 判断键盘输入
{
int direction = _getch();
if (direction == 224)
{
direction = _getch();
moveSnake(direction);
}
}
else
{
moveSnake(77); // 默认向右移动
}
}
return 0;
}
```
这个代码使用了控制台绘制界面,可以直接运行在Windows系统上。当蛇撞到墙或者撞到自己时,游戏结束。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)