【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.pswp.cn/news/371847.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371847.shtml
英文地址,請注明出處:http://en.pswp.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