【C++第一个Demo】---控制台RPG游戏3【登陆菜单树】
【登陆系统--树结构】
1 首先我这里设计,由一个基类MainMenu构建树结构,并实现控制台上菜单之间的切换和返回操作
#ifndef _UI_BASE_H_
#define _UI_BASE_H_ #include <string>
#include <vector>
#include"..//Marco.h"
using namespace std; //================ MainMenu 基类 ========================
class MainMenu
{
public:
MainMenu();
~MainMenu(); public:
// ==================== 外部接口 =======================
void PrintUI(char type, int X, int Y); //打印UI点
virtual void LinkChild(MainMenu*); //作为子目录链接
virtual void OutputFrame(); //打印框架
virtual void ChooseButton(); //按键选择
virtual void ChangingOver(){}; //切换界面 //==================== 内部函数 ======================
virtual void ButtonFunction(){}; //执行按钮功能
virtual void OutputButton(); //打印所有按键
virtual void RemoveButton(); //清除所有按键
virtual void RemoveButtonColor(); //移除按键底色
virtual void AppendButtonColor(); //添加按键底色 private:
CC_PROPERTY(char**, m_frame, Frame); //框架(界面)
CC_PROPERTY(int, m_row, Row); //frame高度
CC_PROPERTY(int, m_colu, Colu); //frame宽度
CC_PROPERTY(string*, m_button, Button); //自己的按键信息 //打印 childButton 的坐标位置
CC_PROPERTY(size_t, m_index, Index); //当前选中的按键序号(非角标)
CC_PROPERTY(int, m_buttonX, ButtonX);
CC_PROPERTY(int, m_buttonY, ButtonY); //tree结构
CC_PROPERTY(MainMenu*, m_father, Father); //上级目录
CC_PROPERTY(vector<MainMenu*>*, m_child, Child); //下级目录
}; #endif // _UI_BASE_H_
MainMenu.h
#include<iostream> #include"..//Tip.h"
#include "MainMenu.h" MainMenu::MainMenu()
{
//1 UI框架数据
char frame[][] = {
"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
"m!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m*0000000000000000000000000000000000000000000000000000000*m",
"m#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$m",
"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
};
m_row = ;
m_colu = ;
m_frame = GetCharMemory(m_row, m_colu);
for (int i = ; i < m_row; i++)
strcpy(m_frame[i], frame[i]); //Button数据
m_button = new string[]; //目录按键的坐标起点
m_index = ; //默认指向第一个按钮
m_buttonX = ; //第一个按钮的坐标x
m_buttonY = ; //第一个按钮的坐标y m_father = nullptr;
m_child = nullptr;
} MainMenu::~MainMenu()
{
if (m_frame != nullptr)
DeleteCharMemory(m_frame,m_row); if (m_button != nullptr)
{
delete[] m_button;
m_button = nullptr;
}
} // ===================== 外部接口 ===========================
//打印框架
void MainMenu::OutputFrame()
{
Color(CT_Grey, CT_Black);
char** pFrame = GetFrame();
//打印框架
for (int i = ; i < GetRow(); i++)
{
for (int j = ; j < GetColu() - ; j++)
{
PrintUI(pFrame[i][j], i, j);
}
} //游戏名字
char gameName[][] = {
"q000q00qq00qqqq0q0000qqq000q000q00qq00qqqq00qqqqqqq",
"q000q0q00q0q00q0q0000q00q00q000q0q00q0q00q000q0q0q0",
"q0q0q0q00q0qqqq0q0000q00q00q0q0q0qqqq0qqqq000q0q0q0",
"qq0qq0q00q0q0q00q0000q00q00qq0qq0q00q0q0q0000q0q0q0",
"q000q00qq00q00q0qqqq0qqq000q000q0q00q0q00q00qqqqqqq",
}; Color(CT_Blue, CT_Blue);
ColorType color = CT_Blue;
for (int i = ; i < ; i++)
{
if (i == ) color = CT_Red;
GotoXY( + , i + );
gameName[][i] == '' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << " ";
GotoXY( + , i + );
gameName[][i] == '' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << " ";
GotoXY( + , i + );
gameName[][i] == '' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << " ";
GotoXY( + , i + );
gameName[][i] == '' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << " ";
GotoXY( + , i + );
gameName[][i] == '' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << " ";
_sleep();
} Color(CT_Grey, CT_Blue);
} //按键选择
void MainMenu::ChooseButton()
{
while (true)
{
fflush(stdin);
char choose = getch(); //选择按键
PlaySound(TEXT(".\\Music\\选择.wav"), NULL, SND_ASYNC);
if (choose == -) choose = getch(); //屏蔽-32 //当按了方向键,切换按钮
if (choose == || choose == )
{
int dPos = ;
switch (choose)
{
case : if ( m_index > ) dPos = -; break;
case : if ( m_index < m_child->size() )dPos = +; break;
}
if (dPos != ) //发生了变动
{
this->RemoveButtonColor(); //清除底色打印
m_index += dPos;
this->AppendButtonColor(); //添加底色打印
}
} //按Enter确定按键进入子菜单
if (choose == )
{
//清除当前菜单按键、切换界面
this->RemoveButton();
this->ChangingOver(); //如果是最低菜单,执行功能函数
if (m_child->at(m_index - )->m_child == nullptr)
{
m_child->at(m_index - )->ButtonFunction();
return;
} //否则打印子菜单按键
m_child->at(m_index - )->OutputButton();
m_child->at(m_index - )->ChooseButton();
}
}
} //==================== 内部函数 ====================== //打印所有按键
void MainMenu::OutputButton()
{
if (m_child == nullptr)
return; Color(CT_Grey, CT_Black);
//遍历打印按钮
for (size_t i = ; i < m_child->size(); i++)
{
//获取按键信息
string* button = m_child->at(i)->m_button; GotoXY(m_buttonX + + * i, m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * i, m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * i, m_buttonY);
cout << button[];
} this->AppendButtonColor();
} //清除所有按键
void MainMenu::RemoveButton()
{
if (m_child == nullptr)
return; Color(CT_Grey, CT_Black);
//遍历打印在按钮位置打印空格,清除按键
for (size_t i = ; i < m_child->size(); i++)
{
GotoXY(m_buttonX + + * i, m_buttonY);
cout << " ";
GotoXY(m_buttonX + + * i, m_buttonY);
cout << " ";
GotoXY(m_buttonX + + * i, m_buttonY);
cout << " ";
}
} //移除按键底色
void MainMenu::RemoveButtonColor()
{
Color(CT_Grey, CT_Black);
string* button = m_child->at(m_index - )->m_button;
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[]; } //添加按键底色
void MainMenu::AppendButtonColor()
{
Color(CT_SkyBlue, CT_Red);
string* button = m_child->at(m_index - )->m_button;
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[];
GotoXY(m_buttonX + + * (m_index - ), m_buttonY);
cout << button[];
Color(CT_Grey, CT_Black);
} //将pMainMenu作为子目录链接到本目录
void MainMenu::LinkChild(MainMenu* pMainMenu)
{
pMainMenu->m_father = this; if (this->m_child == nullptr)
m_child = new vector < MainMenu* > ; m_child->push_back(pMainMenu);
} //打印UI点
void MainMenu::PrintUI(char type, int X, int Y)
{
GotoXY(X, Y);
switch (type)
{
case '': Color(CT_Grey, CT_Blue);
cout << " "; break;
case 'z': Color(CT_White, CT_Black);
cout << "…"; break;
case '': Color(CT_White, CT_Red);
cout << "★"; break;
case '': Color(CT_White, CT_Black);
cout << "☆"; break;
case '': Color(CT_White, CT_Red);
cout << "○"; break;
case '': Color(CT_White, CT_Black);
cout << "∷"; break;
case 'c': Color(CT_White, CT_Black);
cout << "∩"; break;
case 'e': Color(CT_Green, CT_White);
cout << "⊙"; break;
case 't': Color(CT_White, CT_Black);
cout << "♂"; break;
case 'r': Color(CT_White, CT_Black);
cout << "♀"; break;
case 'v': Color(CT_White, CT_Black);
cout << "∧"; break;
case 'b': Color(CT_White, CT_Black);
cout << "∨"; break;
case 'y': Color(CT_White, CT_Black);
cout << "∪"; break;
case 'u': Color(CT_White, CT_Pink);
cout << "⊥"; break;
case 'i': Color(CT_White, CT_Black);
cout << "⌒"; break;
case 'a': Color(CT_White, CT_Black);
cout << "≡"; break;
case 's': Color(CT_SkyBlue, CT_White);
cout << "≈"; break;
case 'd': Color(CT_White, CT_Black);
cout << "∽"; break;
case 'f': Color(CT_White, CT_Black);
cout << "卍"; break;
case 'm': Color(CT_Grey, CT_Black);
cout << "▓"; break;
case 'n': Color(CT_Green, CT_White);
cout << "〃"; break;
case '': Color(CT_White, CT_Black);
cout << "●"; break;
case '': Color(CT_White, CT_Black);
cout << "◎"; break;
case '': Color(CT_White, CT_Black);
cout << "◇"; break;
case '': Color(CT_White, CT_Black);
cout << "◆"; break;
case '': Color(CT_SkyBlue, CT_Black);
cout << "□"; break;
case 'q':
cout << "■"; break;
case 'o': Color(CT_White, CT_Black);
cout << "△"; break;
case 'p': Color(CT_White, CT_Black);
cout << "▲"; break;
case 'w': Color(CT_Grey, CT_Black);
cout << "※"; break;
case 'g': Color(CT_White, CT_Red);
cout << "→"; break;
case 'h': Color(CT_White, CT_Red);
cout << "←"; break;
case 'j': Color(CT_White, CT_Red);
cout << "↑"; break;
case 'k': Color(CT_White, CT_Red);
cout << "↓"; break;
case 'l': Color(CT_White, CT_Black);
cout << "〓"; break;
case 'x': Color(CT_White, CT_Black);
cout << "×"; break; //子弹、手雷
case '_': Color(CT_White, CT_Black); cout << "о"; break;
case '+': Color(CT_White, CT_Black); cout << "¤"; break;
case 'C': Color(CT_White, CT_Black); cout << "·"; break;
case 'D': Color(CT_White, CT_Black); cout << "━"; break; //制表符
case '!': Color(CT_Yellow, CT_Red);
cout << "┏"; break;
case '&': Color(CT_Yellow, CT_Red);
cout << "┳"; break;
case '@': Color(CT_Yellow, CT_Red);
cout << "┓"; break;
case '=': Color(CT_Yellow, CT_Red);
cout << "┫"; break;
case '$': Color(CT_Yellow, CT_Red);
cout << "┛"; break;
case ')': Color(CT_TBlue, CT_Red);
cout << "┻"; break;
case '#': Color(CT_Yellow, CT_Red);
cout << "┗"; break;
case '-': Color(CT_Yellow, CT_Red);
cout << "┣"; break;
case '(': Color(CT_Yellow, CT_Red);
cout << "╋"; break;
case '*': Color(CT_Yellow, CT_Red);
cout << "┃"; break;
case '%': Color(CT_Yellow, CT_Red);
cout << "━"; break;
//额外
case 'A': Color(CT_Yellow, CT_White);
cout << "╭"; break;
case 'B': Color(CT_Yellow, CT_White);
cout << "╮"; break;
}
}
MainMenu.cpp
下面是派生类,也就是登陆菜单中所有实际需要创建的选项
1级菜单:NewGame
#ifndef _NEW_GAME_H_
#define _NEW_GAME_H_ #include "MainMenu.h"
#include "..//Role//Hero.h"
#include "..//Map/Map_1.h"
#include "..//Map/Map_11.h"
#include "..//Map/Map_111.h"
#include "..//Map/Map_22.h"
#include "..//Map/Map_211.h" class NewGame : public MainMenu
{
public:
NewGame()
{
GetButton()[]="┏━━━━━━━━━┓";
GetButton()[]="┃开 始 新 游 戏┃";
GetButton()[]="┗━━━━━━━━━┛"; } //执行按钮功能
void ButtonFunction()
{
int x = GetButtonX();
int y = GetButtonY(); char name[] = {}; GotoXY(x - , y - );
cout << "┏━━━━━━━━━━━━━┓";
GotoXY(x - , y - );
cout << "┃ ┃";
GotoXY(x , y - );
cout << "┃输入名字:_______________ ┃";
GotoXY(x + , y - );
cout << "┃ ┃";
GotoXY(x + , y - );
cout << "┗━━━━━━━━━━━━━┛";
GotoXY(x , y + ); cin >> name; // 音乐 mp3
WCHAR mciCmd[] = TEXT("open .//music//background//gamestartup.mp3 alias background"); //.mp3格式的
mciSendString(mciCmd, NULL, , NULL);
mciSendString(TEXT("play background repeat"), NULL, , NULL); //====== 游戏初始化 ==========================
system("cls");
Role* pRole = new Hero(, RT_CHINA,,);
pRole->SetName(name);
{
Body* body = new SniperRifle();
pRole->GetBackpack()->PushBackBody(body);
}; Map* pMap = new Map_1();
pMap->OutputMap();
GameScene* gameScene = new GameScene(pRole, pMap);
pRole->InitShape(pMap->GetType().c_str());
pRole->GetGameOI()->OutputOI();
pRole->OutputRole(pMap);
gameScene->GameRuning(); //GameRunning结束时返回菜单
this->GetFather()->OutputButton();
this->GetFather()->ChooseButton();
} }; #endif // _NEW_GAME_H_
NewGame.h
#ifndef _OLD_GAME_H_
#define _OLD_GAME_H_ #include "MainMenu.h" class OldGame : public MainMenu
{
public:
OldGame()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃读 取 旧 游 戏┃";
GetButton()[] = "┗━━━━━━━━━┛";
}
}; #endif // _OLD_GAME_H_
OldGame.h
#ifndef _OLD_GAME1_H_
#define _OLD_GAME1_H_ #include "MainMenu.h" class OldGame1 : public MainMenu
{
public:
OldGame1()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃读 取 进 度 一 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
int x = GetButtonX();
int y = GetButtonY(); GotoXY(x, y);
cout << "进度1加载中...";
}
}; #endif // _OLD_GAME1_H_
OldGame1.h
#ifndef _OLD_GAME2_H_
#define _OLD_GAME2_H_ #include "MainMenu.h" class OldGame2 : public MainMenu
{
public:
OldGame2()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃读 取 进 度 二 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
int x = GetButtonX();
int y = GetButtonY(); GotoXY(x, y);
cout << "进度2加载中..."; }
}; #endif // _OLD_GAME2_H_
OldGame2.h
#ifndef _SET_GAME_H_
#define _SET_GAME_H_ #include "MainMenu.h" class SetGame : public MainMenu
{
public:
SetGame()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃ 游 戏 设 置 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} }; #endif // _SET_GAME_H_
SetGame.h
#ifndef _VIDEO_H_
#define _VIDEO_H_ #include "MainMenu.h" class Video : public MainMenu
{
public:
Video()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃ 视 频 设 置 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
int x = GetButtonX();
int y = GetButtonY(); GotoXY(x, y);
cout << "按方向键调整亮度"; }
}; #endif // _VIDEO_H_
Video.h
#ifndef _AUDIO_H_
#define _AUDIO_H_ #include "MainMenu.h" class Audio : public MainMenu
{
public:
Audio()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃ 音 频 设 置 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
int x = GetButtonX();
int y = GetButtonY(); GotoXY(x, y);
cout << "按方向键调整大小"; }
}; #endif // _AUDIO_H_
Audio.h
1级菜单:GameInfo
#ifndef _Game_Info_H_
#define _Game_Info_H_ #include "MainMenu.h" //============= 登录界面 =============== class GameInfo : public MainMenu
{
public:
GameInfo(); public://==== 外部接口 ======
virtual void ChangingOver(); //切换界面 virtual void ButtonFunction(); //执行按钮功能 }; #endif // _Game_Info_H_
GameInfo.h
#include"..//Marco.h"
#include"..//Tip.h" #include "GameInfo.h"
using namespace std; GameInfo::GameInfo()
{
GetButton()[]="┏━━━━━━━━━┓";
GetButton()[]="┃ 关 于 我 们 ┃";
GetButton()[]="┗━━━━━━━━━┛";
} //切换界面
void GameInfo::ChangingOver()
{ } //执行按钮功能
void GameInfo::ButtonFunction()
{
//游戏简介
string introduction[] = {};
introduction[] = " 自从前苏联时期美、苏两国实行“双方保证毁灭政策”之后,世界一直出于冷战与和平的状态......";
introduction[] = "爱因斯坦曾说“我不知道第三次世界大战用什么武器,但是第四次世界大战人类将只会用木棒和石头打”";
introduction[] = "也就是说以现在的科技文明爆发战争就等于自我毁灭。 ";
introduction[] = " 八十年代美国有个学者写过一本书《文明的冲突》,很出名,大致意思是未来世界冲突不再像冷战那";
introduction[] = "样,而是政治集团和意识形态的冲突,而最终归结到文明之间的冲突,世界主要分为基督教文明,佛教文";
introduction[] = "明和伊斯兰教文明。基督教文明和佛教文明与外界冲突很有限,更多表现是经济战争和领土纠纷,而且范";
introduction[] = "围和强度是可控的。只有伊斯兰教文明和外部世界有强烈的对抗性和排他性。未来伊斯兰教很可能会越来";
introduction[] = "越趋向保守和原教旨主义,瓦哈比之类的激进教派将逐步占据绝对主导,开明的穆斯林将逐步被排挤和屠";
introduction[] = "杀,当伊斯兰世界完成内部极端主义的整合和统一后,矛头将逐步对外,会在全世界掀起全面的圣战。新";
introduction[] = "的统一的阿拉伯帝国将会重新出现,它将在中东的边境将开展大肆的侵略,在全世界将进行全面的高强度";
introduction[] = "的有政府组织的自杀式恐怖袭击,试图用瓦哈比派统治全世界,到那个时候,全世界将被迫和伊斯兰文明";
introduction[] = "进行一场全面战争,甚至会相互爆发大规模种族屠杀,只有通过这种方式,文明的冲突才可最终得到解决";
introduction[] = " ";
introduction[] = " 历史历历在目...... ";
introduction[] = " 1979年,震惊世界的伊朗伊斯兰革命爆发,推翻了亲美的巴列维封建王朝。 ";
introduction[] = " 同样在1979年末,苏联出兵入侵阿富汗,美国开始资助阿富汗及巴基斯坦的圣战者抗苏武装。 ";
introduction[] = " 2003年,美国总统小布什以伊拉克萨达姆政权拥有“大规模杀伤性武器”为由,出兵侵占伊拉克。尽";
introduction[] = "管推翻了萨达姆的独裁统治,最终却没有找到任何“大规模杀伤性武器”,美国此举的副作用是大规模摧";
introduction[] = "毁了伊拉克的各种社会机构,客观上促成伊境内恐怖主义组织如雨后春笋般涌现...... ";
introduction[] = " 2006年10月,本.拉登副手扎卡维宣布成立“伊拉克伊斯兰国”...... ";
introduction[] = " 2011年,“阿拉伯之春”波及叙利亚,叙开始动乱,叙利亚从此陷入旷日持久的血腥宗派冲突。“伊";
introduction[] = "斯兰国”在血腥的叙利亚内战中“崛起”,他们在叙利亚东部地区建立了根据地,伊斯兰国袭击的范围不";
introduction[] = "断扩大,冲突次数不断增加,世界人民饱受极端组织的困扰...... ";
introduction[] = " 为了维护世界和平,2014年9月,美国组建了一个包括中、俄、英、法等54个国家和欧盟 、北约以及";
introduction[] = "阿盟等地区组织在内的国际联盟以打击IS;从此,第三次世界大战全面爆发…… ";
introduction[] = " 虽然我们的生活暂未受到恐怖袭击的影响,但为了世界的和平,我避免不了卷入这场战争。 "; Color(CT_White, CT_Black); //打印信息
system("cls");
GotoXY(, );
cout << "【 游 戏 信 息 】";
GotoXY(, );
cout << "●游戏名称:英文名 World War III";
GotoXY(, );
cout << " 中文名 第三次世界大战";
GotoXY(, );
cout << "●游戏类型: Adventure puzzle RPG";
GotoXY(, );
cout << "●学生姓名: xx";
GotoXY(, );
cout << "●版权所有:xxx";
GotoXY(, );
cout << "【 游 戏 背 景 】"; //printRow开始打印的行号 (开始行号会依次上升 -2)
//indexMin当前打印最小角标(最小角标为0,但当总行数超过6行时依次增加)
//indexMax当前打印最大角标(最大角标会依次递增 +1)
//size 表示可现实最大行数
int size = ;
int indexMin = ;
int dTime = ;
for (int printRow = , indexMax = ; indexMax < ; indexMax++)
{
//当总行速达到6时,最小角标开始递增 ,开始、结束打印的行号不再变化
if (indexMax > size)
indexMin = indexMax - size; for (int row = printRow, j = indexMin; j <= indexMax; j++, row += )
{
GotoXY(row, );
//最新的一行一个字一个字打印
if (j == indexMax)
{
Color(CT_White, CT_Red);
cout << introduction[j];
Color(CT_White, CT_Black);
break;
}
cout << introduction[j];
} if (indexMax < size)
printRow -= ; //停顿时间
_sleep(dTime); //按键修改速度
if (kbhit())
{
char ch = getch();
dTime = dTime / ;
}
}
char ch = getch();
//结束时返回菜单
this->GetFather()->OutputFrame();
this->GetFather()->OutputButton();
this->GetFather()->ChooseButton();
}
GameInfo.cpp
1级菜单:退出游戏
#ifndef _EXIT_GAME_H_
#define _EXIT_GAME_H_ #include "MainMenu.h" class ExitGame : public MainMenu
{
public:
ExitGame()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃太危险了还是离开吧┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
exit();
} }; #endif // _EXIT_GAME_H_
ExitGame.h
还有一个可以复用的菜单:返回上一层菜单
#ifndef _RETURN_H_
#define _RETURN_H_ #include "MainMenu.h" class Return : public MainMenu
{
public:
Return()
{
GetButton()[] = "┏━━━━━━━━━┓";
GetButton()[] = "┃ 返 回 上 一 级 ┃";
GetButton()[] = "┗━━━━━━━━━┛";
} //执行按钮功能
void ButtonFunction()
{
this->RemoveButton();
this->GetFather()->GetFather()->OutputButton();
this->GetFather()->GetFather()->ChooseButton();
} }; #endif // _RETURN_H_
Return.h
2、通过上面的的类可以创建出菜单,然后通过一个登陆管理类,来设置这些按钮的层级关系(树结构中的位置关系)
#ifndef _LAND_SURFACE_H_
#define _LAND_SURFACE_H_ #include "MainMenu.h"
#include "GameInfo.h"
#include "NewGame.h"
#include "ExitGame.h"
#include "Return.h"
#include "OldGame.h"
#include "OldGame1.h"
#include "OldGame2.h"
#include "SetGame.h"
#include "Audio.h"
#include "Video.h"
//=======用来管理登陆系统============
class LandSurface
{
public:
//初始化登陆界面(静态函数,不用创建对象)
static MainMenu* InitLandSurface()
{
MainMenu* pMainMenu = new MainMenu();
//主菜单
MainMenu* pNewGame = new NewGame();
pMainMenu->LinkChild(pNewGame);
MainMenu* pOldGame = new OldGame();
pMainMenu->LinkChild(pOldGame);
MainMenu* pSetGame = new SetGame();
pMainMenu->LinkChild(pSetGame);
MainMenu* pGameInfo = new GameInfo();
pMainMenu->LinkChild(pGameInfo);
MainMenu* pExitGame = new ExitGame();
pMainMenu->LinkChild(pExitGame);
//OldGame
MainMenu* pOldGame1 = new OldGame1();
pOldGame->LinkChild(pOldGame1);
MainMenu* pOldGame2 = new OldGame2();
pOldGame->LinkChild(pOldGame2);
MainMenu* pReturn1 = new Return();
pOldGame->LinkChild(pReturn1);
//SetGame
MainMenu* pAudio = new Audio();
pSetGame->LinkChild(pAudio);
MainMenu* pVideo = new Video();
pSetGame->LinkChild(pVideo);
MainMenu* pReturn2 = new Return();
pSetGame->LinkChild(pReturn2); return pMainMenu;
} }; #endif // _LAND_SURFACE_H_
LandSurface.h
总结:通过这段代码,可以实现控制台中制作出一个自动排成1列的菜单目录,可以给任意菜单添加和删除子菜单,实现了菜单与子菜单之间的进入和返回操作
截图:
【C++第一个Demo】---控制台RPG游戏3【登陆菜单树】的更多相关文章
- 【C++第一个Demo】---控制台RPG游戏4【角色系统】
[角色基类] #ifndef _ROLE_H_ #define _ROLE_H_ #include<list> #include<vector> #include " ...
- 【C++第一个Demo】---控制台RPG游戏2【通用宏、背包类】
[通用 ]--一些游戏中常用的宏.函数和枚举 #ifndef _MARCO_H_ #define _MARCO_H_ //------------------------常用系统库---------- ...
- 【C++第一个Demo】---控制台RPG游戏1【游戏简介】
经过1个月的制作和多次修改,终于有了基本雏形(此篇仅用于纪念历时3个多月C/C++学习所付出努力,也给和我一样苦恼于不能快速理解面向对象的同学们一点灵感) 在制作这个Demo过程中也受到了很多大 ...
- Magento添加一个下拉登陆菜单Create Magento Dropdown Login in a few minutes
Dropdown login forms are not a feature many online stores use, but in some cases they could be quite ...
- Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...
- 【android原生态RPG游戏框架源码】
转载请注明原创地址:http://www.cnblogs.com/zisou/p/android-RPG.html 这份源码是在今年6月份写的,当时公司有一个技术部们的学习讨论的讲座,然后我自己就写了 ...
- RPG游戏开发基础教程
RPG游戏开发基础教程 第一步 下载RPG Maker 开发工具包 1.RPG Maker 是什么? RPG Maker 是由Enterbrain公司推出的RPG制作工具. 中文译名为RPG制作大师. ...
- 如何制作一款HTML5 RPG游戏引擎——第五篇,人物&人物特效
上一次,我们实现了对话类,今天就来做一个游戏中必不可少的——人物类. 当然,你完全是可以自己写一个人物类,但是为了方便起见,还是决定把人物类封装到这个引擎里. 为了使这个类更有意义,我还给人物类加了几 ...
- 使用HTML5制作简单的RPG游戏
很久以前就想着做一个游戏,但什么都不会又不知道从哪里开始,胡乱找来一些书籍和资料结果太深奥看不懂,无奈只能放弃.这一弃就是十多年,倥偬半生,眼看垂垂老矣,还是没能有什么成果. 近年来游戏引擎越来越多, ...
随机推荐
- Ubuntu安装 docker
安装docker首先要需要一台宿主机, 我目前用VMvare下安装的Ubuntu16.04系统为宿主机,进行docker安装测试. ubuntu安装时选的中文环境,生成的sources.list里面的 ...
- ccf 201809-3 元素选择器
一.思路: 1.将结构化文档的每一行处理成一个节点(可定义一个结构体,成员包含标签tag.属性id.层级level.祖先所在行数father). 2.然后整个结构化文档就成了一个树形结构,可从任一节点 ...
- excel cannot access the file there are several possible reasons
original link Microsoft Office Excel cannot access the file ‘c:\file.xlsx’. There are several possib ...
- 【mySQL】left join、right join和join的区别
哈,好久没更新文章了,今天来说说关于mySQL那些年的小事.说到mySQL啊,用了挺久的了,但是有个问题一直在困扰着我,就是left join.join.right join和inner join等等 ...
- 解决text-align: justify;浏览器、安卓手机不兼容问题
1.兼容PC端浏览器 .h_text{ text-align:justify; text-justify:inter-ideograph;width:200px;} .span_hid{ displa ...
- SpringBoot-技术专区-异步编程
最近在实现一个聚合搜索的需求时,由于需要从五个索引中查询数据,然后再将搜索结果组合返回给前端app展现,显然这个地方不能再用同步的方式来操作了,如果有一个索引查询出现耗时较长,那么其余的请求都会排同步 ...
- mysql控制台的一些技巧,显示,输入换行,语法正则等
注释: 以/**注释内容**/ mysql> /**列出所有的数据库**/ show databases; +--------------------+ | Database | +------ ...
- 重读ORB_SLAM之LoopClosing线程难点
1. DetectLoop 这里有个ConsistenGroup概念,比较难懂.这里是最让人迷惑的地方.一旦vbConsistentGroup为真,其他帧的spCanditateGroup就进不来了. ...
- ES6新增语法和内置对象(let,const, Array/String/Set 扩展方法(解构赋值,箭头函数,剩余参数))
1.let ES6中新增的用于声明变量的关键字. let 声明的变量只在所处于的块级有效. 注意:使用 let 关键字声明的变量才具有块级作用域,var 关键字是不具备这个特点的. 1. 防止循环变量 ...
- 【记录】Mybatis-Generator 数据层代码生成器,自动生成dao类,mapper,pojo类
Mybatis-Generator 工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 官方文档地址:http://mybatis.org/generato ...