-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceInvaders.cpp
576 lines (423 loc) · 11.8 KB
/
SpaceInvaders.cpp
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
Author : Gaurav Harchwani
Project Started : January 28th 2014
Project Completed : March 4th 2014
Project Name : SpaceInvaders : Save the earth from Metorite shower
*/
#include<allegro5\allegro.h>
#include<allegro5\allegro_primitives.h>
#include<allegro5\allegro_native_dialog.h>
#include"object.h"
#include<allegro5\allegro_font.h>
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_audio.h>
#include<allegro5\allegro_acodec.h>
#include<allegro5\allegro_image.h>
//Global Declarations
enum KEYS{UP,DOWN,LEFT,RIGHT,SPACE};
bool workdone = false;
const int width = 1366;
const int height = 768;
bool keys[5] = {false,false,false,false,false};
bool redraw = true;
float FPS = 60.0;
const int number_of_bullets = 15;
const int number_of_comets = 10;
bool is_game_over = false;
//Prototypes
void initShip(SpaceShip &ship);
void displayShip(SpaceShip &ship);
void moveShip(SpaceShip &ship);
void initBullets(Bullets bullets[],int number_of_bullets);
void displayBullets(Bullets bullets[],int number_of_bullets);
void fireBullets(Bullets bullets[],int number_of_bullets,SpaceShip &ship);
void updateBullets(Bullets bullets[],int number_of_bullets);
void initComets(Comets comets[],int number_of_comets);
void displayComets(Comets comets[],int number_of_comets);
void startComets(Comets comets[],int number_of_comets);
void updateComets(Comets comets[],int number_of_comets);
void collideBullet(Bullets bullets[],int number_of_bullets,Comets comets[],int number_of_comets,SpaceShip &ship);
void collideComet(Comets comets[],int number_of_comets,SpaceShip &ship);
void initBackground(float x,float y,float dirx,float diry,float velx,float vely,int background_width,int background_height,ALLEGRO_BITMAP *image,Background &bg);
void updateBackground(Background &bg);
void displayBackground(Background &bg,ALLEGRO_BITMAP *Background_image1);
void main()
{
//Initializing Display
ALLEGRO_DISPLAY *display = NULL;
//Font
ALLEGRO_FONT *catholic_girls = NULL;
ALLEGRO_FONT *font = NULL;
//Audio sample
ALLEGRO_SAMPLE *sample = NULL;
//Background Initialization
ALLEGRO_BITMAP *background_image1 = NULL;
ALLEGRO_BITMAP *background_image2 = NULL;
ALLEGRO_BITMAP *background_image3 = NULL;
if(al_init() == false )
{
al_show_native_message_box(NULL,"Allegro.exe","Error","Allegro Initialization Failed",NULL,NULL);
}
display = al_create_display(width,height);
if(display == false)
{
al_show_native_message_box(NULL,"Allegro.exe","Error","Allegro Display Failed",NULL,NULL);
}
//Adding Primitives
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_init_image_addon();
//Insatalling Keyboard
al_install_keyboard();
catholic_girls = al_load_font("bloodlust.ttf",30,NULL);
font = al_load_font("BlackWidow.ttf",30,NULL);
//Creating Event Queue
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
//Load image
background_image1 = al_load_bitmap("starBG.png");
background_image2 = al_load_bitmap("starFG.png");
background_image3 = al_load_bitmap("starMG.png");
//Load sample
sample = al_load_sample("binarysolo.ogg");
al_reserve_samples(1);
//Initializing Allegro Event timer
ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
//Registering Events
al_register_event_source(event_queue,al_get_keyboard_event_source());
al_register_event_source(event_queue,al_get_display_event_source(display));
al_register_event_source(event_queue,al_get_timer_event_source(timer));
//Structure Variable Created
SpaceShip ship;
Bullets bullets[15];
Comets comets[number_of_comets];
Background bg1;
Background bg2;
Background bg3;
//Initializing Ship
initShip(ship);
al_hide_mouse_cursor(display);
//Initializing and displaying Bullets
initBullets(bullets,number_of_bullets);
//Initialize Comets
initComets(comets,number_of_comets);
//Initialize background
initBackground(0,0,-1,0,1,0,800,600,background_image1,bg1);
initBackground(0,0,-1,0,1,0,1000,600,background_image2,bg2);
initBackground(0,0,-1,0,1,0,1600,600,background_image3,bg3);
//Starting Timer
al_start_timer(timer);
al_play_sample(sample,2,0,1,ALLEGRO_PLAYMODE_LOOP,NULL);
while(workdone == false)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = true;
fireBullets(bullets,number_of_bullets,ship);
break;
case ALLEGRO_KEY_ESCAPE:
workdone = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
break;
case ALLEGRO_KEY_ESCAPE:
workdone = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
workdone = true;
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
moveShip(ship);
updateBackground(bg1);
updateBackground(bg2);
updateBackground(bg3);
if(is_game_over == false && ship.lives != 0)
{
updateBullets(bullets,number_of_bullets);
startComets(comets,number_of_comets);
updateComets(comets,number_of_comets);
collideBullet(bullets,number_of_bullets,comets,number_of_comets,ship);
collideComet(comets,number_of_comets,ship);
}
}
if(redraw && al_is_event_queue_empty(event_queue) )
{
redraw = false;
if(is_game_over == false && ship.lives != 0)
{
displayShip(ship);
displayBullets(bullets,number_of_bullets);
displayComets(comets,number_of_comets);
displayBackground(bg1,background_image1);
displayBackground(bg2,background_image1);
displayBackground(bg3,background_image1);
al_draw_textf(catholic_girls,al_map_rgb(0,255,255),1300,5,ALLEGRO_ALIGN_RIGHT,"Lives %i ",ship.lives);
al_draw_textf(catholic_girls,al_map_rgb(0,255,255),30,5,ALLEGRO_ALIGN_LEFT,"Score %i ",ship.score);
al_draw_textf(font,al_map_rgb(0,255,255),1330,5,ALLEGRO_ALIGN_RIGHT,"%i",ship.lives);
al_draw_textf(font,al_map_rgb(0,255,255),300,5,ALLEGRO_ALIGN_LEFT,"%i",ship.score);
}
else
{
al_draw_textf(catholic_girls,al_map_rgb(0,255,255),width/2,height/2,ALLEGRO_ALIGN_CENTER,"Game Over. Your Score is %i ",ship.score);
al_draw_textf(font,al_map_rgb(0,255,255),1050,height/2,ALLEGRO_ALIGN_LEFT,"%i",ship.score);
}
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}
}
void initShip(SpaceShip &ship)
{
ship.ID = PLAYER ;
ship.lives = 3;
ship.score = 0;
ship.speed = 5;
ship.x = 20;
ship.y = height/2;
ship.xbound = 6;
ship.ybound = 7;
}
void displayShip(SpaceShip &ship)
{
al_draw_filled_rectangle(ship.x, ship.y - 9, ship.x + 10, ship.y - 7, al_map_rgb(255, 0, 0));
al_draw_filled_rectangle(ship.x, ship.y + 9, ship.x + 10, ship.y + 7, al_map_rgb(255, 0, 0));
al_draw_filled_triangle(ship.x - 12, ship.y - 17, ship.x +12, ship.y, ship.x - 12, ship.y + 17, al_map_rgb(0, 255, 0));
al_draw_filled_rectangle(ship.x - 12, ship.y - 2, ship.x +15, ship.y + 2, al_map_rgb(0, 0, 255));
}
void moveShip(SpaceShip &ship)
{
if(keys[UP] == true)
{
ship.y -= ship.speed;
if(ship.y < 0)
ship.y = 0;
}
if(keys[DOWN] == true)
{
ship.y += ship.speed;
if(ship.y > width )
ship.y = width;
}
if(keys[LEFT] == true)
{
ship.x -= ship.speed;
if(ship.x < 0)
ship.x = 0;
}
if(keys[RIGHT] == true)
{
ship.x += ship.speed;
if(ship.x > 600)
ship.x = 600;
}
}
void initBullets(Bullets bullets[],int number_of_bullets )
{
for(int i=0;i<15;i++)
{
bullets[i].ID = BULLET;
bullets[i].alive = false;
bullets[i].speed = 10;
}
}
void displayBullets(Bullets bullets[],int number_of_bullets )
{
for(int i=0;i<15;i++)
{
if(bullets[i].alive == true)
al_draw_filled_circle(bullets[i].x,bullets[i].y,4,al_map_rgb(rand(),rand(),rand()));
}
}
void fireBullets(Bullets bullets[],int number_of_bullets,SpaceShip &ship)
{
for(int i=0;i<15;i++)
{
if(bullets[i].alive == false)
{
bullets[i].x = ship.x + 17;
bullets[i].y = ship.y;
bullets[i].alive = true;
break;
}
}
}
void updateBullets(Bullets bullets[],int number_of_bullets)
{
for(int i=0;i<15;i++)
{
if(bullets[i].alive == true )
{
bullets[i].x += bullets[i].speed;
if(bullets[i].x > width)
bullets[i].alive = false;
}
}
}
void initComets(Comets comets[],int number_of_comets)
{
for(int i=0; i<number_of_comets; i++)
{
comets[i].alive = false;
comets[i].ID = ENEMY;
comets[i].speed = 5;
comets[i].xbound = 18;
comets[i].ybound = 18;
}
}
void displayComets(Comets comets[],int number_of_comets)
{
for(int i=0; i<number_of_comets; i++)
{
if(comets[i].alive == true )
{
al_draw_filled_circle(comets[i].x,comets[i].y,20,al_map_rgb(255,0,0));
}
}
}
void startComets(Comets comets[],int number_of_comets)
{
for(int i=0 ; i< number_of_comets ; i++)
{
if(comets[i].alive == false)
{
if(rand() % 300 == 0)
{
comets[i].alive = true;
comets[i].x = width + 20;
comets[i].y = (60 + rand()) % (768-30);
}
}
}
}
void updateComets(Comets comets[],int number_of_comets)
{
for(int i=0;i<number_of_bullets;i++)
{
if(comets[i].alive == true )
{
comets[i].x -= comets[i].speed;
}
}
}
void collideBullet(Bullets bullets[],int number_of_bullets,Comets comets[],int number_of_comets,SpaceShip &ship)
{
for(int i=0 ; i<number_of_bullets ; i++)
{
//Collision will happen only with live bullets
if(bullets[i].alive == true)
{
for(int j=0;j<number_of_comets ; j++)
{
if(comets[j].alive == true)
{
//Collision Detection Logic
if( bullets[i].x < (comets[j].x - comets[j].xbound) ||
bullets[i].x > (comets[j].x + comets[j].xbound) ||
bullets[i].y < (comets[j].y - comets[j].ybound) ||
bullets[i].y > (comets[j].y + comets[j].ybound) )
{
comets[j].alive = true;
}
else
{
comets[j].alive = false;
bullets[i].alive = false ;
ship.score++;
}
}
}
}
}
}
void collideComet(Comets comets[],int number_of_comets,SpaceShip &ship)
{
for(int i = 0 ; i<number_of_comets ; i++ )
{
if(comets[i].alive == true)
{
if( comets[i].x - comets[i].xbound < ship.x + ship.xbound &&
comets[i].x + comets[i].xbound > ship.x - ship.xbound &&
comets[i].y - comets[i].ybound < ship.y + ship.ybound &&
comets[i].y + comets[i].ybound > ship.y - ship.ybound)
{
ship.lives--;
comets[i].alive = false;
}
else if(comets[i].x < 0)
{
comets[i].alive = false;
}
}
}
}
void initBackground(float x,float y,float dirx,float diry,float velx,float vely,int background_width,int background_height,ALLEGRO_BITMAP *image,Background &bg)
{
bg.x = x;
bg.y = y;
bg.dirx = dirx;
bg.diry = diry;
bg.x_vel = velx;
bg.y_vel = vely;
bg.background_width = background_width;
bg.background_height = background_height;
bg.image = image;
}
void updateBackground(Background &bg)
{
bg.x += bg.x_vel * bg.dirx;
if(bg.x + bg.background_width <= 0 )
bg.x = 0;
}
void displayBackground(Background &bg_image,ALLEGRO_BITMAP *background_image)
{
al_draw_bitmap(bg_image.image,bg_image.x,bg_image.y,0);
al_draw_bitmap(background_image,bg_image.x,620,0);
if(bg_image.x + bg_image.background_width < width)
{
al_draw_bitmap(bg_image.image,bg_image.x + bg_image.background_width ,bg_image.y,0);
al_draw_bitmap(background_image,bg_image.x + bg_image.background_width,620,0);
}
}