-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordGame.cpp
More file actions
138 lines (127 loc) · 2.32 KB
/
Copy pathWordGame.cpp
File metadata and controls
138 lines (127 loc) · 2.32 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "WordGame.h"
#include "utils.h"
#include "common.h"
void WordGame::init()
{
for (size_t i = 0; i < COL; i++)
{
for (size_t j = 0; j < ROW; j++)
{
map[i][j] = 0;
}
}
}
WordGame::WordGame()
{
init();
}
void WordGame::load(int level)
{
std::ifstream f("1.wor");
if (!f.is_open())
{
std::cout << "关卡加载失败!";
exit(1);
}
int ch;
for (size_t i = 0; i < COL; i++)
{
for (size_t j = 0; j < ROW; j++)
{
if (f >> ch && ch != EOF)
{
map[i][j] = ch;
}
}
}
Pos peoplePos = people.getPos();
map[peoplePos.x][peoplePos.y] = 2;
show();
}
void WordGame::play()
{
while (true)
{
move();
system("cls");
show();
if (map[people.getPos().y][people.getPos().x] == 3)
{
break;
}
}
std::cout << "You win!";
exit(0);
}
// 渲染
void WordGame::show()
{
for (size_t i = 0; i < COL; i++)
{
for (size_t j = 0; j < ROW; j++)
{
if (map[i][j] == 0)
{
std::cout << " ";
}
else if (map[i][j] == 1)
{
std::cout << "墙";
}
else if (map[i][j] == 2)
{
std::cout << "人";
}
else if (map[i][j] == 3)
{
std::cout << "门";
}
}
std::cout << '\n';
}
// 这里是测试用的,打印实际二维数组内容
//for (size_t i = 0; i < COL; i++)
//{
// for (size_t j = 0; j < ROW; j++)
// {
// std::cout << map[i][j];
// }
// std::cout << '\n';
//}
}
// 这里的xy坐标有点问题,之后会进行优化
void WordGame::move()
{
char input = getInput();
Pos curPos = people.getPos();
if ((input == 'w' || input == 'W'))
{
map[curPos.y - 1][curPos.x] == 1 ? people.setPos(curPos.x, curPos.y) : people.setPos(curPos.x, curPos.y - 1);
}
else if ((input == 'a' || input == 'A'))
{
map[curPos.y][curPos.x - 1] == 1 ? people.setPos(curPos.x, curPos.y) : people.setPos(curPos.x - 1, curPos.y);
}
else if ((input == 's' || input == 'S'))
{
map[curPos.y + 1 ][curPos.x]== 1 ? people.setPos(curPos.x, curPos.y) : people.setPos(curPos.x, curPos.y + 1);
}
else if ((input == 'd' || input == 'D'))
{
map[curPos.y][curPos.x + 1] == 1 ? people.setPos(curPos.x, curPos.y) : people.setPos(curPos.x + 1, curPos.y);
}
update(curPos, people.getPos());
}
// 更新地图
void WordGame::update(Pos origin, Pos current)
{
if (map[current.y][current.x] == 3)
{
return;
}
map[origin.y][origin.x] = 0;
map[current.y][current.x] = 2;
}