提示:阅读本文需要一定的C++基础,此思路只用于实现贪吃蛇的基本功能,没有图形化,没有使用EasyX库。(尊重原创,转载本文请指明出处)
此思路对于锦城的学生很适用,这就是一学期的一个小项目。
(如果对你有帮助的话,希望能点个赞或者一键三连)
前言
提示:全部代码请点击–>https://github.com/qing-qing-mei/Snake
了解代码框架戳这儿:https://blog.csdn.net/weixin_43929310/article/details/112667309
前一篇游戏控制类:https://blog.csdn.net/weixin_43929310/article/details/112878920
如何代码有任何不清楚或者疑问的地方欢迎在评论区留言或者私信我,我看到一定会回复。(虽然不一定及时)
一、代码
#pragma once
#include"CMap.h"
#include"CGame.h"
class CMenu
{
public:
CMenu();
~CMenu();
bool showMenu();
void startGame();
void quitGame();
bool gameHelp();
static void loadGame();
public:
CMap* m_iMap;
CGame* m_iGame;
};
#include<iostream>
#include<windows.h>
//#include<conio.h>
#include "CMenu.h"
#include"CUnit.h"
using namespace std;
CMenu::CMenu()
{
m_iMap = new CMap;
m_iGame = new CGame;
}
CMenu::~CMenu()
{
delete m_iMap;
delete m_iGame;
}
void CMenu::loadGame()
{
system("color f5\n");
Cunit::gotoxy(CMap::KWIDTH / 2, CMap::KHEIGHT / 2);
cout << "正在加载游戏";
for (int i = 0; i < 6; i++)
{
Sleep(400);
cout << " .";
}
Sleep(500);
}
bool CMenu::showMenu()
{
system("cls");
system("color f0\n");
bool flag = false;
m_iMap->drawGameArea();
Cunit::gotoxy(CMap::KWIDTH / 2, CMap::KHEIGHT / 3);
cout << "贪吃蛇";
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2);
cout << "2. 退出游戏";
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2 - 2);
cout << "1. 开始游戏";
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2 + 2);
cout << "3. 游戏帮助";
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2 + 4);
cout << "请输入你的选择:";
char your_choose;
cin >> your_choose;
while (your_choose != '1' && your_choose != '2' && your_choose != '3')
{
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2 + 4);
cout << "输入无效,请重新输入:";
//if (_kbhit()) your_choose = _getch();
cin >> your_choose;
}
if (your_choose == '1')
{
startGame();
return false;
}
else if (your_choose == '2')
{
quitGame();
return true;
}
else return gameHelp();
}
void CMenu::startGame()
{
system("cls");
m_iGame->run();
}
void CMenu::quitGame()
{
system("cls");
m_iMap->drawGameArea();
Cunit::gotoxy(CMap::KWIDTH / 2 - 10, CMap::KHEIGHT / 2);
cout << "游戏结束... ";
}
bool CMenu::gameHelp()
{
system("cls");
m_iMap->drawGameArea();
Cunit::gotoxy(0, CMap::KHEIGHT / 3 - 2);
cout << "\t\t游戏说明" << endl << endl;
cout << "\t\t1. 按w,s,a,d控制蛇的上下左右" << endl << endl;
cout << "\t\t2. 按方向键以外的任意键暂停,再按任意键继续游戏" << endl << endl;
cout << "\t\t3. 一个食物10分,每30提升一个等级" << endl << endl;
cout << "\t\t4.游戏过程中,可按q键退出游戏" << endl << endl;
cout << "\t\t5. 按q返回上一级,按e直接退出游戏"<<endl<<endl;
char ch;
Cunit::gotoxy(CMap::KWIDTH / 3, CMap::KHEIGHT - 4);
cout << "请输入q or e:";
cin >> ch;
while (ch != 'q' && ch != 'e')
{
Cunit::gotoxy(CMap::KWIDTH / 3, CMap::KHEIGHT - 4);
cout << "请输入q or e:";
cin >> ch;
}
if (ch == 'q')
{
showMenu();
return false;
}
else
{
quitGame();
return true;
}
}
main函数:
#include"CUnit.h"
#include"CMenu.h"
#include<windows.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
int main()
{
CMenu::loadGame();
PlaySound(L"Lemon.wav",
NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); //加入背景音乐
while (true)
{
CMenu m_iMenu;
if(m_iMenu.showMenu())
break;
cout << "再来一局?(y/n): ";
char ch;
cin >> ch;
if (ch == 'y');
else break;
}
return 0;
}
CMenu类中实现的思路很简单,在此就不做多赘述了。