介绍
在本文中,我们将学习如何使用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;
bool gameOver; // 游戏结束标志
接下来,我们为游戏创建各种函数,包括初始化游戏设置(Setup())、绘制游戏场景(Draw())、处理玩家输入(Input())、更新游戏逻辑(Logic())以及主菜单(MainMenu())和游戏结束菜单(GameOverMenu())。
// 初始化游戏设置
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
nTail = 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':
if (dir != RIGHT)
dir = LEFT;
break;
case 'd':
if (dir != LEFT)
dir = RIGHT;
break;
case 'w':
if (dir != DOWN)
dir = UP;
break;
case 's':
if (dir != UP)
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;
}
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)
gameOver = true;
if (x == fruitX && y == fruitY)
{
nTail++;
fruitX = rand() % width;
fruitY = rand() % height;
}
}
// 主菜单
void MainMenu()
{
system("cls");
cout << " 贪吃蛇" << endl;
cout << "按任意键开始游戏..." << endl;
_getch();
}
这些函数分别负责不同的任务,例如,Setup()函数用于初始化游戏的各种参数,Draw()函数用于绘制游戏场景,Input()函数用于处理玩家的键盘输入,Logic()函数用于更新游戏的逻辑状态,而MainMenu()和GameOverMenu()函数则分别用于显示主菜单和游戏结束菜单。
最后,在main()函数中,我们调用这些函数来运行游戏。首先,我们调用MainMenu()函数显示主菜单,然后调用Setup()函数初始化游戏。接着,我们进入一个循环,在循环中,我们调用Draw()函数绘制游戏场景,然后调用Input()函数处理玩家的输入,再调用Logic()函数更新游戏的状态。如果游戏结束,我们会调用GameOverMenu()函数显示游戏结束菜单并询问玩家是否要重新开始游戏。
void GameOverMenu()
{
cout << "Game Over!" << endl;
cout << "是否再玩一局(y/n)" << endl;
char choice;
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
gameOver = false;
nTail = 0;
Setup();
}
else
{
gameOver = true;
}
}
// 主程序
int main()
{
MainMenu();
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
if (gameOver)
{
GameOverMenu();
}
Sleep(100); // 控制游戏速度
}
return 0;
}
完整代码
#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;
bool gameOver; // 游戏结束标志
// 初始化游戏设置
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
nTail = 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':
if (dir != RIGHT)
dir = LEFT;
break;
case 'd':
if (dir != LEFT)
dir = RIGHT;
break;
case 'w':
if (dir != DOWN)
dir = UP;
break;
case 's':
if (dir != UP)
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;
}
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)
gameOver = true;
if (x == fruitX && y == fruitY)
{
nTail++;
fruitX = rand() % width;
fruitY = rand() % height;
}
}
// 主菜单
void MainMenu()
{
system("cls");
cout << " 贪吃蛇" << endl;
cout << "按任意键开始游戏..." << endl;
_getch();
}
// 游戏结束菜单
void GameOverMenu()
{
cout << "Game Over!" << endl;
cout << "是否再玩一局(y/n)" << endl;
char choice;
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
gameOver = false;
nTail = 0;
Setup();
}
else
{
gameOver = true;
}
}
// 主程序
int main()
{
MainMenu();
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
if (gameOver)
{
GameOverMenu();
}
Sleep(100); // 控制游戏速度
}
return 0;
}
以上就是使用C++编写的简单贪吃蛇游戏的全部内容。通过这个例子,我们可以看到如何使用C++来实现一个有趣的游戏。希望这篇文章对你有所帮助!
请注意,这只是一个简化的示例,你可以根据需要添加更多的功能,比如增加游戏难度、添加得分系统等。