C++游戏开发基础课3

1.回顾

地图

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) {	            //maps为带入的地图, size为大小
system("cls"); //清空屏幕
for(int i = 0; i < size, i++) {
cout << maps[i] << endl; //输出第i行地图
}
return;
}

调用示例

1
print(map1, 10);//map1的长为10(map1[10])

2.编写移动程序

移动程序Move需要许多代码模块,先让我们滤清思路,再继续编写

1.检查玩家初始位置

我们需要将地图便利一遍,寻找玩家初始位置’Y’的位置,然后将’Y’删除(变成空格)并记录该位置,代码如下

1
2
3
4
5
6
7
8
9
10
for(int i = 0; i < size; i++) {                             //size行
for(int j = 0; j < maps[i].size(); j++) { //maps[i].size()列,不懂的去搜string的基本操作
if(maps[i][j] == 'Y') {
y = i;
x = j;
maps[i][j] = ' '; //覆盖初始位置
break;
}
}
}
2.检测并移动
①检测输入

我们需要使用到输入函数getch来读取玩家的操作(WASD),代码如下

1
char a = getch();
②移动(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--; //在方阵中,y越小,所在位置越高
}
}

image-20241009125856077

③完善其余方向移动
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; //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; //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;
}

下载代码示例