【C++第一个Demo】---控制台RPG游戏3【登陆菜单树】

【登陆系统--树结构】

1 首先我这里设计,由一个基类MainMenu构建树结构,并实现控制台上菜单之间的切换和返回操作

 1 #ifndef _UI_BASE_H_
 2 #define _UI_BASE_H_
 3 
 4 #include <string>
 5 #include <vector>
 6 #include"..//Marco.h"
 7 using namespace std;
 8 
 9 //================  MainMenu 基类 ========================
10 class MainMenu
11 {
12 public:
13     MainMenu();
14     ~MainMenu();
15 
16 public:
17     // ==================== 外部接口 =======================
18     void PrintUI(char type, int X, int Y);    //打印UI点
19     virtual void LinkChild(MainMenu*);        //作为子目录链接
20     virtual void OutputFrame();                //打印框架
21     virtual void ChooseButton();            //按键选择
22     virtual void ChangingOver(){};            //切换界面
23 
24     //====================  内部函数  ======================
25     virtual void ButtonFunction(){};        //执行按钮功能
26     virtual void OutputButton();            //打印所有按键
27     virtual void RemoveButton();            //清除所有按键
28     virtual void RemoveButtonColor();        //移除按键底色
29     virtual void AppendButtonColor();        //添加按键底色
30 
31 private:
32     CC_PROPERTY(char**, m_frame, Frame);    //框架(界面)
33     CC_PROPERTY(int, m_row, Row);            //frame高度
34     CC_PROPERTY(int, m_colu, Colu);            //frame宽度
35     CC_PROPERTY(string*, m_button, Button); //自己的按键信息
36 
37     //打印 childButton 的坐标位置
38     CC_PROPERTY(size_t, m_index, Index);    //当前选中的按键序号(非角标)
39     CC_PROPERTY(int, m_buttonX, ButtonX);    
40     CC_PROPERTY(int, m_buttonY, ButtonY);
41 
42     //tree结构
43     CC_PROPERTY(MainMenu*, m_father, Father);            //上级目录
44     CC_PROPERTY(vector<MainMenu*>*, m_child, Child);    //下级目录
45 };
46 
47 #endif // _UI_BASE_H_
MainMenu.h
  1 #include<iostream>
  2 
  3 #include"..//Tip.h"
  4 #include "MainMenu.h"
  5 
  6 
  7 MainMenu::MainMenu()
  8 {
  9     //1 UI框架数据
 10     char frame[40][60] = {
 11         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
 12         "m!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@m",
 13         "m*0000000000000000000000000000000000000000000000000000000*m",
 14         "m*0000000000000000000000000000000000000000000000000000000*m",
 15         "m*0000000000000000000000000000000000000000000000000000000*m",
 16         "m*0000000000000000000000000000000000000000000000000000000*m",
 17         "m*0000000000000000000000000000000000000000000000000000000*m",
 18         "m*0000000000000000000000000000000000000000000000000000000*m",
 19         "m*0000000000000000000000000000000000000000000000000000000*m",
 20         "m*0000000000000000000000000000000000000000000000000000000*m",
 21         "m*0000000000000000000000000000000000000000000000000000000*m",
 22         "m*0000000000000000000000000000000000000000000000000000000*m",
 23         "m*0000000000000000000000000000000000000000000000000000000*m",
 24         "m*0000000000000000000000000000000000000000000000000000000*m",
 25         "m*0000000000000000000000000000000000000000000000000000000*m",
 26         "m*0000000000000000000000000000000000000000000000000000000*m",
 27         "m*0000000000000000000000000000000000000000000000000000000*m",
 28         "m*0000000000000000000000000000000000000000000000000000000*m",
 29         "m*0000000000000000000000000000000000000000000000000000000*m",
 30         "m*0000000000000000000000000000000000000000000000000000000*m",
 31         "m*0000000000000000000000000000000000000000000000000000000*m",
 32         "m*0000000000000000000000000000000000000000000000000000000*m",
 33         "m*0000000000000000000000000000000000000000000000000000000*m",
 34         "m*0000000000000000000000000000000000000000000000000000000*m",
 35         "m*0000000000000000000000000000000000000000000000000000000*m",
 36         "m*0000000000000000000000000000000000000000000000000000000*m",
 37         "m*0000000000000000000000000000000000000000000000000000000*m",
 38         "m*0000000000000000000000000000000000000000000000000000000*m",
 39         "m*0000000000000000000000000000000000000000000000000000000*m",
 40         "m*0000000000000000000000000000000000000000000000000000000*m",
 41         "m*0000000000000000000000000000000000000000000000000000000*m",
 42         "m*0000000000000000000000000000000000000000000000000000000*m",
 43         "m*0000000000000000000000000000000000000000000000000000000*m",
 44         "m*0000000000000000000000000000000000000000000000000000000*m",
 45         "m*0000000000000000000000000000000000000000000000000000000*m",
 46         "m*0000000000000000000000000000000000000000000000000000000*m",
 47         "m*0000000000000000000000000000000000000000000000000000000*m",
 48         "m*0000000000000000000000000000000000000000000000000000000*m",
 49         "m#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$m",
 50         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
 51     };
 52     m_row = 40;
 53     m_colu = 60;
 54     m_frame = GetCharMemory(m_row, m_colu);
 55     for (int i = 0; i < m_row; i++)
 56         strcpy(m_frame[i], frame[i]);
 57 
 58     //Button数据
 59     m_button = new string[3];
 60 
 61     //目录按键的坐标起点
 62     m_index = 1;    //默认指向第一个按钮
 63     m_buttonX = 17; //第一个按钮的坐标x
 64     m_buttonY = 24; //第一个按钮的坐标y
 65 
 66     m_father = nullptr;
 67     m_child = nullptr;
 68 }
 69 
 70 MainMenu::~MainMenu()
 71 {
 72     if (m_frame != nullptr)
 73         DeleteCharMemory(m_frame,m_row);
 74 
 75     if (m_button != nullptr)
 76     {
 77         delete[] m_button;
 78         m_button = nullptr;
 79     }
 80 }
 81 
 82 // ===================== 外部接口 ===========================
 83 //打印框架
 84 void MainMenu::OutputFrame()
 85 {
 86     Color(CT_Grey, CT_Black);
 87     char** pFrame = GetFrame();
 88     //打印框架
 89     for (int i = 0; i < GetRow(); i++)
 90     {
 91         for (int j = 0; j < GetColu() - 1; j++)
 92         {
 93             PrintUI(pFrame[i][j], i, j);
 94         }
 95     }
 96 
 97     //游戏名字
 98     char gameName[5][52] = {
 99         "q000q00qq00qqqq0q0000qqq000q000q00qq00qqqq00qqqqqqq",
100         "q000q0q00q0q00q0q0000q00q00q000q0q00q0q00q000q0q0q0",
101         "q0q0q0q00q0qqqq0q0000q00q00q0q0q0qqqq0qqqq000q0q0q0",
102         "qq0qq0q00q0q0q00q0000q00q00qq0qq0q00q0q0q0000q0q0q0",
103         "q000q00qq00q00q0qqqq0qqq000q000q0q00q0q00q00qqqqqqq",
104     };
105 
106     Color(CT_Blue, CT_Blue);
107     ColorType color = CT_Blue;
108     for (int i = 0; i < 51; i++)
109     {
110         if (i == 44) color = CT_Red;
111         GotoXY(0 + 5, i + 4); 
112         gameName[0][i] == '0' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << "  ";
113         GotoXY(1 + 5, i + 4); 
114         gameName[1][i] == '0' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << "  ";
115         GotoXY(2 + 5, i + 4); 
116         gameName[2][i] == '0' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << "  ";
117         GotoXY(3 + 5, i + 4); 
118         gameName[3][i] == '0' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << "  ";
119         GotoXY(4 + 5, i + 4);
120         gameName[4][i] == '0' ? Color(CT_Grey, CT_Blue) : Color(color, CT_Blue); cout << "  ";
121         _sleep(20);
122     }
123 
124     Color(CT_Grey, CT_Blue);
125 }
126 
127 //按键选择
128 void  MainMenu::ChooseButton()
129 {
130     while (true)
131     {
132         fflush(stdin);
133         char choose = getch();                        //选择按键
134         PlaySound(TEXT(".\\Music\\选择.wav"), NULL, SND_ASYNC);
135         if (choose == -32)  choose = getch();        //屏蔽-32
136 
137         //当按了方向键,切换按钮
138         if (choose == 72 || choose == 80)
139         {
140             int dPos = 0;
141             switch (choose)
142             {
143             case 72: if ( m_index > 1 )                dPos = -1; break;
144             case 80: if ( m_index < m_child->size() )dPos = +1; break;
145             }
146             if (dPos != 0)                            //发生了变动
147             {
148                 this->RemoveButtonColor();            //清除底色打印
149                 m_index += dPos;
150                 this->AppendButtonColor();            //添加底色打印
151             }
152         }
153 
154         //按Enter确定按键进入子菜单
155         if (choose == 13)
156         {
157             //清除当前菜单按键、切换界面
158             this->RemoveButton();
159             this->ChangingOver();
160 
161             //如果是最低菜单,执行功能函数
162             if (m_child->at(m_index - 1)->m_child == nullptr)
163             {
164                 m_child->at(m_index - 1)->ButtonFunction();
165                 return;
166             }
167 
168             //否则打印子菜单按键
169             m_child->at(m_index - 1)->OutputButton();
170             m_child->at(m_index - 1)->ChooseButton();
171         }    
172     }
173 }
174 
175 //====================  内部函数  ======================
176 
177 //打印所有按键
178 void MainMenu::OutputButton()
179 {
180     if (m_child == nullptr)
181         return;
182 
183     Color(CT_Grey, CT_Black);
184     //遍历打印按钮
185     for (size_t i = 0; i < m_child->size(); i++)
186     {
187         //获取按键信息
188         string* button = m_child->at(i)->m_button;
189 
190         GotoXY(m_buttonX + 0 + 4 * i, m_buttonY);
191         cout << button[0];
192         GotoXY(m_buttonX + 1 + 4 * i, m_buttonY);
193         cout << button[1];
194         GotoXY(m_buttonX + 2 + 4 * i, m_buttonY);
195         cout << button[2];
196     }
197 
198     this->AppendButtonColor();
199 }
200 
201 //清除所有按键
202 void MainMenu::RemoveButton()
203 {
204     if (m_child == nullptr)
205         return;
206 
207     Color(CT_Grey, CT_Black);
208     //遍历打印在按钮位置打印空格,清除按键
209     for (size_t i = 0; i < m_child->size(); i++)
210     {
211         GotoXY(m_buttonX + 0 + 4 * i, m_buttonY);
212         cout << "                        ";
213         GotoXY(m_buttonX + 1 + 4 * i, m_buttonY);
214         cout << "                        ";
215         GotoXY(m_buttonX + 2 + 4 * i, m_buttonY);
216         cout << "                        ";
217     }
218 }
219 
220 //移除按键底色
221 void MainMenu::RemoveButtonColor()
222 {
223     Color(CT_Grey, CT_Black);
224     string* button = m_child->at(m_index - 1)->m_button;
225     GotoXY(m_buttonX + 0 + 4 * (m_index - 1), m_buttonY);
226     cout << button[0];
227     GotoXY(m_buttonX + 1 + 4 * (m_index - 1), m_buttonY);
228     cout << button[1];
229     GotoXY(m_buttonX + 2 + 4 * (m_index - 1), m_buttonY);
230     cout << button[2];
231 
232 }
233 
234 //添加按键底色
235 void MainMenu::AppendButtonColor()
236 {
237     Color(CT_SkyBlue, CT_Red);
238     string* button = m_child->at(m_index - 1)->m_button;
239     GotoXY(m_buttonX + 0 + 4 * (m_index - 1), m_buttonY);
240     cout << button[0];
241     GotoXY(m_buttonX + 1 + 4 * (m_index - 1), m_buttonY);
242     cout << button[1];
243     GotoXY(m_buttonX + 2 + 4 * (m_index - 1), m_buttonY);
244     cout << button[2];
245     Color(CT_Grey, CT_Black);
246 }
247 
248 
249 
250 //将pMainMenu作为子目录链接到本目录
251 void MainMenu::LinkChild(MainMenu* pMainMenu)
252 {
253     pMainMenu->m_father = this;
254 
255     if (this->m_child == nullptr)
256         m_child = new vector < MainMenu* > ;
257 
258     m_child->push_back(pMainMenu);
259 }
260 
261 
262 
263 
264 //打印UI点
265 void MainMenu::PrintUI(char type, int X, int Y)
266 {
267     GotoXY(X, Y);
268     switch (type)
269     {
270     case '0': Color(CT_Grey, CT_Blue);
271         cout << "  "; break;
272     case 'z':  Color(CT_White, CT_Black);
273         cout << ""; break;
274     case '3':  Color(CT_White, CT_Red);
275         cout << ""; break;
276     case '4':  Color(CT_White, CT_Black);
277         cout << ""; break;
278     case '5':   Color(CT_White, CT_Red);
279         cout << ""; break;
280     case '8':  Color(CT_White, CT_Black);
281         cout << ""; break;
282     case 'c':  Color(CT_White, CT_Black);
283         cout << ""; break;
284     case 'e':  Color(CT_Green, CT_White);
285         cout << ""; break;
286     case 't':   Color(CT_White, CT_Black);
287         cout << ""; break;
288     case 'r':   Color(CT_White, CT_Black);
289         cout << ""; break;
290     case 'v':   Color(CT_White, CT_Black);
291         cout << ""; break;
292     case 'b':   Color(CT_White, CT_Black);
293         cout << ""; break;
294     case 'y':  Color(CT_White, CT_Black);
295         cout << ""; break;
296     case 'u':  Color(CT_White, CT_Pink);
297         cout << ""; break;
298     case 'i':   Color(CT_White, CT_Black);
299         cout << ""; break;
300     case 'a':   Color(CT_White, CT_Black);
301         cout << ""; break;
302     case 's':  Color(CT_SkyBlue, CT_White);
303         cout << ""; break;
304     case 'd':  Color(CT_White, CT_Black);
305         cout << ""; break;
306     case 'f':   Color(CT_White, CT_Black);
307         cout << ""; break;
308     case 'm':  Color(CT_Grey, CT_Black);
309         cout << ""; break;
310     case 'n':  Color(CT_Green, CT_White);
311         cout << ""; break;
312     case '2':  Color(CT_White, CT_Black);
313         cout << ""; break;
314     case '1':  Color(CT_White, CT_Black);
315         cout << ""; break;
316     case '6':  Color(CT_White, CT_Black);
317         cout << ""; break;
318     case '7':  Color(CT_White, CT_Black);
319         cout << ""; break;
320     case '9':   Color(CT_SkyBlue, CT_Black);
321         cout << ""; break;
322     case 'q':
323         cout << ""; break;
324     case 'o':   Color(CT_White, CT_Black);
325         cout << ""; break;
326     case 'p':   Color(CT_White, CT_Black);
327         cout << ""; break;
328     case 'w':  Color(CT_Grey, CT_Black);
329         cout << ""; break;
330     case 'g':  Color(CT_White, CT_Red);
331         cout << ""; break;
332     case 'h':  Color(CT_White, CT_Red);
333         cout << ""; break;
334     case 'j':   Color(CT_White, CT_Red);
335         cout << ""; break;
336     case 'k':   Color(CT_White, CT_Red);
337         cout << ""; break;
338     case 'l':   Color(CT_White, CT_Black);
339         cout << ""; break;
340     case 'x':  Color(CT_White, CT_Black);
341         cout << "×"; break;
342 
343         //子弹、手雷
344     case '_':  Color(CT_White, CT_Black); cout << "о"; break;
345     case '+':  Color(CT_White, CT_Black); cout << "¤"; break;
346     case 'C':  Color(CT_White, CT_Black); cout << "·"; break;
347     case 'D':  Color(CT_White, CT_Black); cout << ""; break;
348 
349         //制表符
350     case '!':  Color(CT_Yellow, CT_Red);
351         cout << ""; break;
352     case '&':  Color(CT_Yellow, CT_Red);
353         cout << ""; break;
354     case '@': Color(CT_Yellow, CT_Red);
355         cout << ""; break;
356     case '=': Color(CT_Yellow, CT_Red);
357         cout << ""; break;
358     case '$':  Color(CT_Yellow, CT_Red);
359         cout << ""; break;
360     case ')':  Color(CT_TBlue, CT_Red);
361         cout << ""; break;
362     case '#': Color(CT_Yellow, CT_Red);
363         cout << ""; break;
364     case '-':  Color(CT_Yellow, CT_Red);
365         cout << ""; break;
366     case '(':  Color(CT_Yellow, CT_Red);
367         cout << ""; break;
368     case '*':  Color(CT_Yellow, CT_Red);
369         cout << ""; break;
370     case '%':  Color(CT_Yellow, CT_Red);
371         cout << ""; break;
372         //额外
373     case 'A':  Color(CT_Yellow, CT_White);
374         cout << ""; break;
375     case 'B':  Color(CT_Yellow, CT_White);
376         cout << ""; break;
377     }
378 }
MainMenu.cpp

下面是派生类,也就是登陆菜单中所有实际需要创建的选项

 1级菜单:NewGame 

 1 #ifndef _NEW_GAME_H_
 2 #define _NEW_GAME_H_
 3 
 4 #include "MainMenu.h"
 5 #include "..//Role//Hero.h"
 6 #include "..//Map/Map_1.h"
 7 #include "..//Map/Map_11.h"
 8 #include "..//Map/Map_111.h"
 9 #include "..//Map/Map_22.h"
10 #include "..//Map/Map_211.h"
11 
12 
13 class NewGame : public MainMenu
14 {
15 public:
16     NewGame()
17     {
18         GetButton()[0]="┏━━━━━━━━━┓";
19         GetButton()[1]="┃开  始  新  游  戏┃";
20         GetButton()[2]="┗━━━━━━━━━┛";
21 
22     }
23 
24     //执行按钮功能
25     void ButtonFunction()
26     {
27         int x = GetButtonX();
28         int y = GetButtonY();
29 
30         char name[255] = {};
31 
32         GotoXY(x - 2, y - 2);
33         cout << "┏━━━━━━━━━━━━━┓";
34         GotoXY(x - 1, y - 2);
35         cout << "┃                          ┃";
36         GotoXY(x , y - 2);
37         cout << "┃输入名字:_______________ ┃";
38         GotoXY(x + 1, y - 2);
39         cout << "┃                          ┃";
40         GotoXY(x + 2, y - 2);
41         cout << "┗━━━━━━━━━━━━━┛";
42         GotoXY(x , y + 5); cin >> name;
43 
44         // 音乐 mp3
45         WCHAR mciCmd[] = TEXT("open .//music//background//gamestartup.mp3 alias background"); //.mp3格式的
46         mciSendString(mciCmd, NULL, 0, NULL);
47         mciSendString(TEXT("play background repeat"), NULL, 0, NULL);
48 
49        //====== 游戏初始化 ==========================
50         system("cls");
51         Role* pRole = new Hero(5, RT_CHINA,18,64);
52         pRole->SetName(name);
53         {
54         Body* body = new SniperRifle(2);
55         pRole->GetBackpack()->PushBackBody(body);
56         };
57 
58 
59         Map* pMap = new Map_1();
60         pMap->OutputMap();
61         GameScene* gameScene = new GameScene(pRole, pMap);
62         pRole->InitShape(pMap->GetType().c_str());
63         pRole->GetGameOI()->OutputOI();
64         pRole->OutputRole(pMap);
65         gameScene->GameRuning();
66         
67         //GameRunning结束时返回菜单
68         this->GetFather()->OutputButton();
69         this->GetFather()->ChooseButton();
70     }
71 
72 };
73 
74 
75 #endif // _NEW_GAME_H_
NewGame.h
 1级菜单:OldGame 和2个2级菜单
 1 #ifndef _OLD_GAME_H_
 2 #define _OLD_GAME_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class OldGame : public MainMenu
 7 {
 8 public:
 9     OldGame()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃读  取  旧  游  戏┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 };
16 
17 
18 #endif // _OLD_GAME_H_
OldGame.h
 1 #ifndef _OLD_GAME1_H_
 2 #define _OLD_GAME1_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class OldGame1 : public MainMenu
 7 {
 8 public:
 9     OldGame1()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃读  取  进  度 一 ┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 
16 
17     //执行按钮功能
18     void ButtonFunction()
19     {
20         int x = GetButtonX();
21         int y = GetButtonY();
22 
23         GotoXY(x, y);
24         cout << "进度1加载中...";
25     }
26 };
27 
28 
29 #endif // _OLD_GAME1_H_
OldGame1.h
 1 #ifndef _OLD_GAME2_H_
 2 #define _OLD_GAME2_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class OldGame2 : public MainMenu
 7 {
 8 public:
 9     OldGame2()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃读  取  进  度 二 ┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 
16 
17     //执行按钮功能
18     void ButtonFunction()
19     {
20         int x = GetButtonX();
21         int y = GetButtonY();
22 
23         GotoXY(x, y);
24         cout << "进度2加载中...";
25 
26     }
27 };
28 
29 
30 #endif // _OLD_GAME2_H_
OldGame2.h
 1级菜单:SetGame 和2个2级菜单
 1 #ifndef _SET_GAME_H_
 2 #define _SET_GAME_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class SetGame : public MainMenu
 7 {
 8 public:
 9     SetGame()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃  游  戏  设  置  ┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 
16 
17 
18 
19 };
20 
21 
22 #endif // _SET_GAME_H_
SetGame.h
 1 #ifndef _VIDEO_H_
 2 #define _VIDEO_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class Video : public MainMenu
 7 {
 8 public:
 9     Video()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃  视  频  设  置  ┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 
16 
17     //执行按钮功能
18     void ButtonFunction()
19     {
20         int x = GetButtonX();
21         int y = GetButtonY();
22 
23         GotoXY(x, y);
24         cout << "按方向键调整亮度";
25 
26     }
27 };
28 
29 
30 #endif // _VIDEO_H_
Video.h
 1 #ifndef _AUDIO_H_
 2 #define _AUDIO_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 class Audio : public MainMenu
 7 {
 8 public:
 9     Audio()
10     {
11         GetButton()[0] = "┏━━━━━━━━━┓";
12         GetButton()[1] = "┃  音  频  设  置  ┃";
13         GetButton()[2] = "┗━━━━━━━━━┛";
14     }
15 
16 
17     //执行按钮功能
18     void ButtonFunction()
19     {
20         int x = GetButtonX();
21         int y = GetButtonY();
22 
23         GotoXY(x, y);
24         cout << "按方向键调整大小";
25 
26     }
27 };
28 
29 
30 #endif // _AUDIO_H_
Audio.h

1级菜单:GameInfo

 1 #ifndef _Game_Info_H_
 2 #define _Game_Info_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 //============= 登录界面 ===============
 7 
 8 class GameInfo : public MainMenu
 9 {
10 public:
11     GameInfo();
12 
13 public://==== 外部接口 ======
14     virtual void ChangingOver();        //切换界面
15 
16     virtual void ButtonFunction();        //执行按钮功能
17 
18 
19 
20 };
21 
22 #endif // _Game_Info_H_
GameInfo.h
  1 #include"..//Marco.h"
  2 #include"..//Tip.h"
  3 
  4 #include "GameInfo.h"
  5 using namespace std;
  6 
  7 
  8 GameInfo::GameInfo()
  9 {
 10     GetButton()[0]="┏━━━━━━━━━┓";
 11     GetButton()[1]="┃  关  于  我  们  ┃";
 12     GetButton()[2]="┗━━━━━━━━━┛";
 13 }
 14 
 15 
 16 //切换界面
 17 void GameInfo::ChangingOver()
 18 {
 19 
 20 }
 21 
 22 //执行按钮功能
 23 void GameInfo::ButtonFunction()
 24 {
 25     //游戏简介
 26     string introduction[26] = {};
 27     introduction[0] = "    自从前苏联时期美、苏两国实行“双方保证毁灭政策”之后,世界一直出于冷战与和平的状态......";
 28     introduction[1] = "爱因斯坦曾说“我不知道第三次世界大战用什么武器,但是第四次世界大战人类将只会用木棒和石头打”";
 29     introduction[2] = "也就是说以现在的科技文明爆发战争就等于自我毁灭。                                            ";
 30     introduction[3] = "    八十年代美国有个学者写过一本书《文明的冲突》,很出名,大致意思是未来世界冲突不再像冷战那";
 31     introduction[4] = "样,而是政治集团和意识形态的冲突,而最终归结到文明之间的冲突,世界主要分为基督教文明,佛教文";
 32     introduction[5] = "明和伊斯兰教文明。基督教文明和佛教文明与外界冲突很有限,更多表现是经济战争和领土纠纷,而且范";
 33     introduction[6] = "围和强度是可控的。只有伊斯兰教文明和外部世界有强烈的对抗性和排他性。未来伊斯兰教很可能会越来";
 34     introduction[7] = "越趋向保守和原教旨主义,瓦哈比之类的激进教派将逐步占据绝对主导,开明的穆斯林将逐步被排挤和屠";
 35     introduction[8] = "杀,当伊斯兰世界完成内部极端主义的整合和统一后,矛头将逐步对外,会在全世界掀起全面的圣战。新";
 36     introduction[9] = "的统一的阿拉伯帝国将会重新出现,它将在中东的边境将开展大肆的侵略,在全世界将进行全面的高强度";
 37     introduction[10] = "的有政府组织的自杀式恐怖袭击,试图用瓦哈比派统治全世界,到那个时候,全世界将被迫和伊斯兰文明";
 38     introduction[11] = "进行一场全面战争,甚至会相互爆发大规模种族屠杀,只有通过这种方式,文明的冲突才可最终得到解决";
 39     introduction[12] = "                                                                                            ";
 40     introduction[13] = "    历史历历在目......                                                                        ";
 41     introduction[14] = "    1979年,震惊世界的伊朗伊斯兰革命爆发,推翻了亲美的巴列维封建王朝。                        ";
 42     introduction[15] = "    同样在1979年末,苏联出兵入侵阿富汗,美国开始资助阿富汗及巴基斯坦的圣战者抗苏武装。        ";
 43     introduction[16] = "    2003年,美国总统小布什以伊拉克萨达姆政权拥有“大规模杀伤性武器”为由,出兵侵占伊拉克。尽";
 44     introduction[17] = "管推翻了萨达姆的独裁统治,最终却没有找到任何“大规模杀伤性武器”,美国此举的副作用是大规模摧";
 45     introduction[18] = "毁了伊拉克的各种社会机构,客观上促成伊境内恐怖主义组织如雨后春笋般涌现......                ";
 46     introduction[19] = "    2006年10月,本.拉登副手扎卡维宣布成立“伊拉克伊斯兰国”......                            ";
 47     introduction[20] = "    2011年,“阿拉伯之春”波及叙利亚,叙开始动乱,叙利亚从此陷入旷日持久的血腥宗派冲突。“伊";
 48     introduction[21] = "斯兰国”在血腥的叙利亚内战中“崛起”,他们在叙利亚东部地区建立了根据地,伊斯兰国袭击的范围不";
 49     introduction[22] = "断扩大,冲突次数不断增加,世界人民饱受极端组织的困扰......                                    ";
 50     introduction[23] = "    为了维护世界和平,2014年9月,美国组建了一个包括中、俄、英、法等54个国家和欧盟 、北约以及";
 51     introduction[24] = "阿盟等地区组织在内的国际联盟以打击IS;从此,第三次世界大战全面爆发……                        ";
 52     introduction[25] = "    虽然我们的生活暂未受到恐怖袭击的影响,但为了世界的和平,我避免不了卷入这场战争。        ";
 53 
 54     Color(CT_White, CT_Black);
 55 
 56     //打印信息
 57     system("cls");
 58     GotoXY(2, 21);
 59     cout << "【 游 戏 信 息 】";
 60     GotoXY(4, 3);
 61     cout << "●游戏名称:英文名  World War III";
 62     GotoXY(6, 3);
 63     cout << "            中文名 第三次世界大战";
 64     GotoXY(8, 3);
 65     cout << "●游戏类型: Adventure puzzle RPG";
 66     GotoXY(10, 3);
 67     cout << "●学生姓名: xx";
 68     GotoXY(12, 3);
 69     cout << "●版权所有:xxx";
 70     GotoXY(14, 21);
 71     cout << "【 游 戏 背 景 】";
 72 
 73     //printRow开始打印的行号 (开始行号会依次上升 -2)
 74     //indexMin当前打印最小角标(最小角标为0,但当总行数超过6行时依次增加)
 75     //indexMax当前打印最大角标(最大角标会依次递增 +1)
 76     //size 表示可现实最大行数
 77     int  size = 7;
 78     int  indexMin = 0;
 79     int  dTime = 2000;
 80     for (int printRow = 30, indexMax = 0; indexMax < 26; indexMax++)
 81     {
 82         //当总行速达到6时,最小角标开始递增 ,开始、结束打印的行号不再变化
 83         if (indexMax > size)
 84             indexMin = indexMax - size;
 85 
 86         for (int row = printRow, j = indexMin; j <= indexMax; j++, row += 2)
 87         {
 88             GotoXY(row, 3);
 89             //最新的一行一个字一个字打印
 90             if (j == indexMax)
 91             {
 92                 Color(CT_White, CT_Red);
 93                 cout << introduction[j];
 94                 Color(CT_White, CT_Black);
 95                 break;
 96             }
 97             cout << introduction[j];
 98         }
 99 
100         if (indexMax < size)
101             printRow -= 2;
102 
103         //停顿时间
104         _sleep(dTime);
105 
106         //按键修改速度
107         if (kbhit())
108         {
109             char ch = getch();
110             dTime = dTime / 2;
111         }
112     }
113     char ch = getch();
114     //结束时返回菜单
115     this->GetFather()->OutputFrame();
116     this->GetFather()->OutputButton();
117     this->GetFather()->ChooseButton();
118 }
GameInfo.cpp

 

1级菜单:退出游戏

 1 #ifndef _EXIT_GAME_H_
 2 #define _EXIT_GAME_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 
 7 class ExitGame : public MainMenu
 8 {
 9 public:
10     ExitGame()
11     {
12         GetButton()[0] = "┏━━━━━━━━━┓";
13         GetButton()[1] = "┃太危险了还是离开吧┃";
14         GetButton()[2] = "┗━━━━━━━━━┛";
15     }
16 
17 
18 
19     //执行按钮功能
20     void ButtonFunction()
21     {
22         exit(0);
23     }
24 
25 };
26 
27 
28 #endif // _EXIT_GAME_H_
ExitGame.h

还有一个可以复用的菜单:返回上一层菜单

 1 #ifndef _RETURN_H_
 2 #define _RETURN_H_
 3 
 4 #include "MainMenu.h"
 5 
 6 
 7 class Return : public MainMenu
 8 {
 9 public:
10     Return()
11     {
12         GetButton()[0] = "┏━━━━━━━━━┓";
13         GetButton()[1] = "┃  返 回 上 一 级  ┃";
14         GetButton()[2] = "┗━━━━━━━━━┛";
15     }
16 
17     //执行按钮功能
18     void ButtonFunction()
19     {
20         this->RemoveButton();
21         this->GetFather()->GetFather()->OutputButton();
22         this->GetFather()->GetFather()->ChooseButton();
23     }
24 
25 };
26 
27 
28 #endif // _RETURN_H_
Return.h

 

2、通过上面的的类可以创建出菜单,然后通过一个登陆管理类,来设置这些按钮的层级关系(树结构中的位置关系)

 1 #ifndef _LAND_SURFACE_H_
 2 #define _LAND_SURFACE_H_
 3 
 4 #include "MainMenu.h"
 5 #include "GameInfo.h"
 6 #include "NewGame.h"
 7 #include "ExitGame.h"
 8 #include "Return.h"
 9 #include "OldGame.h"
10 #include "OldGame1.h"
11 #include "OldGame2.h"
12 #include "SetGame.h"
13 #include "Audio.h"
14 #include "Video.h"
15 //=======用来管理登陆系统============
16 class LandSurface
17 {
18 public:
19     //初始化登陆界面(静态函数,不用创建对象)
20     static MainMenu* InitLandSurface()
21     {
22         MainMenu* pMainMenu = new MainMenu();
23         //主菜单
24         MainMenu* pNewGame = new NewGame();
25         pMainMenu->LinkChild(pNewGame);
26         MainMenu* pOldGame = new OldGame();
27         pMainMenu->LinkChild(pOldGame);
28         MainMenu* pSetGame = new SetGame();
29         pMainMenu->LinkChild(pSetGame);
30         MainMenu* pGameInfo = new GameInfo();
31         pMainMenu->LinkChild(pGameInfo);
32         MainMenu* pExitGame = new ExitGame();
33         pMainMenu->LinkChild(pExitGame);
34         //OldGame
35         MainMenu* pOldGame1 = new OldGame1();
36         pOldGame->LinkChild(pOldGame1);
37         MainMenu* pOldGame2 = new OldGame2();
38         pOldGame->LinkChild(pOldGame2);
39         MainMenu* pReturn1 = new Return();
40         pOldGame->LinkChild(pReturn1);
41         //SetGame
42         MainMenu* pAudio = new Audio();
43         pSetGame->LinkChild(pAudio);
44         MainMenu* pVideo = new Video();
45         pSetGame->LinkChild(pVideo);
46         MainMenu* pReturn2 = new Return();
47         pSetGame->LinkChild(pReturn2);
48 
49         return pMainMenu;
50     }
51 
52 };
53 
54 
55 #endif // _LAND_SURFACE_H_
LandSurface.h

 

总结:通过这段代码,可以实现控制台中制作出一个自动排成1列的菜单目录,可以给任意菜单添加和删除子菜单,实现了菜单与子菜单之间的进入和返回操作

截图:

 

转载于:https://www.cnblogs.com/nanwei/p/7087683.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371847.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

不存在_施文忠 | ”存在“与“不存在”——巴蜀文明概论

海德格尔有句名言&#xff1a;“存在者存在&#xff0c;不存在者不存在&#xff01;”四川&#xff0c;一个伟大的存在&#xff0c;偏偏存在于四川的口头禅却是“不存在”。在不存在中追求存在&#xff0c;在存在中摆脱存在。六月白鹿镇&#xff0c;书院学习了《李白与海德格尔…

Spring和JSF集成:异常处理

大多数JSF开发人员都会熟悉“发生错误”页面&#xff0c;当在他们的代码某处引发意外异常时&#xff0c;该页面就会显示。 该页面在开发时确实很有用&#xff0c;但对于生产应用程序通常不是您想要的。 通常&#xff0c;在用库存JSF替换此页面时&#xff0c;您有两种选择。 您可…

Altium 原理图出现元件“Extra Pin…in Normal of part ”警告的解决方法

转载于&#xff1a; http://blog.csdn.net/idoming/article/details/45575627 使用Altium Designer的时候编译完后&#xff0c;只关注过错误没有关注过警告&#xff0c;现在认真排查一下有哪些警告。 正在进行的项目原理图编译完成后提示标题中的警告信息。经过在网上搜索&…

XidianOJ 1087 浪漫的V8

题目描述 V8为了讨女朋友开心&#xff0c;给lx承包大活后面那个水塘。为了筹集资金&#xff0c;V8偷偷地溜进了一座古墓&#xff0c;发现在他面前有金光闪闪的若干小箱子&#xff0c;里面全都是金粉&#xff0c;作为横行于各种#&#xffe5;&场所的V8来说&#xff0c;辨别不…

curl php 模拟来源_php 使用curl模拟ip和来源进行访问的实现方法

对于限制了ip和来源的网站&#xff0c;使用正常的访问方式是无法访问的。本文将介绍一种方法&#xff0c;使用php的curl类实现模拟ip和来源&#xff0c;访问那些限制了ip和来源的网站。1.设置页面限制ip和来源访问server.php$client_ip getip();$referer getreferer();$allow…

堆栈C语言实现

堆栈的抽象数据类型描述&#xff1a; 类型名称&#xff1a; 堆栈&#xff08;Stack&#xff09;。数据对象集&#xff1a; 一个有 0 个或多个元素的又穷表。操作集&#xff1a; 长度为 max_size 的堆栈 S ∈ Stack&#xff0c; 堆栈元素 item ∈ ElementType。stack creatc_sta…

woocommerce 分类到菜单_Woocommerce商店显示分类

我是wordpress的新手, 所以如果我输入的语言错误, 请仍然为我提供帮助。我想使用woocommerce显示具有可变产品的商店, 我希望商店链接登录页面显示具有该类别特征图像的商店类别。我当前的商店页面显示所有产品, 并分页到其他产品页面, 我找不到所有产品的模板。当我进入wp-adm…

JBoss BRMS 5.3 –添加了业务活动监视(BAM)报告

自从JBoss BRMS 5.3产品发布以来&#xff0c;添加了jBPM 5 BPM组件的最常见问题之一是业务活动监视&#xff08;BAM&#xff09;和报告功能。 本文将引导您完成添加过程&#xff0c;但是请注意&#xff0c;在撰写本文时&#xff0c;这不是产品的受支持功能。 在JBoss BRMS 5.3上…

Zookeeper开源客户端框架Curator简介

Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情, 于是在它的基础上包装了一下, 提供了一套更好用的客户端框架. Netflix在用ZooKeeper的过程中遇到的问题, 我们也遇…

【树形DP】 HDU 2196 Computer

题意&#xff1a;求节点间的最大距离 先DFS一次 记录下 每一节点的子树下的最大距离&#xff08;DP[ u ] [ 0 ]&#xff09;和第二大距离&#xff08;DP[ u ] [ 1 ]&#xff09; 用DP[ v ] [ 2 ] 表示由v的父节点来的最大距离 再取DP[ u ] [ 0 ] 与 DP[ u ][ 2 ] 的最值 #inclu…

适当的Java堆大小的5个技巧

确定生产系统合适的Java堆大小不是一件容易的事。 在我的Java EE企业经验中&#xff0c;我发现由于Java堆容量和调整不足而导致的多个性能问题。 本文将为您提供5个技巧&#xff0c;这些技巧可以帮助您确定当前或新生产环境的最佳Java堆大小。 这些技巧中的一些对于预防和解决j…

pythondocumentation是什么_怎样阅读Python官方文档

如何阅读官方Python文档的初学者,因为他们没有相关的经验,学习语言通常是费时且劳动密集型和效果不是很好。下面简要介绍如何阅读官方文件。一旦你学会快速查询官方文件,学习效率会提高很多文档门户。如何阅读API文档中内容标准库,如何快速找到你想要的。第一种方法是先查找索引…

数据库过大无法导入

导SQL数据库结构数据时&#xff0c;如果数据是批量插入的话会报错&#xff1a;2006 - MySQL server has gone away。 解决办法&#xff1a;找到你的mysql目录下的my.ini配置文件&#xff0c;加入以下代码 max_allowed_packet500M wait_timeout288000 interactive_timeout 2880…

UVa 11475 - Extend to Palindrome

題目&#xff1a;給你一個字符串&#xff0c;在後面拼接一部分使得它變成回文串&#xff0c;使得串最短。輸出這個回文串。分析&#xff1a;KMP&#xff0c;dp。這裡利用KMP算法將串和它的轉置匹配&#xff0c;看結束時匹配的長度就可以。 因為串比较長。使用KMP比较合適&#…

构建Java Web应用程序时遵循MVC的三个步骤

步骤1 做 始终通过servlet / action bean处理URL&#xff08;POST表单&#xff0c;单击链接等&#xff09;&#xff0c;而不是通过JSP处理 为什么 ActionBeans&#xff08;无论某些框架调用那些类&#xff09;&#xff0c;而servlet很少是控制器 用于处理用户输入。 JSP是专用于…

曝光原理_泰国精戈咖啡效果反馈 作用原理曝光

我的男人才三十五六&#xff0c;两个人就开始分开睡了&#xff0c;自从咱们在一起以来&#xff0c;咱们的感情一向很好&#xff0c;这是十分调和的。但随着年纪的添加&#xff0c;我逐渐发现他身体阑珊的越来越凶猛&#xff0c;夫妻生活方面硬度逐渐下降&#xff0c;时间也越来…

使用junit4测试Spring

Spring 提供便捷的测试&#xff0c;非常方便整合Junit 导入 spring-test-3.2.0.RELEASE.jar ---- 提供与Junit的整合 RunWith(SpringJUnit4ClassRunner.class) // 整合 ContextConfiguration(locations"classpath:applicationContext.xml") // 加载配置public class…

EasyCriteria –使用JPA Criteria的简便方法

今天&#xff0c;我们将看到有关此工具的信息&#xff0c;该工具使使用JPA Criteria更加容易。 使用该库的应用程序将在JPA实现中更加简洁&#xff0c;易于使用和可移植。 在本文的结尾&#xff0c;您将找到要下载的源代码。 什么是标准&#xff1f; 当前是创建动态查询的最佳…

语言模拟蒲丰问题_R语言小数定律的保险业应用:泊松分布模拟索赔次数

原文链接&#xff1a;拓端数据科技 / Welcome to tecdat​tecdat.cn在保险业中&#xff0c;由于分散投资&#xff0c;通常会在合法的大型投资组合中提及大数定律。在一定时期内&#xff0c;损失“可预测”。当然&#xff0c;在标准的统计假设下&#xff0c;即有限的期望值和独立…

THINKPHP

路径 /index.php/home/...一般路径应用或者U方法转载于:https://www.cnblogs.com/lidepeng/p/6180631.html