-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceWar.cpp
More file actions
268 lines (207 loc) · 5.91 KB
/
SpaceWar.cpp
File metadata and controls
268 lines (207 loc) · 5.91 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#define CONSOLE_GAME_ENGINE_IMPLEMENTATION
#include "ConsoleGameEngine.hpp"
#include <list>
using namespace std;
class Space : public ConsoleGameEngine
{
public:
Space()
{
sAppName = L"Space In War";
}
int nPlayerWidth;
int nPlayerHeight;
wstring sPlayerModel;
int nEnemyWidth;
int nEnemyHeight;
wstring sEnemyModel;
struct Object
{
pair<float, float> pos;
bool bRedundant = false;
};
list<Object> listEnemies;
list<Object> listProjectiles;
float fPlayerX;
float fPlayerY;
int nScore;
bool bGameOver;
float fSpeed;
vector<wstring> vPatterns;
int nCurrentPattern;
protected:
bool OnUserCreate() override
{
srand(time(NULL));
nPlayerWidth = 9;
nPlayerHeight = 7;
sPlayerModel += L"<-----> ";
sPlayerModel += L" | ";
sPlayerModel += L" @@@\\ ";
sPlayerModel += L"-###=) ";
sPlayerModel += L" @@@/ ";
sPlayerModel += L" | ";
sPlayerModel += L"<-----> ";
nEnemyWidth = 9;
nEnemyHeight = 5;
sEnemyModel += L"<------->";
sEnemyModel += L" | ";
sEnemyModel += L" <#####=-";
sEnemyModel += L" | ";
sEnemyModel += L"<------->";
wstring sPattern;
sPattern = L".........";
sPattern += L".........";
sPattern += L".........";
sPattern += L"#........";
sPattern += L".........";
sPattern += L".........";
sPattern += L".........";
vPatterns.push_back(sPattern);
sPattern = L".........";
sPattern += L".........";
sPattern += L"#........";
sPattern += L".........";
sPattern += L"#........";
sPattern += L".........";
sPattern += L".........";
vPatterns.push_back(sPattern);
sPattern = L".........";
sPattern += L".........";
sPattern += L"..#......";
sPattern += L"#........";
sPattern += L"..#......";
sPattern += L".........";
sPattern += L".........";
vPatterns.push_back(sPattern);
sPattern = L".........";
sPattern += L"....#....";
sPattern += L"..#......";
sPattern += L"#...#....";
sPattern += L"..#......";
sPattern += L"....#....";
sPattern += L".........";
vPatterns.push_back(sPattern);
fSpeed = 20.0f;
Restart();
return true;
}
void Restart()
{
listEnemies.clear();
listProjectiles.clear();
for (int i = 0; i < 3; i++)
AddEnemy();
fPlayerX = ScreenWidth() / 4;
fPlayerY = ScreenHeight() / 2 - nPlayerHeight / 2;
nScore = 0;
nCurrentPattern = 0;
bGameOver = false;
}
void AddEnemy()
{
listEnemies.push_back({ make_pair<float, float>(
ScreenWidth() + nEnemyWidth + (rand() % (nEnemyWidth * 2)),
RandInt(nEnemyHeight, ScreenHeight() - nEnemyHeight)
) });
}
int RandInt(int min, int max)
{
return min + rand() % (max - min);
}
bool RectVsRect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2)
{
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
}
bool RectVsPoint(float x1, float y1, float w, float h, float x2, float y2)
{
return x1 <= x2 && x2 < x1 + w && y1 <= y2 && y2 < y1 + h;
}
void AddProjectiles(size_t i)
{
wstring& p = vPatterns[i];
for (int i = 0; i < nPlayerWidth; i++)
for (int j = 0; j < nPlayerHeight; j++)
{
if (p[j * nPlayerWidth + i] == '#')
{
listProjectiles.push_back({ {
fPlayerX + float(nPlayerWidth + i),
fPlayerY + float(j)
} });
}
}
}
bool OnUserUpdate(float fDeltaTime) override
{
if (bGameOver)
{
if (GetKey(VK_SPACE).bPressed)
Restart();
Clear(PIXEL_SOLID, FG_BLACK);
std::wstring sMsg = L"Game Over! Score: " + to_wstring(nScore) + L". Press SPACE to play again.";
DrawString(ScreenWidth() / 2 - sMsg.length() / 2, ScreenHeight() / 2, sMsg, FG_WHITE);
return true;
}
if (GetKey(VK_UP).bHeld) fPlayerY -= fSpeed * fDeltaTime;
if (GetKey(VK_DOWN).bHeld) fPlayerY += fSpeed * fDeltaTime;
if (GetKey(VK_RETURN).bPressed) ++nCurrentPattern %= vPatterns.size();
if (GetKey(VK_SPACE).bPressed)
AddProjectiles(nCurrentPattern);
for (auto& p : listEnemies)
{
p.pos.first -= fSpeed * fDeltaTime;
if (RectVsRect(p.pos.first, p.pos.second, nEnemyWidth, nEnemyHeight, fPlayerX, fPlayerY, nPlayerWidth, nPlayerHeight))
bGameOver = true;
}
for (auto& proj : listProjectiles)
{
proj.pos.first += 3.0f * fSpeed * fDeltaTime;
proj.bRedundant = (proj.pos.first >= ScreenWidth());
for (auto& enemy : listEnemies)
{
enemy.bRedundant = (enemy.pos.first <= -nEnemyWidth);
if (RectVsPoint(enemy.pos.first, enemy.pos.second, nEnemyWidth, nEnemyHeight, proj.pos.first, proj.pos.second))
{
proj.bRedundant = enemy.bRedundant = true;
AddEnemy();
nScore++;
}
}
}
listProjectiles.remove_if([](const Object& o) { return o.bRedundant; });
listEnemies.remove_if([](const Object& o) { return o.bRedundant; });
auto itRemove = remove_if(listEnemies.begin(), listEnemies.end(), [&](const Object& o) { return o.pos.first <= -nEnemyWidth; });
if (itRemove != listEnemies.end())
{
listEnemies.erase(itRemove);
AddEnemy();
nScore--;
}
listProjectiles.remove_if([&](const Object& o) { return o.pos.first >= ScreenWidth(); });
if (nScore < 0)
bGameOver = true;
Clear(PIXEL_SOLID, FG_BLACK);
for (const auto& p : listProjectiles)
Draw(p.pos.first, p.pos.second, PIXEL_SOLID, FG_DARK_YELLOW);
auto draw_ship = [&](int sx, int sy, int width, int height, const std::wstring& model)
{
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
Draw(sx + x, sy + y, model[y * width + x], FG_GREY);
};
draw_ship((int)fPlayerX, (int)fPlayerY, nPlayerWidth, nPlayerHeight, sPlayerModel);
for (const auto& p : listEnemies)
draw_ship(p.pos.first, p.pos.second, nEnemyWidth, nEnemyHeight, sEnemyModel);
DrawString(1, 1, L"Score: " + to_wstring(nScore) + L". Pattern: " + to_wstring(nCurrentPattern), FG_WHITE);
//DrawString(1, 2, L"Projectiles: " + to_wstring(listProjectiles.size()), FG_WHITE);
return true;
}
};
int main()
{
Space demo;
if (demo.ConstructConsole(120, 40, 16, 16) == RC_OK)
demo.Run();
return 0;
}