-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
323 lines (288 loc) · 11.6 KB
/
main.cpp
File metadata and controls
323 lines (288 loc) · 11.6 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <windows.h>
#include <mmsystem.h>
#include <graphics.h>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <iostream>
#include "Blocks.h"
#include "Bullet.h"
#include "Alien.h"
#include "Canon.h"
#include "Text.h"
using namespace std;
class VectorContainer{
public:
vector<Bullet> bullets;
vector<Alien> aliens;
};
void moveAlien(Alien alien, boolean add,int x, int y, int type, bool &willBlink, int timer){
if (type == 0)
alien.drawRocketAlien(x, y, timer );
else if (type == 1)
alien.drawBlinkingAlien(x, y, willBlink);
//mciSendString("play .\\assets\\zzz.wav",NULL,0,NULL);
}
vector<Alien> addAlien(vector<Alien> aliens){
Alien alien;
int y = 10;
int x;
for(int j=0, ctr=0;j<2;j++){
int type = j%2;
x = 50;
for(int i=0; i<5;i++,ctr++){
alien.x = x;
alien.y = y;
alien.ctr = ctr;
alien.type = type;
aliens.push_back(alien);
x+=60;
}
y+=50;
}
return aliens;
}
vector<Bullet> checkBullets(vector<Bullet> bullets){
for(int b = 0; b<bullets.size();b++){
int bx1 = bullets[b].x - 2;
int by1 = bullets[b].y - 5;
int bx2 = bullets[b].x + 2;
int by2 = bullets[b].y - 5;
if(bx1 < 0 || bx1 > 640 || by1 < 0 || by2 < 0){
bullets.erase (bullets.begin()+b);
}
}
return bullets;
}
VectorContainer checkCollision(VectorContainer vectorContainer, int &score, int &bullets_left){
for(int b = 0; b<vectorContainer.bullets.size();b++){
int newX1 = vectorContainer.bullets[b].x - 5;
int newY1 = vectorContainer.bullets[b].y;
int newX2 = vectorContainer.bullets[b].x + 5;
int newY2 = vectorContainer.bullets[b].y;
int newX3 = vectorContainer.bullets[b].x - 5;
int newY3 = vectorContainer.bullets[b].y + 10;
int newX4 = vectorContainer.bullets[b].x + 5;
int newY4 = vectorContainer.bullets[b].y + 10;
int bx1 = cos(vectorContainer.bullets[b].angle) * (newX1 - vectorContainer.bullets[b].x) - sin(vectorContainer.bullets[b].angle) * (newY1 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].x;
int by1 = sin(vectorContainer.bullets[b].angle) * (newX1 - vectorContainer.bullets[b].x) + cos(vectorContainer.bullets[b].angle) * (newY1 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].y;
int bx2 = cos(vectorContainer.bullets[b].angle) * (newX2 - vectorContainer.bullets[b].x) - sin(vectorContainer.bullets[b].angle) * (newY2 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].x;
int by2 = sin(vectorContainer.bullets[b].angle) * (newX2 - vectorContainer.bullets[b].x) + cos(vectorContainer.bullets[b].angle) * (newY2 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].y;
int bx3 = cos(vectorContainer.bullets[b].angle) * (newX3 - vectorContainer.bullets[b].x) - sin(vectorContainer.bullets[b].angle) * (newY3 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].x;
int by3 = sin(vectorContainer.bullets[b].angle) * (newX3 - vectorContainer.bullets[b].x) + cos(vectorContainer.bullets[b].angle) * (newY3 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].y;
int bx4 = cos(vectorContainer.bullets[b].angle) * (newX4 - vectorContainer.bullets[b].x) - sin(vectorContainer.bullets[b].angle) * (newY4 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].x;
int by4 = sin(vectorContainer.bullets[b].angle) * (newX4 - vectorContainer.bullets[b].x) + cos(vectorContainer.bullets[b].angle) * (newY4 - vectorContainer.bullets[b].y) + vectorContainer.bullets[b].y;
for(int a = 0; a<vectorContainer.aliens.size();a++){
int ax1 = vectorContainer.aliens[a].x - 10;
int ay1 = vectorContainer.aliens[a].y + 0;
int ax2 = vectorContainer.aliens[a].x + 40;
int ay2 = vectorContainer.aliens[a].y + 0;
int ax3 = vectorContainer.aliens[a].x - 10;
int ay3 = vectorContainer.aliens[a].y + 40;
int ax4 = vectorContainer.aliens[a].x + 40;
int ay4 = vectorContainer.aliens[a].y + 40;
if((ax1 <= bx1 && ax2 >= bx1 && ax3 <= bx1 && ax4 >= bx1 && ay1 <= by1 && ay2 <= by1 && ay3 >= by1 && ay4 >= by1) ||
(ax1 <= bx2 && ax2 >= bx2 && ax3 <= bx2 && ax4 >= bx2 && ay1 <= by2 && ay2 <= by2 && ay3 >= by2 && ay4 >= by2) ||
(ax1 <= bx3 && ax2 >= bx3 && ax3 <= bx3 && ax4 >= bx3 && ay1 <= by3 && ay2 <= by3 && ay3 >= by3 && ay4 >= by3) ||
(ax1 <= bx4 && ax2 >= bx4 && ax3 <= bx4 && ax4 >= bx4 && ay1 <= by4 && ay2 <= by4 && ay3 >= by4 && ay4 >= by4)){
vectorContainer.aliens.erase (vectorContainer.aliens.begin()+a);
vectorContainer.bullets.erase (vectorContainer.bullets.begin()+b);
//mciSendString("play .\\assets\\destroyed.wav",NULL,0,NULL);
score++;
if(bullets_left !=5){
bullets_left++;
}
}
}
}
return vectorContainer;
}
boolean checkisGameOver(vector<Alien> aliens){
for(int a=0;a<aliens.size();a++) {
int ay3 = aliens[a].y + 40;
int ay4 = aliens[a].y + 40;
if(ay3 >= 425 || ay4 >= 425){
Sleep(2*1000);
return true;
}
}
return false;
}
void showResults(int page, boolean isStageComplete){
cleardevice();
setcolor(11);
// mciSendString("stop .\\assets\\zzz.wav",NULL,0,NULL);
//mciSendString("close .\\assets\\zzz.wav",NULL,0,NULL);
if(isStageComplete){
outtextxy(10, 10, "Get ready to next level!");
}
else{
outtextxy(10, 10, "Game over! Starting again!");
//mciSendString("play .\\assets\\over.wav",NULL,0,NULL);
Sleep(500);
// mciSendString("close .\\assets\\over.wav",NULL,0,NULL);
Sleep(1500);
}
setactivepage(page);
setvisualpage(1-page);
}
void startGame(int page, int &level, int &score, double &inc){
VectorContainer vectorContainer;
vectorContainer.aliens = addAlien(vectorContainer.aliens);
Canon canon;
boolean add = true, isGameOver = false, isStageComplete = false, isAllowedToShot = true;
double k = .07 - inc, angle = 0;
int timer = 0, bullets_left = 5;
char msg[12], msg2[12], msg3[18];
bool willBlink = true;
int windowWidth = getwindowwidth();
int windowHeight = getwindowheight();
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 1);
//----GAME LOOP----------
for(int i=0, j=0, b=0;!isGameOver;i+=10,b++){
setactivepage(page);
setvisualpage(1-page);
cleardevice();
if (j>=250){ // first zone
k = 0.03 - inc ;
} else if(j>=150){ // second zone
k = 0.05 - inc;
}
if (i == 300 && add){
i = 10;
j = j + 10;
add = false;
} else if (i == 310 && !add){
i = 0;
j = j + 10;
add = true;
}
if (angle<60 && GetAsyncKeyState( VK_RIGHT ) & 0x8000){
angle+=3;
} else if (angle>-60 && GetAsyncKeyState( VK_LEFT ) & 0x8000){
angle-=3;
}
if (GetAsyncKeyState( VK_SPACE ) & 0x8000 && b>8 && isAllowedToShot && bullets_left != 0){
//mciSendString("play .\\assets\\player_shot.wav",NULL,0,NULL);
Bullet bullet;
bullet.x = 330;
bullet.y = 460;
bullet.angle = (angle*3.1428)/180;
vectorContainer.bullets.push_back(bullet);
b = 0;
bullets_left--;
isAllowedToShot = false;
}
for(int ctr=0;ctr<vectorContainer.aliens.size();ctr++){
if(add){
vectorContainer.aliens[ctr].x = 50 + (vectorContainer.aliens[ctr].ctr%5*60);
vectorContainer.aliens[ctr].x = vectorContainer.aliens[ctr].x + i;
} else {
vectorContainer.aliens[ctr].x = 350 + (vectorContainer.aliens[ctr].ctr%5*60);
vectorContainer.aliens[ctr].x = vectorContainer.aliens[ctr].x - i;
}
vectorContainer.aliens[ctr].y = vectorContainer.aliens[ctr].type*50 + j;
moveAlien(vectorContainer.aliens[ctr],add,vectorContainer.aliens[ctr].x, vectorContainer.aliens[ctr].y, vectorContainer.aliens[ctr].type, willBlink, timer);
}
if( timer == 17 ) {
isAllowedToShot = true;
willBlink = !willBlink;
timer = 0;
}
// user
setcolor(11);
canon.drawPlatform(310,455);
canon.drawCanon(330,460, angle);
for(int ctr=0;ctr<vectorContainer.bullets.size();ctr++){
vectorContainer.bullets[ctr].rotateBullet(vectorContainer.bullets[ctr].x,vectorContainer.bullets[ctr].y,vectorContainer.bullets[ctr].angle);
vectorContainer.bullets[ctr].y -= cos(vectorContainer.bullets[ctr].angle)*10;
vectorContainer.bullets[ctr].x += sin(vectorContainer.bullets[ctr].angle)*10;
}
vectorContainer = checkCollision(vectorContainer, score, bullets_left);
sprintf(msg2, "Level: %d", level);
sprintf(msg, "Score: %d", score);
sprintf(msg3, "Bullets Left: %d", bullets_left);
outtextxy(10, windowHeight - 80, msg);
outtextxy(10,10, msg2);
outtextxy(10, windowHeight - 60, msg3);
vectorContainer = vectorContainer;
vectorContainer.bullets = checkBullets(vectorContainer.bullets);
if (vectorContainer.aliens.size()==0){
isStageComplete = true;
level++; // added value
isGameOver = true;
inc += 0.001;
}
if(checkisGameOver(vectorContainer.aliens) || bullets_left==0){
isGameOver = true;
level = 1;
score = 0;
}
page = 1 - page;
Sleep(k*1000);
if(isGameOver){
break;
}
timer++;
}
showResults(page,isStageComplete);
}
int main(){
initwindow(640, 480, "CMSC 178: Game", GetSystemMetrics(SM_CXSCREEN)/6, GetSystemMetrics(SM_CYSCREEN)/8 );
bool isRunning = true;
int level = 1, score = 0, pointer = 210;
double inc = 0.0;
Text text;
//INITIAL RENDER
text.drawPointer(pointer);
text.drawMainMenu();
while(isRunning) {
if(GetAsyncKeyState( VK_DOWN )) {
//pointer == 300 ? 210 : pointer+=30;
if(pointer == 300) {
pointer = 210;
} else {
pointer+=30;
}
text.drawPointer(pointer);
text.drawMainMenu();
}
if(GetAsyncKeyState( VK_UP )) {
//pointer == 210 ? 300 : pointer-=30;
if(pointer == 210) {
pointer = 300;
} else {
pointer-=30;
}
text.drawPointer(pointer);
text.drawMainMenu();
}
if(GetAsyncKeyState( VK_ESCAPE )){
text.drawPointer(pointer);
text.drawMainMenu();
}
if(GetAsyncKeyState( VK_RETURN)) {
switch (pointer) {
case 210:
startGame(0, level, score, inc);
break;
case 240:
startGame(0, level, score, inc);
break;
case 270:
cleardevice();
text.drawHowToPlay();
break;
case 300:
isRunning = false;
break;
default:
std::cout << "Unknown selection" << endl;
}
}
Sleep(100);
}
getch();
closegraph();
return 0;
}