-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2380 lines (2026 loc) · 83.2 KB
/
main.c
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// [main.c]
// this template is provided for the 2D shooter game.
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <math.h>
// If defined, logs will be shown on console and written to file.
// If commented out, logs will not be shown nor be saved.
#define LOG_ENABLED
/* Constants. */
// Frame rate (frame per second)
const int FPS = 60;
// Display (screen) width.
const int SCREEN_W = 1000;
// Display (screen) height.
const int SCREEN_H = 800;
// At most 4 audios can be played at a time.
const int RESERVE_SAMPLES = 4;
// Same as:
// const int SCENE_MENU = 1;
// const int SCENE_START = 2;
const float resurrect = 1; //time for plane to be dead.
enum
{
SCENE_MENU = 1,
SCENE_START = 2
, SCENE_SETTINGS = 3
, SCENE_LOSE = 4
, SCENE_WIN = 5
, SCENE_CHARACTER = 6
, SCENE_INSTRUCTION1 = 7
, SCENE_INSTRUCTION2 = 8
};
/* Input states */
// The active scene id.
int active_scene;
// Keyboard state, whether the key is down or not.
bool key_state[ALLEGRO_KEY_MAX];
// Mouse state, whether the key is down or not.
// 1 is for left, 2 is for right, 3 is for middle.
bool* mouse_state;
// Mouse position.
int mouse_x, mouse_y;
// TODO: More variables to store input states such as joysticks, ...
int kill_count = 0;
int counter = 0;
int counter_bigboss = 10;
int plane1 = 1;
int plane2 = 0;
int plane3 = 0;
int plane4 = 0;
int plane5 = 0;
int music_flag1 = 1;
int music_flag2 = 0;
int music_flag3 = 0;
int music_flag4 = 0;
/* Variables for allegro basic routines. */
ALLEGRO_DISPLAY* game_display;
ALLEGRO_EVENT_QUEUE* game_event_queue;
ALLEGRO_TIMER* game_update_timer;
/* Shared resources*/
ALLEGRO_FONT* font_pirulen_32;
ALLEGRO_FONT* font_pirulen_24;
ALLEGRO_FONT* font_earthorbiter_20;
ALLEGRO_FONT* font_sega_50;
ALLEGRO_FONT* font_death_star_70;
ALLEGRO_FONT* font_over_there_70; //35
ALLEGRO_FONT* font_stone_50; //150
ALLEGRO_FONT* font_darth_chowder_50;
/* Menu Scene resources*/
ALLEGRO_BITMAP* main_img_background;
ALLEGRO_BITMAP* img_interstellar_odyssey;
ALLEGRO_BITMAP* img_interplanetary_odyssey;
ALLEGRO_BITMAP* img_electronic_fantasy;
ALLEGRO_BITMAP* img_background_settings;
ALLEGRO_BITMAP* img_base;
ALLEGRO_BITMAP* img_arrow_character;
ALLEGRO_BITMAP* img_fuel;
ALLEGRO_BITMAP* img_bigboss_bullet;
ALLEGRO_BITMAP* img_bag;
ALLEGRO_BITMAP* img_alien_laser;
ALLEGRO_BITMAP* img_bluesky;
ALLEGRO_BITMAP* img_orangesky;
ALLEGRO_BITMAP* img_lose;
ALLEGRO_BITMAP* img_enemies_lvl3;
ALLEGRO_BITMAP* img_enemies_lvl5;
ALLEGRO_BITMAP* img_bigboss;
ALLEGRO_BITMAP* img_instruction;
ALLEGRO_BITMAP* img_instruction1;
ALLEGRO_BITMAP* img_instruction2;
ALLEGRO_BITMAP* img_settings;
ALLEGRO_BITMAP* img_settings2;
ALLEGRO_BITMAP* img_settings;
ALLEGRO_BITMAP* img_settings2;
ALLEGRO_SAMPLE* main_bgm;
ALLEGRO_SAMPLE_ID main_bgm_id;
ALLEGRO_SAMPLE* audio_electronic_fantasy;
ALLEGRO_SAMPLE* audio_interstellar_odyssey;
ALLEGRO_SAMPLE* audio_interplanetary_odyssey;
ALLEGRO_SAMPLE_ID audio_electronic_fantasy_id;
ALLEGRO_SAMPLE_ID audio_interplanetary_odyssey_id;
ALLEGRO_SAMPLE_ID audio_interstellar_odyssey_id;
ALLEGRO_SAMPLE* audio_shoot;
ALLEGRO_SAMPLE* audio_crash;
ALLEGRO_SAMPLE* audio_bigboss_shoot;
ALLEGRO_SAMPLE* audio_money;
ALLEGRO_SAMPLE* audio_fuel;
ALLEGRO_SAMPLE_ID audio_shoot_id;
ALLEGRO_SAMPLE_ID audio_crash_id;
ALLEGRO_SAMPLE_ID audio_bigboss_shoot_id;
ALLEGRO_SAMPLE_ID audio_money_id;
ALLEGRO_SAMPLE_ID audio_fuel_id;
/* Start Scene resources*/
ALLEGRO_BITMAP* start_img_background;
ALLEGRO_BITMAP* start_img_plane;
ALLEGRO_BITMAP* img_plane2;
ALLEGRO_BITMAP* img_plane3;
ALLEGRO_BITMAP* img_plane4;
ALLEGRO_BITMAP* img_plane5;
ALLEGRO_BITMAP* img_shelf;
ALLEGRO_BITMAP* img_laser;
ALLEGRO_BITMAP* start_img_enemy;
ALLEGRO_SAMPLE* start_bgm;
ALLEGRO_SAMPLE_ID start_bgm_id;
ALLEGRO_BITMAP* img_bullet;
typedef struct
{
// The center coordinate of the image.
float x, y;
// The width and height of the object.
float w, h;
// The velocity in x, y axes.
float vx, vy;
// Should we draw this object on the screen.
bool hidden;
// The pointer to the object’s image.
int score;
int hp;
int e_hp;
ALLEGRO_BITMAP* img;
} MovableObject;
void draw_movable_object(MovableObject obj);
#define MAX_ENEMY 3
#define MAX_BULLET 5
#define MAX_BAG 2
#define MAX_BIGBOSS_BULLET 3
#define MAX_ALIEN_LASER 3
MovableObject plane;
MovableObject enemies[MAX_ENEMY];
MovableObject enemies1[MAX_ENEMY];
MovableObject enemies2[MAX_ENEMY];
MovableObject bigboss;
MovableObject laser;
MovableObject bullets[MAX_BULLET];
MovableObject bigboss_bullets[MAX_BIGBOSS_BULLET];
MovableObject alien_laser[MAX_ALIEN_LASER];
MovableObject fuel;
MovableObject bag[MAX_BAG];
const float MAX_COOLDOWN = 0.2;
const float MAX_ENEMY_COOLDOWN = 0.5; //enemy cooldown
const float MAX_ALIEN_COOLDOWN = 1;
double last_shoot_timestamp;
double last_enemy_timestamp;
double last_alien_timestamp;
double plane_crash;
/* Declare function prototypes. */
// Initialize allegro5 library
void allegro5_init(void);
// Initialize variables and resources.
// Allows the game to perform any initialization it needs before
// starting to run.
void game_init(void);
// Process events inside the event queue using an infinity loop.
void game_start_event_loop(void);
// Run game logic such as updating the world, checking for collision,
// switching scenes and so on.
// This is called when the game should update its logic.
void game_update(void);
// Draw to display.
// This is called when the game should draw itself.
void game_draw(void);
// Release resources.
// Free the pointers we allocated.
void game_destroy(void);
// Function to change from one scene to another.
void game_change_scene(int next_scene);
// Load resized bitmap and check if failed.
ALLEGRO_BITMAP* load_bitmap_resized(const char* filename, int w, int h);
bool pnt_in_rect(int px, int py, int x, int y, int w, int h);
/* Event callbacks. */
void on_key_down(int keycode);
void on_mouse_down(int btn, int x, int y);
/* Declare function prototypes for debugging. */
// Display error message and exit the program, used like 'printf'.
// Write formatted output to stdout and file from the format string.
// If the program crashes unexpectedly, you can inspect "log.txt" for
// further information.
void game_abort(const char* format, ...);
// Log events for later debugging, used like 'printf'.
// Write formatted output to stdout and file from the format string.
// You can inspect "log.txt" for logs in the last run.
void game_log(const char* format, ...);
// Log using va_list.
void game_vlog(const char* format, va_list arg);
int main(int argc, char** argv)
{
// Set random seed for better random outcome.
srand(time(NULL));
allegro5_init();
game_log("Allegro5 initialized");
game_log("Game begin");
// Initialize game variables.
game_init();
game_log("Game initialized");
// Draw the first frame.
game_draw();
game_log("Game start event loop");
// This call blocks until the game is finished.
game_start_event_loop();
game_log("Game end");
game_destroy();
return 0;
}
void allegro5_init(void)
{
if (!al_init())
game_abort("failed to initialize allegro");
// Initialize add-ons.
if (!al_init_primitives_addon())
game_abort("failed to initialize primitives add-on");
if (!al_init_font_addon())
game_abort("failed to initialize font add-on");
if (!al_init_ttf_addon())
game_abort("failed to initialize ttf add-on");
if (!al_init_image_addon())
game_abort("failed to initialize image add-on");
if (!al_install_audio())
game_abort("failed to initialize audio add-on");
if (!al_init_acodec_addon())
game_abort("failed to initialize audio codec add-on");
if (!al_reserve_samples(RESERVE_SAMPLES))
game_abort("failed to reserve samples");
if (!al_install_keyboard())
game_abort("failed to install keyboard");
if (!al_install_mouse())
game_abort("failed to install mouse");
// Setup game display.
game_display = al_create_display(SCREEN_W, SCREEN_H);
if (!game_display)
game_abort("failed to create display");
al_set_window_title(game_display, "Space Shooter");
// Setup update timer.
game_update_timer = al_create_timer(1.0f / FPS);
if (!game_update_timer)
game_abort("failed to create timer");
// Setup event queue.
game_event_queue = al_create_event_queue();
if (!game_event_queue)
game_abort("failed to create event queue");
// Malloc mouse buttons state according to button counts.
const unsigned m_buttons = al_get_mouse_num_buttons();
game_log("There are total %u supported mouse buttons", m_buttons);
// mouse_state[0] will not be used.
mouse_state = malloc((m_buttons + 1) * sizeof(bool));
memset(mouse_state, false, (m_buttons + 1) * sizeof(bool));
// Register display, timer, keyboard, mouse events to the event queue.
al_register_event_source(game_event_queue, al_get_display_event_source(game_display));
al_register_event_source(game_event_queue, al_get_timer_event_source(game_update_timer));
al_register_event_source(game_event_queue, al_get_keyboard_event_source());
al_register_event_source(game_event_queue, al_get_mouse_event_source());
// TODO: Register other event sources such as timer, video, ...
// Start the timer to update and draw the game.
al_start_timer(game_update_timer);
}
void game_init(void)
{
/* Shared resources*/
font_pirulen_32 = al_load_font("pirulen.ttf", 32, 0);
if (!font_pirulen_32)
game_abort("failed to load font: pirulen.ttf with size 32");
font_pirulen_24 = al_load_font("pirulen.ttf", 24, 0);
if (!font_pirulen_24)
game_abort("failed to load font: pirulen.ttf with size 24");
font_earthorbiter_20 = al_load_font("earthorbiter.ttf", 20, 0);
if (!font_earthorbiter_20)
game_abort("failed to load font: earthorbiter.ttf with size 20");
font_sega_50 = al_load_font("sega.ttf", 50, 0);
if (!font_sega_50)
game_abort("failed to load font: sega.ttf with size 50");
font_death_star_70 = al_load_font("deathstar.ttf", 70, 0);
if (!font_death_star_70)
game_abort("failed to load font: deathstar.ttf with size 70");
font_over_there_70 = al_load_font("overthere.ttf", 35, 0);
if (!font_over_there_70)
game_abort("failed to load font: overthere.ttf with size 70");
font_stone_50 = al_load_font("stone.ttf", 150, 0);
if (!font_stone_50)
game_abort("failed to load font: stone.ttf with size 50");
font_darth_chowder_50 = al_load_font("darthchowder.ttf", 50, 0);
if (!font_darth_chowder_50)
game_abort("failed to load font: darthchowder.ttf with size 50");
/* Menu Scene resources*/
main_img_background = load_bitmap_resized("main-bg.jpg", SCREEN_W, SCREEN_H);
main_bgm = al_load_sample("S31-Night Prowler.ogg"); //opening music
if (!main_bgm)
game_abort("failed to load audio: S31-Night Prowler.ogg");
audio_electronic_fantasy = al_load_sample("Electronic Fantasy.ogg");
if (!audio_electronic_fantasy)
game_abort("failed to load audio: Electronic Fantasy.ogg");
audio_interstellar_odyssey = al_load_sample("Interstellar Odyssey.ogg");
if (!audio_interstellar_odyssey)
game_abort("failed to load audio: Interstellar Odyssey.ogg");
audio_interplanetary_odyssey = al_load_sample("Interplanetary Odyssey.ogg");
if (!audio_interplanetary_odyssey)
game_abort("failed to load audio: Interplanetary Odyssey.ogg");
audio_shoot = al_load_sample("shoot.ogg");
if (!audio_shoot)
game_abort("failed to load audio: shoot.ogg");
audio_bigboss_shoot = al_load_sample("bigboss_shoot.ogg");
if (!audio_bigboss_shoot)
game_abort("failed to load audio: bigboss_shoot.ogg");
audio_crash = al_load_sample("crash.ogg");
if (!audio_crash)
game_abort("failed to load audio: crash.ogg");
audio_money = al_load_sample("money.ogg");
if (!audio_money)
game_abort("failed to load audio: money.ogg");
audio_fuel = al_load_sample("fuel.ogg");
if (!audio_fuel)
game_abort("failed to load audio: fuel.ogg");
img_settings = al_load_bitmap("settings.png");
if (!img_settings)
game_abort("failed to load image: settings.png");
img_settings2 = al_load_bitmap("settings2.png");
if (!img_settings2)
game_abort("failed to load image: settings2.png");
img_base = al_load_bitmap("base.png");
if (!img_base)
game_abort("failed to load image: base.png");
img_electronic_fantasy = al_load_bitmap("electronic_fantasy.png");
if (!img_electronic_fantasy)
game_abort("failed to load image: electronic_fantasy.png");
img_interplanetary_odyssey = al_load_bitmap("interplanetary_odyssey.png");
if (!img_interplanetary_odyssey)
game_abort("failed to load image: interplanetary_odyssey.png");
img_interstellar_odyssey = al_load_bitmap("interstellar_odyssey.png");
if (!img_interstellar_odyssey)
game_abort("failed to load image: interstellar_odyssey.png");
img_background_settings = al_load_bitmap("outerspace.png");
if (!img_background_settings)
game_abort("failed to load image: outerspace.png");
img_lose = al_load_bitmap("spacelose.png");
if (!img_lose)
game_abort("failed to load image: spacelose.png");
img_arrow_character = al_load_bitmap("arrow.png");
if (!img_arrow_character)
game_abort("failed to load image: arrow.png");
img_plane2 = al_load_bitmap("plane2.png");
if (!img_plane2)
game_abort("failed to load image: plane2.png");
img_plane3 = al_load_bitmap("plane3.png");
if (!img_plane3)
game_abort("failed to load image: plane3.png");
img_plane4 = al_load_bitmap("plane4.png");
if (!img_plane4)
game_abort("failed to load image: plane4.png");
img_plane5 = al_load_bitmap("plane5.png");
if (!img_plane5)
game_abort("failed to load image: plane5.png");
img_shelf = al_load_bitmap("shelf.png");
if (!img_shelf)
game_abort("failed to load image: shelf.png");
img_fuel = al_load_bitmap("fuel.png");
if (!img_fuel)
game_abort("failed to load image: fuel.png");
img_bag = al_load_bitmap("bag.png");
if (!img_bag)
game_abort("failed to load image: bag.png");
img_enemies_lvl3 = al_load_bitmap("3times.png");
if (!img_enemies_lvl3)
game_abort("failed to load image: 3times.png");
img_enemies_lvl5 = al_load_bitmap("5times.png");
if (!img_enemies_lvl5)
game_abort("failed to load image: 5times.png");
img_bigboss = al_load_bitmap("bigboss.png");
if (!img_bigboss)
game_abort("failed to load image: bigboss.png");
img_bigboss_bullet = al_load_bitmap("bigbossbullet.png");
if (!img_bigboss_bullet)
game_abort("failed to load image: bigbossbullet.png");
img_bluesky = al_load_bitmap("bluesky.png");
if (!img_bluesky)
game_abort("failed to load image: bluesky.png");
img_orangesky = al_load_bitmap("orangesky.png");
if (!img_orangesky)
game_abort("failed to load image: orangesky.png");
img_instruction = al_load_bitmap("instruction.png");
if (!img_instruction)
game_abort("failed to load image: instruction.png");
img_instruction1 = al_load_bitmap("instruction1.png");
if (!img_instruction1)
game_abort("failed to load image: instruction1.png");
img_instruction2 = al_load_bitmap("instruction2.png");
if (!img_instruction2)
game_abort("failed to load image: instruction2.png");
img_laser = al_load_bitmap("laser.png");
if (!img_laser)
game_abort("failed to load image: laser.png");
img_alien_laser = al_load_bitmap("redlaser.png");
if (!img_alien_laser)
game_abort("failed to load image: redlaser.png");
/* Start Scene resources*/
start_img_background = load_bitmap_resized("start-bg.jpg", SCREEN_W, SCREEN_H);
start_img_plane = al_load_bitmap("plane.png");
if (!start_img_plane)
game_abort("failed to load image: plane.png");
start_img_enemy = al_load_bitmap("smallfighter0006.png");
if (!start_img_enemy)
game_abort("failed to load image: smallfighter0006.png");
start_bgm = al_load_sample("mythica.ogg"); //game sound
if (!start_bgm)
game_abort("failed to load audio: mythica.ogg");
img_bullet = al_load_bitmap("image12.png");
if (!img_bullet)
game_abort("failed to load image: image12.png");
// Change to first scene.
game_change_scene(SCENE_MENU);
}
void game_start_event_loop(void)
{
bool done = false;
ALLEGRO_EVENT event;
int redraws = 0;
while (!done)
{
al_wait_for_event(game_event_queue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
// Event for clicking the window close button.
game_log("Window close button clicked");
done = true;
}
else if (event.type == ALLEGRO_EVENT_TIMER)
{
// Event for redrawing the display.
if (event.timer.source == game_update_timer)
// The redraw timer has ticked.
redraws++;
}
else if (event.type == ALLEGRO_EVENT_KEY_DOWN)
{
// Event for keyboard key down.
game_log("Key with keycode %d down", event.keyboard.keycode);
key_state[event.keyboard.keycode] = true;
on_key_down(event.keyboard.keycode);
}
else if (event.type == ALLEGRO_EVENT_KEY_UP)
{
// Event for keyboard key up.
game_log("Key with keycode %d up", event.keyboard.keycode);
key_state[event.keyboard.keycode] = false;
}
else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
// Event for mouse key down.
game_log("Mouse button %d down at (%d, %d)", event.mouse.button, event.mouse.x, event.mouse.y);
mouse_state[event.mouse.button] = true;
on_mouse_down(event.mouse.button, event.mouse.x, event.mouse.y);
}
else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
{
// Event for mouse key up.
game_log("Mouse button %d up at (%d, %d)", event.mouse.button, event.mouse.x, event.mouse.y);
mouse_state[event.mouse.button] = false;
}
else if (event.type == ALLEGRO_EVENT_MOUSE_AXES)
{
if (event.mouse.dx != 0 || event.mouse.dy != 0)
{
// Event for mouse move.
game_log("Mouse move to (%d, %d)", event.mouse.x, event.mouse.y);
mouse_x = event.mouse.x;
mouse_y = event.mouse.y;
}
else if (event.mouse.dz != 0)
{
// Event for mouse scroll.
game_log("Mouse scroll at (%d, %d) with delta %d", event.mouse.x, event.mouse.y, event.mouse.dz);
}
}
// TODO: Process more events and call callbacks by adding more
// entries inside Scene.
// Redraw
if (redraws > 0 && al_is_event_queue_empty(game_event_queue))
{
// if (redraws > 1)
// game_log("%d frame(s) dropped", redraws - 1);
// Update and draw the next frame.
game_update();
game_draw();
redraws = 0;
}
}
}
void game_update(void)
{
if (active_scene == SCENE_START)
{
plane.vx = plane.vy = 0;
if (key_state[ALLEGRO_KEY_UP] || key_state[ALLEGRO_KEY_W])
plane.vy -= 1;
if (key_state[ALLEGRO_KEY_DOWN] || key_state[ALLEGRO_KEY_S])
plane.vy += 1;
if (key_state[ALLEGRO_KEY_LEFT] || key_state[ALLEGRO_KEY_A])
plane.vx -= 1;
if (key_state[ALLEGRO_KEY_RIGHT] || key_state[ALLEGRO_KEY_D])
plane.vx += 1;
// 0.71 is (1/sqrt(2)).
plane.y += plane.vy * 10 * (plane.vx ? 0.71f : 1); //plane speed up down
plane.x += plane.vx * 10 * (plane.vy ? 0.71f : 1); //plane speed right left
if (plane.x - plane.w / 2 < 0)
plane.x = plane.w / 2;
else if (plane.x + plane.w / 2 > SCREEN_W)
plane.x = SCREEN_W - plane.w / 2;
if (plane.y - plane.h / 2 < 0)
plane.y = plane.h / 2;
else if (plane.y + plane.h / 2 > SCREEN_H)
plane.y = SCREEN_H - plane.h / 2;
double time_now = al_get_time(); //to get new time after being dead.
//to revive the plane after being dead, but hp is still decreased
if (time_now - plane_crash >= resurrect)
plane.hidden = false;
int i;
for (int i = 0; i < MAX_BULLET; i++)
{
if (bullets[i].hidden == true)
continue;
bullets[i].x += bullets[i].vx;
bullets[i].y += bullets[i].vy;
if (bullets[i].y < 0)
bullets[i].hidden = true;
}
//bigboss bullets
for (int i = 0; i < MAX_BIGBOSS_BULLET; i++)
{
if (!bigboss_bullets[i].hidden)
{
bigboss_bullets[i].x += bigboss_bullets[i].vx;
bigboss_bullets[i].y += bigboss_bullets[i].vy;
}
}
for (int i = 0; i < MAX_BIGBOSS_BULLET; i++)
{
if (bigboss_bullets[i].y >= SCREEN_H)
bigboss_bullets[i].hidden = true;
}
//laser bigboss
if (!laser.hidden)
{
laser.x += laser.vx;
laser.y += laser.vy;
}
if (laser.y < 0)
laser.hidden = true;
double now = al_get_time();
if (!plane.hidden && counter_bigboss != 0)
{
if (key_state[ALLEGRO_KEY_SPACE] && now - last_shoot_timestamp >= MAX_COOLDOWN)
{
al_play_sample(audio_shoot, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
for (i = 0; i < MAX_BULLET; i++)
{
if (bullets[i].hidden == true)
break;
}
if (i < MAX_BULLET)
{
last_shoot_timestamp = now;
bullets[i].hidden = false;
bullets[i].x = plane.x;
bullets[i].y = plane.y;
}
}
}
if (!plane.hidden && counter_bigboss == 0 && kill_count >= 18)
{
if (key_state[ALLEGRO_KEY_SPACE] && now - last_shoot_timestamp >= MAX_COOLDOWN)
{
if (laser.hidden == true)
{
last_shoot_timestamp = now;
laser.hidden = false;
laser.x = plane.x;
laser.y = plane.y;
counter_bigboss = 10;
}
}
}
//bigboss shooting bullets
double enemy_now = al_get_time();
for (int i = 0; i < MAX_BIGBOSS_BULLET; i++)
{
if (!bigboss.hidden && enemy_now - last_enemy_timestamp >= MAX_ENEMY_COOLDOWN)
{
if (bigboss_bullets[i].hidden)
{
last_enemy_timestamp = enemy_now;
bigboss_bullets[i].hidden = false;
bigboss_bullets[i].x = bigboss.x;
bigboss_bullets[i].y = bigboss.y + bigboss.h / 2;
}
}
}
for (int i = 0; i < MAX_BIGBOSS_BULLET; i++)
{
if (!bigboss_bullets[i].hidden)
{
bigboss_bullets[i].x += bigboss_bullets[i].vx;
bigboss_bullets[i].y += bigboss_bullets[i].vy;
}
}
if (bigboss.y >= SCREEN_H)
bigboss.hidden = true;
if (kill_count < 10)
{
for (i = 0; i < MAX_ENEMY; i++) //to make the enemies move downwards
{
if (enemies[i].hidden == true)
continue;
enemies[i].x += enemies[i].vx;
enemies[i].y += enemies[i].vy;
if (enemies[i].y > SCREEN_H)
enemies[i].hidden = true;
}
for (i = 0; i < MAX_ENEMY; i++)
{
if (enemies[i].hidden == true)
{
break;
}
}
if (i < MAX_ENEMY) //to show the enemies again
{
enemies[i].hidden = false;
enemies[i].x = enemies[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies[i].w);
enemies[i].y = 0;
}
if (!fuel.hidden)
{
fuel.x += fuel.vx;
fuel.y += fuel.vy;
}
if (fuel.y > SCREEN_H)
fuel.hidden = true;
if (fuel.hidden == true)
{
fuel.hidden = false;
fuel.x = enemies[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies[i].w);
fuel.y = 0;
}
for (i = 0; i < MAX_BAG; i++)
{
if (bag[i].hidden == true)
continue;
bag[i].x += bag[i].vx;
bag[i].y += bag[i].vy;
if (bag[i].y > SCREEN_H)
bag[i].hidden = true;
}
for (i = 0; i < MAX_BAG; i++)
{
if (bag[i].hidden == true)
break;
}
if (i < MAX_BAG) //to show the bag again
{
bag[i].hidden = false;
bag[i].x = enemies[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies[i].w);
bag[i].y = 0;
}
//COLLISION BULLET PLANE TO ENEMY
for (i = 0; i < MAX_BULLET; i++) //if bullets is not hidden, then use for loop
{
if (!bullets[i].hidden)
{
for (int j = 0; j < MAX_ENEMY; j++) //if enemies is not hidden, then use for loop
{
if (!enemies[j].hidden)
{
//when u know that bullets and enemies are not hidden,
//then set the coordinate of the bullets and the enemies
//so that when their coordinates align
//the enemies will disappear
if (bullets[i].x < enemies[j].x + enemies[j].w / 2 &&
bullets[i].x > enemies[j].x - enemies[j].w / 2 &&
bullets[i].y < enemies[j].y + enemies[j].h / 2 &&
bullets[i].y > enemies[j].y - enemies[j].h / 2)
{
al_play_sample(audio_crash, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
enemies[j].hidden = true;
bullets[i].hidden = true;
plane.score += 10;
kill_count += 1;
}
}
}
}
}
double time_crash = al_get_time();
//ENEMIES CRASH TO PLANE
for (i = 0; i < MAX_ENEMY; i++) //if enemy is not hidden, then use for loop
{
if (!enemies[i].hidden)
{
if (!plane.hidden)
//when u know that plane and enemies are not hidden,
//when their coordinates align
//both the plane and enemies will disappear
if (plane.x - plane.w / 2 < enemies[i].x + enemies[i].w / 2 &&
plane.x + plane.w / 2 > enemies[i].x - enemies[i].w / 2 &&
plane.y - plane.h / 2 < enemies[i].y + enemies[i].h / 2 &&
plane.y + plane.h / 2 > enemies[i].y - enemies[i].h / 2)
{
al_play_sample(audio_crash, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
enemies[i].hidden = true;
plane.hidden = true;
plane.hp -= 1;
plane_crash = time_crash; //to get the time when plane crashes the enemy;
}
}
}
}
if (kill_count >= 10 && kill_count < 15)
{
for (i = 0; i < MAX_ENEMY; i++) //to make the enemies move downwards
{
if (enemies1[i].hidden == true)
continue;
enemies1[i].x += enemies1[i].vx;
enemies1[i].y += enemies1[i].vy;
if (enemies1[i].y > SCREEN_H)
enemies1[i].hidden = true;
}
for (i = 0; i < MAX_ENEMY; i++)
{
if (enemies1[i].hidden == true)
break;
}
if (i < MAX_ENEMY) //to show the enemies again
{
enemies1[i].hidden = false;
enemies1[i].x = enemies1[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies1[i].w);
enemies1[i].y = 0;
}
if (!fuel.hidden)
{
fuel.x += fuel.vx;
fuel.y += fuel.vy;
}
if (fuel.y > SCREEN_H)
fuel.hidden = true;
if (fuel.hidden == true)
{
fuel.hidden = false;
fuel.x = enemies1[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies1[i].w);
fuel.y = 0;
}
for (i = 0; i < MAX_BAG; i++)
{
if (bag[i].hidden == true)
continue;
bag[i].x += bag[i].vx;
bag[i].y += bag[i].vy;
if (bag[i].y > SCREEN_H)
bag[i].hidden = true;
}
for (i = 0; i < MAX_BAG; i++)
{
if (bag[i].hidden == true)
{
break;
}
}
if (i < MAX_BAG) //to show the bag again
{
bag[i].hidden = false;
bag[i].x = enemies1[i].w / 2 + (float)rand() / RAND_MAX * (SCREEN_W - enemies1[i].w);
bag[i].y = 0;
}
//COLLISION
for (i = 0; i < MAX_BULLET; i++) //if bullets is not hidden, then use for loop
{
if (!bullets[i].hidden)
{
for (int j = 0; j < MAX_ENEMY; j++) //if enemies is not hidden, then use for loop
{
if (!enemies1[j].hidden)
//when u know that bullets and enemies are not hidden,
//then set the coordinate of the bullets and the enemies
//so that when their coordinates align
//the enemies will disappear
if (bullets[i].x < enemies1[j].x + enemies1[j].w / 2 &&
bullets[i].x > enemies1[j].x - enemies1[j].w / 2 &&
bullets[i].y < enemies1[j].y + enemies1[j].h / 2 &&
bullets[i].y > enemies1[j].y - enemies1[j].h / 2)
{
al_play_sample(audio_crash, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
enemies1[j].e_hp -= 1;
bullets[i].hidden = true;
if (enemies1[j].e_hp <= 0)
{
enemies1[j].hidden = true;
plane.score += 20;
kill_count += 1;
}
}
}
}
}
for (int i = 0; i < MAX_ENEMY; i++)
{
if (enemies1[i].hidden == true)
{
enemies1[i].e_hp = 3;
}
}
double time_crash = al_get_time();
//ENEMIES CRASH TO PLANE
for (i = 0; i < MAX_ENEMY; i++) //if enemy is not hidden, then use for loop
{
if (!enemies1[i].hidden)
{
if (!plane.hidden)