C++开发C++开发C++游戏开发基础课4
serverDream1.system(“pause”)
“pause“命令为控制台窗口CMD的等待命令,目的是输出”按任意键继续“然后等待用户按下任意的键,继续执行后面的命令
system命令可以在C++中执行CMD的命令,所以system(“pause”)就是”pause”的”C++版”
2.多地图闯关
我们已经完成了第一关地图的编写,我们现在需要加入更多的地图以丰富游戏
1 2 3 4 5 6 7 8 9 10 11 12 13
| ################################## #Y#X # # ############################## # # # # ############################## # # # # # # # # ############### # # # # # # # ############### # # # # # ############################## # # # ##################################
|
用map2(string)保存方式
1 2 3 4 5 6 7 8 9 10 11 12 13
| string map2[13] = {"##################################", "#Y#X #", "# ############################## #", "# # #", "############################## # #", "# # # #", "# # ############### #", "# # # #", "# # ############### #", "# # #", "# ############################## #", "# #", "##################################"}
|
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <iostream> #include <conio.h> #include <cstring> #include <windows.h> using namespace std; string map1[10] = {"####################", "#Y #", "# #", "# #", "# #", "# #", "# #", "# #", "# X#", "####################"}; string map2[13] = {"##################################", "#Y#X #", "# ############################## #", "# # #", "############################## # #", "# # # #", "# # ############### #", "# # # #", "# # ############### #", "# # #", "# ############################## #", "# #", "##################################"} void print(string maps[], int size, int x, int y) { system("cls"); for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(i == y && j == x) { cout << "Y"; } else { cout << maps[i][j]; } } cout << endl; } return; } void move(string maps[], int size) { int x, y; for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(maps[i][j] == 'Y') { y = i; x = j; maps[i][j] = ' '; break; } } } while(true) { print(maps, size, x, y); char a = getch(); if(a == 'w' || a == 'W') { if(maps[y - 1][x] != '#') { y--; } } else if(a == 's' || a == 'S') { if(maps[y + 1][x] != '#') { y++; } } else if(a == 'a' || a == 'A') { if(maps[y][x - 1] != '#') { x--; } } else if(a == 'd' || a == 'D') { if(maps[y][x + 1] != '#') { x++; } } if(maps[y][x] == 'X') { system("cls"); cout << "你赢了!" << endl; system("pause"); return; } } return; } int main() { move(map1, 10); move(map2, 13); return 0; }
|
2.设置陷阱
由于地图没有陷阱(比如陷阱出口),所以我们要设置一点坑货
在地图中加入陷阱出口( V )
1 2 3 4 5 6 7 8 9 10 11 12 13
| string map2[13] = {"##################################", "#Y#X #", "# ############################## #", "# # #", "############################## # #", "# # # #", "# # ############### #", "# # V# #", "# # ############### #", "# V# #", "# ############################## #", "# #", "##################################"}
|
玩家在碰到”V”的时候会清屏并输出”你死了”,然后等待用户确认
用户在确认后将会传送至入口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| void move(string maps[], int size) { int x, y, old_x, old_y; for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(maps[i][j] == 'Y') { y = i; x = j; old_x = i; old_y = j; maps[i][j] = ' '; break; } } } while(true) { print(maps, size, x, y); char a = getch(); if(a == 'w' || a == 'W') { if(maps[y - 1][x] != '#') { y--; } } else if(a == 's' || a == 'S') { if(maps[y + 1][x] != '#') { y++; } } else if(a == 'a' || a == 'A') { if(maps[y][x - 1] != '#') { x--; } } else if(a == 'd' || a == 'D') { if(maps[y][x + 1] != '#') { x++; } } if(maps[y][x] == 'X') { system("cls"); cout << "你赢了!" << endl; system("pause"); return; } if(maps[y][x] == 'V') { system("cls"); cout << "你死了!" << endl; cout << "呃,这个出口可能是陷阱……" << endl; system("pause"); x = old_x; y = old_y; } } return; }
|
输出的时候要特意隐藏该出口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void print(string maps[], int size, int x, int y) { system("cls"); for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(i == y && j == x) { cout << "Y"; } else { if(maps[i][j] == 'V') { cout << "Y"; } else { cout << maps[i][j]; } } } cout << endl; } return; }
|
3.总结
代码一放,完结撒花~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <iostream> #include <conio.h> #include <cstring> #include <windows.h> using namespace std; string map1[10] = {"####################", "#Y #", "# #", "# #", "# #", "# #", "# #", "# #", "# X#", "####################"}; string map2[13] = {"##################################", "#Y#X #", "# ############################## #", "# # #", "############################## # #", "# # # #", "# # ############### #", "# # V# #", "# # ############### #", "# V# #", "# ############################## #", "# #", "##################################"} void print(string maps[], int size, int x, int y) { system("cls"); for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(i == y && j == x) { cout << "Y"; } else { if(maps[i][j] == 'V') { cout << "Y"; } else { cout << maps[i][j]; } } } cout << endl; } return; } void move(string maps[], int size) { int x, y, old_x, old_y; for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(maps[i][j] == 'Y') { y = i; x = j; old_x = i; old_y = j; maps[i][j] = ' '; break; } } } while(true) { print(maps, size, x, y); char a = getch(); if(a == 'w' || a == 'W') { if(maps[y - 1][x] != '#') { y--; } } else if(a == 's' || a == 'S') { if(maps[y + 1][x] != '#') { y++; } } else if(a == 'a' || a == 'A') { if(maps[y][x - 1] != '#') { x--; } } else if(a == 'd' || a == 'D') { if(maps[y][x + 1] != '#') { x++; } } if(maps[y][x] == 'X') { system("cls"); cout << "你赢了!" << endl; system("pause"); return; } if(maps[y][x] == 'V') { system("cls"); cout << "你死了!" << endl; cout << "呃,这个出口可能是陷阱……" << endl; system("pause"); x = old_x; y = old_y; } } return; } int main() { move(map1, 10); move(map2, 13); return 0; }
|