贪吃蛇codeblocks
时间: 2023-06-27 08:07:13 浏览: 172
以下是一个简单的贪吃蛇游戏的CodeBlocks代码:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver; // 游戏是否结束标志
const int width = 20; // 游戏界面宽度
const int height = 20; // 游戏界面高度
int x, y; // 蛇头坐标
int fruitX, fruitY; // 食物坐标
int 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 > width || x < 0 || y > height || y < 0) // 撞墙
gameOver = true;
for (int i = 0; i < nTail; i++) // 撞到蛇身
{
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
}
if (x == fruitX && y == fruitY) // 吃到食物
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(100); // 控制游戏速度
}
return 0;
}
```
这是一个简单的控制台版贪吃蛇游戏,代码中使用了Windows API库中的`Sleep()`函数来控制游戏速度。在CodeBlocks中,需要将项目属性中的“编译器设置”中的“调试选项”中的“-g”选项勾选去掉,否则程序运行时会弹出黑框。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)