我是靠谱客的博主 小巧宝马,最近开发中收集的这篇文章主要介绍【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

最后

以上就是小巧宝马为你收集整理的【C++第一个Demo】---控制台RPG游戏3【登陆菜单树】的全部内容,希望文章能够帮你解决【C++第一个Demo】---控制台RPG游戏3【登陆菜单树】所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部