-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine_v0.2.cpp
More file actions
219 lines (184 loc) · 5.58 KB
/
Copy pathGameEngine_v0.2.cpp
File metadata and controls
219 lines (184 loc) · 5.58 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <windows.h>
#include <iostream>
#include <conio.h>
struct Position {
int x = 10;
int y = 5;
};
struct Command {
bool isMoveCommand = false;
bool isSystemCommand = false;
};
struct MoveCommand {
bool up = false;
bool down = false;
bool left = false;
bool right = false;
};
struct SystemCommand {
bool running = false;
bool quit = false;
};
class Character {
public:
int getPositionX() {
return pos.x;
}
int getPositionY() {
return pos.y;
}
void setPositionX(int newPosX) {
pos.x = newPosX;
}
void setPositionY(int newPosY) {
pos.y = newPosY;
}
private:
Position pos;
};
void GoToXY(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// 隐藏光标,避免闪烁
void hideCursor() {
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// 清空输入缓冲区中所有待处理的按键
void cleanInputBuffer() {
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
FlushConsoleInputBuffer(hInput);
}
// 设置颜色的辅助函数
void SetColor(int textColor, int backgroundColor) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, backgroundColor * 16 + textColor);
}
// 检测按键是否被按下
bool isKeyPressed(int vKey) {
return (GetAsyncKeyState(vKey) & 0x8000) != 0;
}
void move(MoveCommand& moveCommand, Character& character) {
int x = character.getPositionX();
int y = character.getPositionY();
if (moveCommand.up == true && y > 5) {
character.setPositionY(--y);
}
if (moveCommand.left == true && x > 11) {
x -= 2;
character.setPositionX(x);
}
if (moveCommand.down == true && y < 25) {
character.setPositionY(++y);
}
if (moveCommand.right == true && x < 100) {
x += 2;
character.setPositionX(x);
}
}
// 清除指定位置的火柴人(用空格覆盖,防止拖影)
void clearCharacter(Character& character) {
int x = character.getPositionX();
int y = character.getPositionY();
GoToXY(x, y); std::cout << " ";
GoToXY(x, y + 1); std::cout << " ";
GoToXY(x, y + 2); std::cout << " ";
}
void render(Character& character) {
int x = character.getPositionX();
int y = character.getPositionY();
GoToXY(x, y++);
std::cout << " O " << std::endl; // 头
GoToXY(x, y++);
std::cout << " /|\\ " << std::endl; // 身体和手
GoToXY(x, y);
std::cout << " / \\ " << std::endl; // 腿
hideCursor();
}
void checkBoarder(Character& character) {
int x = character.getPositionX();
int y = character.getPositionY();
if (x == 10) {
GoToXY(x - 1, y);
std::cout << "|" << std::endl;
GoToXY(x - 1, y + 1);
std::cout << "|" << std::endl;
GoToXY(x - 1, y + 2);
std::cout << "|" << std::endl;
}
else if (x == 100) {
GoToXY(x + 6, y);
std::cout << "|" << std::endl;
GoToXY(x + 6, y + 1);
std::cout << "|" << std::endl;
GoToXY(x + 6, y + 2);
std::cout << "|" << std::endl;
}
if (y == 5) {
GoToXY(x, y - 1);
std::cout << "______";
}
else if (y == 25) {
GoToXY(x, y + 3);
std::cout << "______";
}
}
void screenRefresh(MoveCommand& moveCommand, Character& character) {
clearCharacter(character);
move(moveCommand, character);
render(character);
checkBoarder(character);
}
char getCommand(MoveCommand& moveCommand, SystemCommand& systemCommand, Command& sumCommand) {
cleanInputBuffer();
systemCommand.quit = isKeyPressed('0');
if (systemCommand.quit == true) {
sumCommand.isSystemCommand = true;
systemCommand.running = false;
sumCommand.isMoveCommand = false;
return '0';
}
moveCommand.up = isKeyPressed('W') || isKeyPressed(VK_UP);
moveCommand.down = isKeyPressed('S') || isKeyPressed(VK_DOWN);
moveCommand.left = isKeyPressed('A') || isKeyPressed(VK_LEFT);
moveCommand.right = isKeyPressed('D') || isKeyPressed(VK_RIGHT);
sumCommand.isMoveCommand = moveCommand.up || moveCommand.down || moveCommand.left || moveCommand.right;
return '1';
}
int main() {
char command = '1';
int i = 2;
Character character;
MoveCommand moveCommand;
SystemCommand systemCommand;
Command sumCommand;
SetConsoleTitleA("Game Engine v0.2 —— 自由移动(free moving)");
SetColor(15, 0); // 7+8,7为白,8为亮
std::cout << "操作时请输入'W','A','S','D'控制人物移动" << std::endl;
std::cout << "输入'0'为终止程序" << std::endl;
std::cout << "请输入以开始:";
command = _getch();
command = '1';
system("cls");
screenRefresh(moveCommand, character);
while (1) {
if (command != '0') {
systemCommand.running = true;
if (sumCommand.isMoveCommand == true) {
screenRefresh(moveCommand, character);
}
Sleep(15);
}
else {
break;
}
cleanInputBuffer();
command = getCommand(moveCommand, systemCommand, sumCommand);
}
return 0;
}