原来还有这么通俗易懂的贪吃蛇写法之CMenu类和主函数(五)

这是一个使用C++编写的贪吃蛇游戏代码实现,不依赖图形库如EasyX,适合初学者进行项目实践。文章提供了游戏的主菜单、开始、退出和帮助功能,并附带了详细的代码实现。游戏包括加载、主菜单选择、游戏控制和结束等流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:阅读本文需要一定的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类中实现的思路很简单,在此就不做多赘述了。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值