1#include <allegro5\allegro.h>
2#include <allegro5\allegro_primitives.h>
3#include <allegro5\allegro_image.h>
4#include <allegro5\allegro_ttf.h>
5#include <allegro5\allegro_font.h>
6#include "objects.h"
7
8// Buttons
9enum BUTON
{A,S,D,W,SPACE
};
10bool buton
[5] = {false,
false,
false,
false,
false};
11
12// Globals
13const int FPS
= 80, width
= 1000, height
= 500;
14const int NUM_BULLETS
= 50;
15const int NUM_COMETS
= 10;
16const int NUM_COMETS1
= 2;
17int x
= width
/ 2;
18int y
= height
/ 2;
19
20// Protorypes
21void InitShip
(SpaceShip
&ship
);
22void DrawShip
(SpaceShip
&ship
);
23void MoveShipUp
(SpaceShip
&ship
);
24void MoveShipDown
(SpaceShip
&ship
);
25void MoveShipLeft
(SpaceShip
&ship
);
26void MoveShipRight
(SpaceShip
&ship
);
27
28void InitBullet
(Bullet bullet
[],
int size
);
29void DrawBullet
(Bullet bullet
[],
int size
);
30void FireBullet
(Bullet bullet
[],
int size, SpaceShip
&ship
);
31void UpdateBullet
(Bullet bullet
[],
int size
);
32void CollideBullet
(Bullet bullet
[],
int bSize, Comet comets
[],
int cSize, SpaceShip
&ship
);
33void CollideBullet1
(Bullet bullet
[],
int bSize, Comet1 comets
[],
int cSize, SpaceShip
&ship
);
34
35void InitComet
(Comet comets
[],
int size
);
36void DrawComet
(Comet comet
[],
int size
);
37void StartComet
(Comet comet
[],
int size
);
38void UpdateComet
(Comet comet
[],
int size
);
39void CollideComet
(Comet comets
[],
int cSize, SpaceShip
&ship
);
40
41void InitComet1
(Comet1 comets1
[],
int size
);
42void DrawComet1
(Comet1 comet1
[],
int size
);
43void StartComet1
(Comet1 comet1
[],
int size
);
44void UpdateComet1
(Comet1 comet1
[],
int size
);
45void CollideComet1
(Comet1 comets1
[],
int cSize, SpaceShip
&ship
);
46
47int main
()
48{
49 // Initialization
50 al_init();
51 al_init_primitives_addon();
52 al_init_image_addon();
53 al_init_font_addon();
54 al_init_ttf_addon();
55
56 // Installations
57 al_install_keyboard();
58 al_install_mouse();
59
60 // Variables
61 SpaceShip ship
;
62 Bullet bullets
[NUM_BULLETS
];
63 Comet comets
[NUM_COMETS
];
64 Comet1 comets1
[NUM_COMETS1
];
65
66 // I don't think you are actually using this variable
67 int fireCooldown
= 0;
68
69 bool quit
= false;
70 bool draw
= true;
71 bool GameOver
= false;
72
73 // ALLEGRO variables
74 ALLEGRO_DISPLAY *window = al_create_display(width, height
);
75 ALLEGRO_BITMAP *icon
= al_load_bitmap("image/1.png");
76 ALLEGRO_FONT *font16
= al_load_ttf_font("fonts/1.ttf",
80, ALLEGRO_ALIGN_CENTRE
);
77 ALLEGRO_TIMER *timer
= NULL
;
78 ALLEGRO_EVENT_QUEUE *event
= NULL
;
79
80 event
= al_create_event_queue();
81 timer
= al_create_timer(1.
0 / FPS
);
82
83 // Events
84 al_register_event_source(event,
al_get_keyboard_event_source());
85 al_register_event_source(event,
al_get_mouse_event_source());
86 al_register_event_source(event,
al_get_display_event_source(window));
87 al_register_event_source(event,
al_get_timer_event_source(timer
));
88
89 // Window title, icon and mouse hide
90 al_set_window_title(window,
"Cigarette Life");
91 al_set_display_icon(window, icon
);
92 al_hide_mouse_cursor(window);
93
94 // Player initializations
95 srand(time(NULL
));
96 InitShip
(ship
);
97 InitBullet
(bullets, NUM_BULLETS
);
98 InitComet
(comets, NUM_COMETS
);
99
100 // Start timer
101 al_start_timer(timer
);
102
103
104 // The drawing
105 while(!quit
)
106 {
107 // Call of the event
108 ALLEGRO_EVENT e
;
109 al_wait_for_event(event,
&e
);
110
111 // Drawing
112 if(e.type
== ALLEGRO_EVENT_TIMER
)
113 {
114 draw
= true;
115 if(ship.score
>= 100)
116 {
117 InitComet1
(comets1, NUM_COMETS1
);
118 }
119 if(buton
[A
])
120 MoveShipLeft
(ship
);
121 if(buton
[D
])
122 MoveShipRight
(ship
);
123 if(buton
[S
])
124 MoveShipDown
(ship
);
125 if(buton
[W
])
126 MoveShipUp
(ship
);
127
128 // Why didn't you apply the same logic to firing your bullets?
129 if(!GameOver
)
130 {
131 UpdateBullet
(bullets, NUM_BULLETS
);
132 StartComet
(comets, NUM_COMETS
);
133 UpdateComet
(comets, NUM_COMETS
);
134 StartComet1
(comets1, NUM_COMETS1
);
135 UpdateComet1
(comets1, NUM_COMETS1
);
136 CollideBullet
(bullets, NUM_BULLETS, comets, NUM_COMETS, ship
);
137 CollideComet
(comets, NUM_COMETS, ship
);
138 CollideBullet1
(bullets, NUM_BULLETS, comets1, NUM_COMETS, ship
);
139 CollideComet1
(comets1, NUM_COMETS1, ship
);
140
141 if(ship.lives
<= 0)
142 {
143 GameOver
= true;
144 }
145 }
146
147 if(buton
[SPACE
])
148 {
149 // also it's button, you got it right in the comments though.
150 // Or is this a different language?
151 if(fireCooldown
== 0)
152 {
153 // We'll decrement this variable later
154 // This block will only get executed if fireCooldown equals 0
155 fireCooldown
= 25;
156 // Now that we've assigned 10 to fireCooldown we can be sure the next bullet is not going to be fired for a couple of frames
157 FireBullet
(bullets, NUM_BULLETS, ship
);
158 }
159 }
160
161 // Let's get the ship ready for the next shot
162 if(fireCooldown
> 0)
163 fireCooldown--
;
164 }
165
166 // Key down or up
167 switch(e.type
)
168 {
169 case ALLEGRO_EVENT_KEY_DOWN:
170 switch(e.keyboard.keycode
)
171 {
172 case ALLEGRO_KEY_A:
173 buton
[A
] = true;
174 break;
175
176 case ALLEGRO_KEY_S:
177 buton
[S
] = true;
178 break;
179
180 case ALLEGRO_KEY_D:
181 buton
[D
] = true;
182 break;
183
184 case ALLEGRO_KEY_W:
185 buton
[W
] = true;
186 break;
187
188 case ALLEGRO_KEY_P:
189 GameOver
= !GameOver
;
190 break;
191
192 case ALLEGRO_KEY_SPACE:
193 buton
[SPACE
] = true;
194 // Now you are going to fire a bullet each time you press your spacebar, so let's move this out of here
195 // FireBullet(bullets, NUM_BULLETS, ship);
196 break;
197
198 case ALLEGRO_KEY_ESCAPE:
199 quit
= true;
200 break;
201 }
202 }
203
204 switch(e.type
)
205 {
206 case ALLEGRO_EVENT_KEY_UP:
207 switch(e.keyboard.keycode
)
208 {
209 case ALLEGRO_KEY_A:
210 buton
[A
] = false;
211 break;
212
213 case ALLEGRO_KEY_S:
214 buton
[S
] = false;
215 break;
216
217 case ALLEGRO_KEY_D:
218 buton
[D
] = false;
219 break;
220
221 case ALLEGRO_KEY_W:
222 buton
[W
] = false;
223 break;
224
225 case ALLEGRO_KEY_SPACE:
226 buton
[SPACE
] = false;
227 break;
228 }
229 }
230
231
232 // Close display when X is pressed
233 if(e.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
234 {
235 quit
= true;
236 }
237
238 // Mouse movement
239 else if(e.type
== ALLEGRO_EVENT_MOUSE_AXES
)
240 {
241 ship.y
= e.mouse.y
;
242 }
243
244 // Bad idea, an ALLEGRO_EVENT is an union
245 // Meaning that multiple different members can take the same place in memory
246 // Other events might 'overwrite' e.mouse.button, causing it to falsely evaluate to true and this is more likely than you might think as it caused the firing to seem irregular with this not being commented
247 /*else if(e.mouse.button & 1)
248 {
249 FireBullet(bullets, NUM_BULLETS, ship);
250 }*/
251 if(e.type
== ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
)
252 {
253 if(e.mouse.button
& 1)
254 {
255 buton
[SPACE
] = true; // Ugly hack but it works
256 }
257 }
258 else if(e.type
== ALLEGRO_EVENT_MOUSE_BUTTON_UP
)
259 {
260 if(e.mouse.button
& 1)
261 {
262 buton
[SPACE
] = false;
263 }
264 }
265
266 if(draw
&& al_event_queue_is_empty
(event
))
267 {
268 draw
= false;
269 if(!GameOver
)
270 {
271
272 DrawShip
(ship
);
273 DrawBullet
(bullets, NUM_BULLETS
);
274 DrawComet
(comets, NUM_COMETS
);
275 DrawComet1
(comets1, NUM_COMETS1
);
276
277 al_draw_textf(font16,
al_map_rgb(255,
0,
0), x
+ 300,
10, ALLEGRO_ALIGN_CENTRE,
"Lifes: %i Points %i", ship.lives, ship.score
);
278 }
279 else
280 {
281 al_draw_textf(font16,
al_map_rgb(255,
0,
0), x,
100, ALLEGRO_ALIGN_CENTRE,
"Game Over");
282 al_draw_textf(font16,
al_map_rgb(255,
0,
0), x,
200, ALLEGRO_ALIGN_CENTRE,
"Final Score: %i", ship.score
);
283 }
284
285 al_flip_display();
286 al_clear_to_color(al_map_rgb(0,
0,
0));
287 }
288
289 }
290
291 // Destroy
292 al_destroy_event_queue(event
);
293 al_destroy_bitmap(icon
);
294 al_destroy_timer(timer
);
295 al_destroy_display(window);
296}
297
298void InitShip
(SpaceShip
&ship
)
299{
300 ship.x
= 20;
301 ship.y
= height
/ 2;
302 ship.ID
= PLAYER
;
303 ship.lives
= 3;
304 ship.speed
= 7;
305 ship.boundx
= 126;
306 ship.boundy
= 0;
307 ship.score
= 0;
308}
309
310void DrawShip
(SpaceShip
&ship
)
311{
312 al_draw_filled_rectangle(ship.x
- 10, ship.y-15, ship.x
+ 35, ship.y,
al_map_rgb(255,
128,
0));
313 al_draw_filled_rectangle(ship.x
+ 35, ship.y-15, ship.x
+ 130, ship.y,
al_map_rgb(255,
255,
255));
314 al_draw_line(ship.x
+ 30, ship.y-15, ship.x
+ 30, ship.y,
al_map_rgb(255,
255,
50),
2.
0);
315}
316
317void MoveShipUp
(SpaceShip
&ship
)
318{
319 ship.y
-= ship.speed
;
320 if(ship.y
< 15)
321 ship.y
= 15;
322}
323
324void MoveShipDown
(SpaceShip
&ship
)
325{
326 ship.y
+= ship.speed
;
327 if(ship.y
> height
)
328 ship.y
= height
;
329}
330
331void MoveShipLeft
(SpaceShip
&ship
)
332{
333 ship.x
-= ship.speed
;
334 if(ship.x
< 10)
335 ship.x
= 10;
336}
337
338void MoveShipRight
(SpaceShip
&ship
)
339{
340 ship.x
+= ship.speed
;
341 if(ship.x
> 100)
342 ship.x
= 100;
343}
344
345void InitBullet
(Bullet bullet
[],
int size
)
346{
347 for(int i
= 0; i
< size
; i
++)
348 {
349 bullet
[i
].ID
= BULLET
;
350 bullet
[i
].speed
= 10;
351 bullet
[i
].live
= false;
352 }
353}
354
355void DrawBullet
(Bullet bullet
[],
int size
)
356{
357 for(int i
= 0; i
< size
; i
++)
358 {
359 if(bullet
[i
].live
)
360 al_draw_filled_circle(bullet
[i
].x, bullet
[i
].y,
5,
al_map_rgb(128,
128,
128));
361 }
362}
363
364void FireBullet
(Bullet bullet
[],
int size, SpaceShip
&ship
)
365{
366 for(int i
= 0; i
< size
; i
++)
367 {
368 if(!bullet
[i
].live
)
369 {
370 bullet
[i
].x
= ship.x
+ 120;
371 bullet
[i
].y
= ship.y
- 7;
372 bullet
[i
].live
= true;
373 break;
374 }
375 }
376}
377
378void UpdateBullet
(Bullet bullet
[],
int size
)
379{
380 for(int i
= 0; i
< size
; i
++)
381 {
382 if(bullet
[i
].live
)
383 {
384 bullet
[i
].x
+= bullet
[i
].speed
;
385 if(bullet
[i
].x
> width
)
386 {
387 bullet
[i
].live
= false;
388 break;
389 }
390 }
391 }
392}
393
394void CollideBullet
(Bullet bullet
[],
int bSize, Comet comets
[],
int cSize, SpaceShip
&ship
)
395{
396 for(int i
= 0; i
< bSize
; i
++)
397 {
398 if(bullet
[i
].live
)
399 {
400 for(int j
= 0; j
< cSize
; j
++)
401 {
402 if(comets
[j
].live
> 0)
403 {
404 if(bullet
[i
].x
> (comets
[j
].x
- comets
[j
].boundx
) &&
405 bullet
[i
].x
< (comets
[j
].x
+ comets
[j
].boundx
) &&
406 bullet
[i
].y
> (comets
[j
].y
- comets
[j
].boundy
) &&
407 bullet
[i
].y
< (comets
[j
].y
+ comets
[j
].boundy
))
408 {
409 bullet
[i
].live
= false;
410 comets
[j
].live
-= 1;
411 ship.score
+= 5;
412 }
413 }
414 }
415 }
416 }
417}
418
419void CollideBullet1
(Bullet bullet
[],
int bSize, Comet1 comets1
[],
int cSize, SpaceShip
&ship
)
420{
421 for(int i
= 0; i
< bSize
; i
++)
422 {
423 if(bullet
[i
].live
)
424 {
425 for(int j
= 0; j
< cSize
; j
++)
426 {
427 if(comets1
[j
].live
> 0)
428 {
429 if(bullet
[i
].x
> (comets1
[j
].x
- comets1
[j
].boundx
) &&
430 bullet
[i
].x
< (comets1
[j
].x
+ comets1
[j
].boundx
) &&
431 bullet
[i
].y
> (comets1
[j
].y
- comets1
[j
].boundy
) &&
432 bullet
[i
].y
< (comets1
[j
].y
+ comets1
[j
].boundy
))
433 {
434 bullet
[i
].live
= false;
435 comets1
[j
].live
-= 1;
436 if(comets1
[j
].live
== 0)
437 {
438 ship.score
+= 25;
439 }
440 }
441 }
442 }
443 }
444 }
445}
446
447void InitComet
(Comet comets
[],
int size
)
448{
449 for(int i
= 0; i
< size
; i
++)
450 {
451 comets
[i
].ID
= ENEMY
;
452 comets
[i
].live
= 2;
453 comets
[i
].speed
= 2;
454 comets
[i
].boundx
= 20;
455 comets
[i
].boundy
= 30;
456 }
457}
458
459void DrawComet
(Comet comets
[],
int size
)
460{
461 for(int i
= 0; i
< size
; i
++)
462 {
463 if(comets
[i
].live
> 0)
464 {
465 al_draw_filled_ellipse(comets
[i
].x, comets
[i
].y,
20,
30,
al_map_rgb(255,
0,
0));
466 al_draw_ellipse(comets
[i
].x, comets
[i
].y,
10,
20,
al_map_rgb(0,
0,
0),
5.
0);
467 }
468 }
469}
470
471void StartComet
(Comet comets
[],
int size
)
472{
473 for(int i
= 0; i
< size
; i
++)
474 {
475 if(comets
[i
].live
== 0)
476 {
477 if(rand() %
500 == 0)
478 {
479 comets
[i
].live
+= 1;
480 comets
[i
].x
= width
;
481 comets
[i
].y
= 30 + rand() %
(height
- 60);
482 break;
483 }
484 }
485 }
486}
487
488void UpdateComet
(Comet comets
[],
int size
)
489{
490 for(int i
= 0; i
< size
; i
++)
491 {
492 if(comets
[i
].live
> 0)
493 {
494 comets
[i
].x
-= comets
[i
].speed
;
495
496 if(comets
[i
].x
< 0)
497 {
498 comets
[i
].live
= 0;
499 }
500 }
501 }
502}
503
504void CollideComet
(Comet comets
[],
int cSize, SpaceShip
&ship
)
505{
506 for(int i
= 0; i
< cSize
; i
++)
507 {
508 if(comets
[i
].live
> 0)
509 {
510 if(comets
[i
].x
- comets
[i
].boundx
< ship.x
+ ship.boundx
&&
511 comets
[i
].x
+ comets
[i
].boundx
> ship.x
- ship.boundx
&&
512 comets
[i
].y
- comets
[i
].boundy
< ship.y
+ ship.boundy
&&
513 comets
[i
].y
+ comets
[i
].boundy
> ship.y
- ship.boundy
)
514 {
515 ship.lives--
;
516 comets
[i
].live
= 0;
517 }
518 else if(comets
[i
].x
< 10)
519 {
520 ship.lives--
;
521 comets
[i
].live
= 0;
522 }
523 }
524 }
525}
526
527
528void InitComet1
(Comet1 comets1
[],
int size
)
529{
530 for(int i
= 0; i
< size
; i
++)
531 {
532 comets1
[i
].ID
= ENEMY
;
533 comets1
[i
].live
= 5;
534 comets1
[i
].speed
= 1;
535 comets1
[i
].boundx
= 20;
536 comets1
[i
].boundy
= 30;
537 }
538}
539
540void DrawComet1
(Comet1 comets1
[],
int size
)
541{
542 for(int i
= 0; i
< size
; i
++)
543 {
544 if(comets1
[i
].live
> 0)
545 {
546 al_draw_filled_ellipse(comets1
[i
].x, comets1
[i
].y,
20,
30,
al_map_rgb(255,
255,
255));
547 al_draw_ellipse(comets1
[i
].x, comets1
[i
].y,
10,
20,
al_map_rgb(0,
0,
0),
5.
0);
548 }
549 }
550}
551
552void StartComet1
(Comet1 comets1
[],
int size
)
553{
554 for(int i
= 0; i
< size
; i
++)
555 {
556 if(comets1
[i
].live
== 0)
557 {
558 if(rand() %
500 == 0)
559 {
560 comets1
[i
].live
+= 5;
561 comets1
[i
].x
= width
;
562 comets1
[i
].y
= 30 + rand() %
(height
- 60);
563 break;
564 }
565 }
566 }
567}
568
569void UpdateComet1
(Comet1 comets1
[],
int size
)
570{
571 for(int i
= 0; i
< size
; i
++)
572 {
573 if(comets1
[i
].live
> 0)
574 {
575 comets1
[i
].x
-= comets1
[i
].speed
;
576
577 if(comets1
[i
].x
< 0)
578 {
579 comets1
[i
].live
= 0;
580 }
581 }
582 }
583}
584
585void CollideComet1
(Comet1 comets1
[],
int cSize, SpaceShip
&ship
)
586{
587 for(int i
= 0; i
< cSize
; i
++)
588 {
589 if(comets1
[i
].live
> 0)
590 {
591 if(comets1
[i
].x
- comets1
[i
].boundx
< ship.x
+ ship.boundx
&&
592 comets1
[i
].x
+ comets1
[i
].boundx
> ship.x
- ship.boundx
&&
593 comets1
[i
].y
- comets1
[i
].boundy
< ship.y
+ ship.boundy
&&
594 comets1
[i
].y
+ comets1
[i
].boundy
> ship.y
- ship.boundy
)
595 {
596 ship.lives
= 0;
597 comets1
[i
].live
= 0;
598 }
599 else if(comets1
[i
].x
< 10)
600 {
601 ship.lives
= 0;
602 comets1
[i
].live
= 0;
603 }
604 }
605 }
606}