C++贪吃蛇代码,附带详细注释
时间: 2023-08-12 17:32:50 浏览: 1535
下面是C++实现贪吃蛇游戏的代码,已附带详细注释。
```c++
#include <iostream>
#include <conio.h> // 用于 getch() 函数,可在控制台获取用户按下的键
#include <windows.h> // 用于 Sleep() 函数,可使程序暂停指定的时间
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100]; // 记录蛇身体的坐标
int nTail; // 蛇身长度
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); // 清空控制台
// 画顶部边框
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
// 画中间区域
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O"; // 画蛇头
else if (i == fruitY && j == fruitX)
cout << "F"; // 画水果
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o"; // 画蛇身
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
// 画底部边框
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
// 显示分数
cout << "Score:" << score << endl;
}
void Input()
{
if (_kbhit()) // 判断是否有输入
{
switch (_getch()) // 获取输入字符
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
// 移动蛇身
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
// 移动蛇头
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
// 判断是否吃到水果
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
// 判断是否碰到边界
if (x < 0 || x == width || y < 0 || y == height)
gameOver = true;
// 判断是否碰到自己的身体
for (int i = 0; i < nTail; i++)
{
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(50); // 暂停 50 毫秒
}
return 0;
}
```
该程序的运行方式为控制台窗口,玩家使用键盘上的方向键控制贪吃蛇的移动方向,每吃到一个水果加 10 分,并增加一节身体长度。如果贪吃蛇碰到边界或自己的身体,则游戏结束。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)