C++开发C++开发C++游戏开发基础课3
serverDream1.回顾
地图
1 2 3 4 5 6 7 8 9 10
| string map1[10] = {"####################", "#Y #", "# #", "# #", "# #", "# #", "# #", "# #", "# X#", "####################"};
|
输出
1 2 3 4 5 6 7
| void print(string maps[], int size) { system("cls"); for(int i = 0; i < size, i++) { cout << maps[i] << endl; } return; }
|
调用示例
2.编写移动程序
移动程序Move需要许多代码模块,先让我们滤清思路,再继续编写
1.检查玩家初始位置
我们需要将地图便利一遍,寻找玩家初始位置’Y’的位置,然后将’Y’删除(变成空格)并记录该位置,代码如下
1 2 3 4 5 6 7 8 9 10
| 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; } } }
|
2.检测并移动
①检测输入
我们需要使用到输入函数getch来读取玩家的操作(WASD),代码如下
②移动(w)
先检测按下的键是否是w
1
| if(a == 'w' || a == 'W')
|
再改变玩家的y坐标
1 2 3 4 5
| if(a == 'w' || a == 'W') { if(maps[y - 1][x] != '#') { y--; } }
|
③完善其余方向移动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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++; } }
|
④胜利条件
胜利条件为到达终点
1 2 3 4 5
| if(maps[y][x] == 'X') { system("cls"); cout << "你赢了!" << endl; return; }
|
⑤改进显示地图函数
由于我们的xy坐标并没有在地图中通过字符改变,所以我们需要在显示地图函数中加入判断,以显示玩家位置
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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]; } } } return; }
|
⑥移动代码
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
| 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; return; } } return; }
|
4.调用函数
在主函数中调用移动函数,以启动游戏
1 2 3 4
| int main() { move(map1, 10); return 0; }
|
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
| #include <iostream> #include <conio.h> #include <cstring> #include <windows.h> using namespace std; string map1[10] = {"####################", "#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; return; } } return; } int main() { move(map1, 10); return 0; }
|
下载代码示例