【登陸系統--樹結構】
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_


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 }
下面是派生類,也就是登陸菜單中所有實際需要創建的選項
?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_
?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_


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_


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_
?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_


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_


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_
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_


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 }
?
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_
還有一個可以復用的菜單:返回上一層菜單


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_
?
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_
?
總結:通過這段代碼,可以實現控制臺中制作出一個自動排成1列的菜單目錄,可以給任意菜單添加和刪除子菜單,實現了菜單與子菜單之間的進入和返回操作
截圖:
?