用c++实现一个贪吃蛇
时间: 2023-08-24 10:16:24 浏览: 64
好的,以下是一个简单的C++实现贪吃蛇的示例代码,供参考:
```c++
#include <iostream>
#include <conio.h> // 用于控制台输入输出
#include <windows.h> // 用于控制台窗口大小和光标位置
using namespace std;
const int width = 20; // 游戏区域宽度
const int height = 20; // 游戏区域高度
int x, y; // 蛇头位置
int fruitX, fruitY; // 水果位置
int tailX[100], tailY[100]; // 蛇身位置
int nTail; // 蛇身长度
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 枚举方向
eDirection dir; // 当前方向
void Setup()
{
srand(time(NULL)); // 随机数种子
dir = STOP; // 初始方向为静止
x = width / 2; // 初始蛇头位置
y = height / 2;
fruitX = rand() % width; // 初始水果位置随机生成
fruitY = rand() % height;
nTail = 0; // 初始蛇身长度为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;
}
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':
dir = STOP;
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 >= width)
x = 0;
else if (x < 0)
x = width - 1;
if (y >= height)
y = 0;
else if (y < 0)
y = height - 1;
for (int i = 0; i < nTail; i++) // 判断蛇头是否碰到蛇身
{
if (tailX[i] == x && tailY[i] == y)
dir = STOP;
}
if (x == fruitX && y == fruitY) // 如果蛇头碰到水果,长度加一,重新生成水果
{
nTail++;
fruitX = rand() % width;
fruitY = rand() % height;
}
}
int main()
{
Setup();
while (dir != STOP)
{
Draw();
Input();
Logic();
Sleep(50); // 控制帧率
}
return 0;
}
```
这段代码中用到了控制台窗口输出、随机数生成、键盘输入、帧率控制等技巧,需要了解一些C++基础知识才能看懂。如果您对这些知识还不熟悉,建议先学习一下C++的基础语法和控制台编程相关的知识。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)