-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1574 lines (1092 loc) · 42.7 KB
/
main.cpp
File metadata and controls
1574 lines (1092 loc) · 42.7 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
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Game Name: Dunk It Together
// Date: JAN 2021
// Author: Nandinbold Norovsambuu
// Description: This is a simple platformer game
/////////////// The creator myself wanted to point out one of the worldwide environmental issues that are demanding in today's world, Air Pollution.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <allegro5/allegro5.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.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>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <tuple>
#include "mappy_A5.h"
#include <pthread.h>
#define WIDTH 960
#define HEIGHT 470
#define FPS 50
#define NUM_THREADS 5
using namespace std;
bool keys[] = {false,false,false,false,false};
enum KEYS{LEFT,RIGHT,SPACE,UP,DOWN};
//Finite states
enum carState{CLOSE,FAR};
enum basketballState{NORMAL, LONGDUNK, REG_DUNK, TOO_MANY_ATTEMPT, FAIR_ATTEMPTS, GOOD_ATTEMPTS, PERFECT_ATTEMPTS, POOR_ATTEMPTS, TOO_CLOSE};
int textCenterY = 80 , textX = WIDTH;
int moveSpeed = 12 , moveSpeed1 = 5,moveSpeed2 = 2;
int current_x_off = 0;
int carState = -1;
int hoopState = -1;
int hoopNum = 0;
struct BALLER{
ALLEGRO_BITMAP *sprite_sheet;
ALLEGRO_BITMAP *image;
int top[4];
int bottom[4];
// int x1,x2,y1,y2;
int w_img,h_img;
};
struct Objects{
ALLEGRO_BITMAP *sprite_sheet;
ALLEGRO_BITMAP *image;
int positionX,positionY;
int x1,x2,y1,y2;
int w_img,h_img;
int sprite_count;
int counter = 0,counter2;
bool draw = true;
};
void loadObjects(Objects * objects);
void changeCarState(int &state, int newState);
void changeHoopState(int &hoopState, int newState);
int main(int argc, char **argv)
{
//al_init
if(!al_init())
al_show_native_message_box(NULL,NULL,NULL,"failed to initialize allegro!!",NULL,0);
//display
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE * queue = NULL;
ALLEGRO_TIMER * timer;
display = al_create_display(WIDTH,HEIGHT);
if(!display)
al_show_native_message_box(NULL,NULL,"Couldn't open the display",NULL,NULL,0);
queue = al_create_event_queue();
timer = al_create_timer(1.0f/FPS);
// addons
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
al_init_acodec_addon();
al_init_image_addon();
al_install_keyboard();
al_install_audio();
//coordinates
int x_off = 0, y_off = 0;
int x_player = 10 , y_player = 240;
int mapColumns = 0,mapSize= 0, tilesize = 32;
int counter_baller= 0, counter_stopping =0, counter_dunking = 0;
int checkPoint = 10;
int jumpRange = 0;
int attemptNum = 0, madeNum = 0,randNum = 1;
int animation_count = 0 , hoopCount = 0;
int jumpDistance,jumpDistance2;
int i_rand = rand() % 3;
//game logic
bool done = false,render = false;
bool playerDribbling = false, playerStopping = false, playerDunking = false,longDunk=false,boundary = true;
bool gamePlay = false,gamePlayFront = true;
bool frontItems = true;
bool intro = true;
bool dunkAttempt, dunkMade = false;
bool drawPlayer = true , drawPlayerBack = false;
bool hoopEffect = false, hoopEffect2 = false;
bool collided = false,getRandNum = false;
bool musicPlaying = true;
//booleans for help menu
bool help = false ,showDunking = false,showLongDunking = false , endGame = false;
//game fonts
ALLEGRO_FONT *font_40_man = al_load_font("fonts/manaspc.ttf",40,0);
ALLEGRO_FONT *font_30_man = al_load_font("fonts/manaspc.ttf",30,0);
ALLEGRO_FONT *font_25_man = al_load_font("fonts/manaspc.ttf",25,0);
ALLEGRO_FONT *font_20_man = al_load_font("fonts/manaspc.ttf",20,0);
ALLEGRO_FONT * font_100_tarrget = al_load_font("fonts/Tarrget3D.otf",100,0);
ALLEGRO_FONT * font_80_tarrget = al_load_font("fonts/Tarrget3D.otf",80,0);
ALLEGRO_FONT * font_40_tarrget = al_load_font("fonts/Tarrget3D.otf",40,0);
ALLEGRO_FONT * font_20_tarrget = al_load_font("fonts/Tarrget3D.otf",20,0);
//text colors
ALLEGRO_COLOR text_col = al_map_rgb(255,191,70);
ALLEGRO_COLOR text_col_green = al_map_rgb(170,230,136);
ALLEGRO_COLOR text_col_red = al_map_rgb(255,107,107);
// ALLEGRO_COLOR text_col_green = al_map_rgb(57,202,243);
//register events
al_register_event_source(queue, al_get_timer_event_source(timer));
al_register_event_source(queue,al_get_display_event_source(display));
al_register_event_source(queue,al_get_keyboard_event_source());
//game sound
al_reserve_samples(1);
ALLEGRO_SAMPLE *gamesample = al_load_sample("shopTheme.wav");
ALLEGRO_SAMPLE_INSTANCE * gamesound = al_create_sample_instance(gamesample);
al_attach_sample_instance_to_mixer(gamesound,al_get_default_mixer());
al_set_sample_instance_gain(gamesound,0.5);
//load the map using mappy_A5
if(MapLoad("map.fmp", 1))
return -5;
BALLER baller;
baller.sprite_sheet = al_load_bitmap("bitmaps/baller1.png");
baller.w_img = al_get_bitmap_width(baller.sprite_sheet);
baller.h_img = al_get_bitmap_height(baller.sprite_sheet);
baller.image = al_create_sub_bitmap(baller.sprite_sheet, 0, 0, baller.w_img/21, baller.h_img);
BALLER baller2;
baller2.sprite_sheet = al_load_bitmap("bitmaps/baller2.png");
baller2.w_img = al_get_bitmap_width(baller2.sprite_sheet);
baller2.h_img = al_get_bitmap_height(baller2.sprite_sheet);
baller2.image = al_create_sub_bitmap(baller2.sprite_sheet, 0, 0, baller2.w_img/7 , baller2.h_img);
BALLER baller3;
baller3.sprite_sheet = al_load_bitmap("bitmaps/baller3.png");
baller3.w_img = al_get_bitmap_width(baller3.sprite_sheet);
baller3.h_img = al_get_bitmap_height(baller3.sprite_sheet);
baller3.image = al_create_sub_bitmap(baller3.sprite_sheet, 0, 0, baller3.w_img/17 , baller3.h_img);
//main array conaining objects in the game
Objects objects[5];
loadObjects(objects);
Objects hoop;
hoop.sprite_sheet = al_load_bitmap ("bitmaps/hoop.png");
hoop.w_img = al_get_bitmap_width(hoop.sprite_sheet);
hoop.h_img = al_get_bitmap_height(hoop.sprite_sheet);
hoop.positionX = WIDTH + 200;
hoop.positionY = 50;
hoop.sprite_count = 14;
Objects hoop2;
hoop2.sprite_sheet = al_load_bitmap ("bitmaps/hoop.png");
hoop2.w_img = al_get_bitmap_width(hoop2.sprite_sheet);
hoop2.h_img = al_get_bitmap_height(hoop2.sprite_sheet);
hoop2.positionX = WIDTH + 600;
hoop2.positionY = -20;
hoop2.counter = 0;
hoop2.sprite_count = 14;
Objects hoop_green;
hoop_green.sprite_sheet = al_load_bitmap("bitmaps/hoop1.png");
hoop_green.image = al_create_sub_bitmap(hoop_green.sprite_sheet,0,0,hoop.w_img, hoop.h_img/hoop.sprite_count);
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT ev;
ALLEGRO_KEYBOARD_STATE keyState;
al_wait_for_event(queue, &ev);
al_get_keyboard_state (&keyState);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
if(!playerDunking){
keys[RIGHT] = true;
playerDribbling = true;
playerStopping = false;
intro = false;
}
break;
case ALLEGRO_KEY_UP:
if(!playerDunking){
keys[UP] = true;
playerDribbling = true;
playerStopping = false;
moveSpeed1 -= 4;
moveSpeed -=9;
}
break;
case ALLEGRO_KEY_DOWN:
if(!playerDunking){
keys[DOWN] = true;
playerDribbling = true;
playerStopping = false;
moveSpeed1 -= 4;
moveSpeed -=9;
}
break;
case ALLEGRO_KEY_SPACE:
if(al_key_down(&keyState,ALLEGRO_KEY_LSHIFT)) longDunk = true;
keys[SPACE] = true;
playerDunking = true;
playerDribbling = false;
playerStopping = false;
counter_dunking = 0;
moveSpeed1 -= 2;
moveSpeed -= 4;
break;
case ALLEGRO_KEY_ENTER:
if(collided){
//reset finite States
changeHoopState(hoopState,NORMAL);
//reset the coordinates
x_player = 10;
y_player = 240;
gamePlay = true;
gamePlayFront = true;
collided = false;
drawPlayer = true;
objects[0].positionX = WIDTH + 200;
objects[1].positionX = -300;
objects[2].positionX = WIDTH + 1000;
//reset the hoop positions when restarts
if(checkPoint == 10){
hoop.positionX = WIDTH;
}else if(checkPoint ==12){
hoop2.positionX = WIDTH;
}else if (checkPoint == 14){
hoop2.positionX = WIDTH;
}
if(checkPoint == 15){
//reset hoop positions
hoop2.positionX = WIDTH + 600;
hoop.positionX = WIDTH;
hoopEffect2 =false;
objects[4].counter = 0;
hoopCount = 0;
}
}
break;
}
if(al_key_down(&keyState , ALLEGRO_KEY_LCTRL)){
//help Screen
if(al_key_down(&keyState,ALLEGRO_KEY_H))
help = true;
//Music Toggle
if(al_key_down(&keyState,ALLEGRO_KEY_M)){
if(musicPlaying) musicPlaying = false;
else musicPlaying = true;
}
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
if(!playerDunking){
playerDribbling = false;
playerStopping = true;
}
break;
case ALLEGRO_KEY_UP:
keys[UP] = false;
if(!playerDunking){
playerDribbling = false;
playerStopping = true;
}
moveSpeed1 += 4;
moveSpeed += 9;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
if(!playerDunking){
playerDribbling = false;
playerStopping = true;
}
moveSpeed1 += 4;
moveSpeed += 9;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
longDunk = false;
dunkAttempt = true;
counter_dunking = 16;
attemptNum++;
moveSpeed1 += 2;
moveSpeed += 4;
break;
case ALLEGRO_KEY_H:
if(x_off < 50)
intro = true;
else
{
gamePlay = true;
gamePlayFront = true;
}
help = false;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
//toggle the music on/off with musicPlaying boolean
al_set_sample_instance_playmode(gamesound, ALLEGRO_PLAYMODE_LOOP);
al_set_sample_instance_playing(gamesound,musicPlaying);
y_player -= keys[UP] * 1;
y_player += keys[DOWN] * 1 ;
if(x_off < 0)
x_off = 0;
if(y_off < 0)
y_off = 0;
if(x_off > (mapwidth*mapblockwidth - WIDTH)){
x_off = 9028;
}
if(y_off > mapheight*mapblockheight - HEIGHT)
y_off = mapheight*mapblockheight - HEIGHT;
//keep the player within the y boundaries
if(y_player > 240)
y_player = 240;
if(!playerDunking){
//if the player moves up make objects seem in front of the player
if(y_player < 160 && checkPoint >=11){
drawPlayer = false;
drawPlayerBack = true;
if(y_player < 120)
y_player = 120;
}else{
if(!collided){
drawPlayer = true;
drawPlayerBack =false;
}
}
}
//x boundaries
if( !playerDunking && x_player > 800 && boundary){
x_player = 800;
}
if(x_player < 0)
x_player = 0;
//checkpoint examination
if(x_off > 50 && !collided && !endGame && !help){
gamePlay = true;
}
render = true;
}
if(render && al_is_event_queue_empty(queue))
{
render = false;
MapDrawBG(x_off,y_off, 0, 0, WIDTH, HEIGHT);
MapDrawFG(x_off,y_off,0,0,WIDTH,HEIGHT,2);
//// collision detection for dunking
// if(jumpDistance< 340 && jumpDistance > 322 && baller.top[3]> hoop.y1 && baller.top[1] < hoop.y2 && baller.top[2] > hoop.x1 && baller.top[0] < hoop.x2 ){
// //if player dunked on the current hoop already, don't increment the score
// if(!hoopEffect) dunkMade = true;
// }
if(playerDribbling){
if(counter_baller >= 20) counter_baller =0;
baller.image = al_create_sub_bitmap(baller.sprite_sheet, baller.w_img * counter_baller / 21, 0, baller.w_img/21 , baller.h_img);
counter_baller++;
if(y_player < 140){
y_player = 140;
}else if (y_player > 245) {
y_player = 240;
}
x_off += moveSpeed1;
x_player +=1;
al_rest(0.008);
}
if(playerStopping){
if (counter_stopping >= 8){
// playerStopping = false;
counter_stopping = 7;
}
// if(x_player = )
baller.image = al_create_sub_bitmap(baller2.sprite_sheet, baller2.w_img * counter_stopping / 8, 0, baller2.w_img/8 , baller2.h_img/2);
counter_stopping ++;
if(y_player < 120){
y_player = 120;
}else if (y_player > 245) {
y_player = 240;
}
if(animation_count < jumpRange){
x_off += 2;
x_player -= 2;
animation_count += 1;
if(x_player < 20)
jumpRange = 0;
}else{
animation_count = 0;
jumpRange = 0;
}
x_off += keys[LEFT] * (moveSpeed1 -2);
x_player -= keys[LEFT] * (moveSpeed1 -2);
}
if(playerDunking){
int dunkingSpeed = 20;
int y_before_jumping;
int x_before_jumping;
//get the initial y position
if(counter_dunking == 0){
y_before_jumping = y_player;
x_before_jumping = x_player;
//get the distance between player and hoop to get the accuracy
jumpDistance = hoop.positionX - x_player;
jumpDistance2 = hoop2.positionX - x_player;
}
if(counter_dunking >= 11){
if(counter_dunking == 16){
playerDunking = false;
playerStopping = true;
y_player = y_player - 20 * counter_dunking + 4 * pow(counter_dunking,2);
y_player = y_before_jumping;
}
}
else{
x_player += dunkingSpeed + longDunk * 20;
jumpRange +=dunkingSpeed;
y_player = y_player - 30 * counter_dunking + 4 * pow(counter_dunking,2);
}
baller.image = al_create_sub_bitmap (baller3.sprite_sheet,baller3.w_img * counter_dunking / 17 ,0, baller3.w_img/17,baller3.h_img);
counter_dunking++;
al_rest(0.07);
}
if(intro){
al_draw_text(font_25_man,text_col_green , 50,50,0,"LCTRL + H for help");
al_draw_text(font_25_man,text_col_green , 50,80,0,"LCTRL + M to switch music on/off");
al_draw_multiline_text(font_100_tarrget,text_col,540,textCenterY + 120,600,70,ALLEGRO_ALIGN_CENTER, "dunk it together");
al_draw_text(font_30_man,text_col,560,textCenterY + 300,ALLEGRO_ALIGN_CENTER,"please hold on a RIGHT key to start");
}
//////////////////////////////////////////// GAME PLAY ////////////////////////////////////////////
//////////////////////////////////// Gameplay is divided into checkpoints (10 - 16) //////////////////
//////////////////////////////////// Each checkpoint is unique //////////////////
//////////////////////////////////// The game restarts from the most recent checkpoint //////////////////
if (gamePlay){
//finite States for dodging the car closely,or dunking over the car are BELOW
if(carState == CLOSE){
//show feedback only when user is dodging with either UP or DOWN
if(keys[UP] || keys[DOWN]){
if(i_rand == 0){
al_draw_text(font_25_man , text_col_red, x_player, y_player - 40, 0, "NICE DODGE");
}
else if (i_rand == 1) {
al_draw_text(font_25_man , text_col_red, x_player, y_player - 40, 0, "GOOD DODGE");
}
else if (i_rand == 2) {
al_draw_text(font_25_man , text_col_red, x_player, y_player - 40, 0, "AMAZING");
}
else if (i_rand == 3) {
al_draw_text(font_25_man , text_col_red, x_player, y_player - 40, 0, "EXCELENT SKILLS");
}
else{
al_draw_text(font_25_man , text_col_red, x_player, y_player - 40, 0, "CLOSE ONE");
}
}
else {
changeCarState(carState,FAR);
}
}
//NORMAL
else {
//collision detection for dodging cars
if(!playerDunking && baller.bottom[3] > objects[0].y1 - 40 && baller.bottom[1] < objects[0].y2 && baller.bottom[2] > objects[0].x1 -25 && baller.bottom[0] < objects[0].x2 ){
changeCarState(carState,CLOSE);
//refresh the random number
i_rand = rand() % 5;
}
if(!playerDunking && baller.bottom[3] > objects[2].y1 && baller.bottom[1] < objects[2].y2 + 40 && baller.bottom[2] > objects[2].x1 -25 && baller.bottom[0] < objects[2].x2 ){
changeCarState(carState,CLOSE);
//refresh the random number
i_rand = rand() % 5;
}
}
//finite states for dunks mostly, and for moving the hoop if necessary
if(hoopState == REG_DUNK){
//perfect range for perfect dunking
if ( jumpDistance <= 330 && jumpDistance >=325){
al_draw_text(font_30_man , text_col_red, x_player, y_player - 80, 0, "PERFECT DUNK !!!");
}else{
//print out feedback randomly
if(i_rand == 0)
al_draw_text(font_30_man , text_col_red, x_player, y_player - 80, 0, "GOOD DUNK");
else if (i_rand == 1) {
al_draw_text(font_30_man , text_col_red, x_player, y_player - 80, 0, "FAIR DUNK");
}else{
al_draw_text(font_30_man , text_col_red, x_player, y_player - 80, 0, "LOOK AT YA");
}
}
}
else if (hoopState == LONGDUNK) {
al_draw_text(font_30_man , text_col_red, x_player, y_player - 80, 0, "OMG, YOU ARE KILLING IT !!!");
}
else if (hoopState == TOO_MANY_ATTEMPT) {
if(i_rand == 0)
al_draw_textf(font_30_man , text_col_red, 200, 200, 0, "%d/%d - WE CAN CHANGE THE 0",madeNum,attemptNum);
else
al_draw_text(font_30_man , text_col_red, 200, 200, 0, "COME ON, I KNOW YOU CAN DO THIS");
}
else if (hoopState == FAIR_ATTEMPTS) {
al_draw_text(font_30_man , text_col_red, 200, 200, 0, "I KNOW YOU CAN DO BETTER");
}
else if (hoopState == GOOD_ATTEMPTS) {
if(i_rand == 0){
al_draw_text(font_30_man , text_col_red, 200, 200, 0, "YOU ARE DOING GOOD");
}else if (i_rand == 1) {
al_draw_text(font_30_man , text_col_red, 200, 200, 0, "KEEP PUSHING");
}else{
al_draw_text(font_30_man , text_col_red, 200, 200, 0, "YOU GOT THIS");
}
}
else if (hoopState == PERFECT_ATTEMPTS) {
al_draw_textf(font_30_man , text_col_red, 200, 200, 0, "%d OUT OF %d YOU ARE ON POINT!!!", madeNum,attemptNum);
}
//move the hoop itself if the player is already past the distance for succesful dunks
else if (hoopState == TOO_CLOSE) {
if(hoopNum == 1){
hoop2.positionX -=moveSpeed1;
}
if(hoopNum == 2){
hoop.positionX -= moveSpeed1;
}
}
//NORMAL
else{
//refresh the random number for getting a feedback on dunks
i_rand = rand() % 3;
// get the ratio of number of succesful dunks over number of dunk attempts
double percentage = 1.0 * madeNum/attemptNum;
//give feedback to user depending on their success percentage
if(percentage >= 0.5){
if (percentage > 0.9)
changeHoopState(hoopState,PERFECT_ATTEMPTS);
else
changeHoopState(hoopState, GOOD_ATTEMPTS);
}else if( percentage >= 0.3 && percentage < 0.5){
changeHoopState(hoopState,FAIR_ATTEMPTS);
}else if(percentage >= 0.1 && percentage < 0.3){
changeHoopState(hoopState, POOR_ATTEMPTS);
}else{
//if the user doesn't make dunk after more than 5 attempts
if(attemptNum >= 5) changeHoopState(hoopState,TOO_MANY_ATTEMPT);
}
}
//if the hoop positions are less than x = 320, too late
if(!hoopEffect && hoop.positionX < 320){
changeHoopState(hoopState,TOO_CLOSE);
hoopNum = 2;
}
if(!hoopEffect && hoop2.positionX < 320){
changeHoopState(hoopState,TOO_CLOSE);
hoopNum = 1;
}
textX -= playerDribbling * moveSpeed1;
if(jumpRange)
textX -= playerStopping * 2;
if(hoop.counter == 13)
hoop.counter = 5;
if(hoop2.counter == 13)
hoop2.counter = 5;
if(!longDunk){
//collision detection for dunking
if(jumpDistance< 344 && jumpDistance > 322 && baller.top[3]> hoop.y1 && baller.top[1] < hoop.y2 && baller.top[2] > hoop.x1 && baller.top[0] < hoop.x2 ){
//if player dunked on the current hoop already, don't increment the score
if(!hoopEffect) {
dunkMade = true;
changeHoopState(hoopState,REG_DUNK);
}
}
if(jumpDistance2 < 344 && jumpDistance2 > 322 && baller.top[3]> hoop2.y1 && baller.top[1] < hoop2.y2 && baller.top[2] > hoop2.x1 && baller.top[0] < hoop2.x2 ){
//if player dunked on the current hoop already, don't increment the score
if(!hoopEffect) {
dunkMade = true;
changeHoopState(hoopState,REG_DUNK);
}
}
}else{
//collision detection for long ranged dunking
if(baller.top[3]> hoop.y1 && baller.top[1] < hoop.y2 && baller.top[2] > hoop.x1 && baller.top[0] < hoop.x2 ){
//if player dunked on the current hoop already, don't increment the score
if(!hoopEffect) {
dunkMade = true;
changeHoopState(hoopState,LONGDUNK);
}
}
if(baller.top[3]> hoop2.y1 && baller.top[1] < hoop2.y2 && baller.top[2] > hoop2.x1 && baller.top[0] < hoop2.x2 ){
//if player dunked on the current hoop already, don't increment the score
if(!hoopEffect) {
dunkMade = true;
changeHoopState(hoopState,LONGDUNK);
}
}
}
if(checkPoint >= 11){
//car in the back
if(objects[1].draw){
if(objects[1].counter == objects[1].sprite_count)
objects[1].counter = 0;
al_draw_bitmap_region(objects[1].sprite_sheet,objects[1].w_img * objects[1].counter / objects[1].sprite_count , 0, objects[1].w_img/objects[1].sprite_count,objects[1].h_img,objects[1].positionX,objects[1].positionY,0);
objects[1].counter += 1;
objects[1].positionX += moveSpeed1;
}
if(objects[1].positionX > WIDTH + 10){
objects[1].positionX = (-rand()%200) - 100 ;
}
//draw player before the car in middle when they move up
if(drawPlayerBack)
al_draw_bitmap(baller.image, x_player,y_player,0);
//car in the middle
if(objects[2].draw){
if(objects[2].counter == objects[2].sprite_count)
objects[2].counter = 0;
al_draw_bitmap_region(objects[2].sprite_sheet,objects[2].w_img * objects[2].counter / objects[2].sprite_count , 0, objects[2].w_img/objects[2].sprite_count,objects[2].h_img,objects[2].positionX,objects[2].positionY,0);
objects[2].counter += 1;
objects[2].positionX -= moveSpeed;
if(objects[2].positionX < - 1000){
objects[2].positionX = rand() % 1800 + WIDTH;
}
}
}
//checkpoint 1.0
if(checkPoint == 10){
al_draw_text(font_30_man,text_col,textX + 100,textCenterY + 200,ALLEGRO_ALIGN_CENTER,"Hold on SPACE to dunk and LSHIFT + SPACE for longer dunks");
al_draw_text(font_30_man,text_col,textX + 1000,textCenterY + 200,ALLEGRO_ALIGN_CENTER,"Use LEFT to slide faster");
//hoop animation below
al_draw_bitmap_region(hoop.sprite_sheet,0,hoop.h_img * hoop.counter / 14 , hoop.w_img,hoop.h_img/14,hoop.positionX, hoop.positionY ,0);
if(dunkMade){
hoopEffect = true;
madeNum++;
dunkMade = false;
}
if(hoopEffect){
hoop.counter += 1;
hoop.positionX -= moveSpeed2;
hoop.positionX -= playerDribbling * (moveSpeed1 - moveSpeed2);
if(hoop.positionX < -150) {
current_x_off = x_off;
hoopEffect = false;
checkPoint ++;
}
}else{
hoop.positionX -= playerDribbling * moveSpeed1;
if(hoop.positionX < - 100){
collided = true;
hoop.positionX = WIDTH;
}
}
}
//checkPoint - 1.1
if(checkPoint == 11){
if(textX < -50)
textX = WIDTH;
al_draw_multiline_text(font_40_man,text_col,textX - 200,textCenterY,400,50,0,"please use arrow keys UP,DOWN to slow down and dodge objects");
if(x_off > current_x_off + 300 ){
//if the car in the middle passes, advance to the next checkpoint
if(objects[2].positionX < - 200){
checkPoint ++;
}
}
}
//checkpoint 1.2
if(checkPoint == 12 ){
//hoop becomes green - avialable for dunk - when only the car passed the screen
if(!hoopEffect && objects[2].positionX < -50){
al_draw_bitmap(hoop_green.image,hoop2.positionX, hoop2.positionY ,0);
}else{
al_draw_bitmap_region(hoop2.sprite_sheet,0,hoop2.h_img * hoop2.counter / 14 , hoop2.w_img,hoop2.h_img/14,hoop2.positionX, hoop2.positionY ,0);
}
al_draw_text(font_30_man , text_col, 50 ,120, 0, "Dunk when the rim is green");
//hoop animation below
if(dunkMade ){
//advance only when the rim is green
if(objects[2].positionX < -50){
hoopEffect = true;
madeNum++;
dunkMade = false;
}else {
dunkMade = false;
}
}
if(hoopEffect){
hoop2.counter += 1;
hoop2.positionX -= moveSpeed1;
hoop2.positionX -= playerDribbling * (moveSpeed1 - moveSpeed2);
if(hoop2.positionX < -150) {
// current_x_off = x_off;
hoopEffect = false;
checkPoint++;
//reset hoop positions
hoop2.positionX = WIDTH +200;
hoop.positionX = WIDTH +700;
}
}else{
hoop2.positionX -= playerDribbling * moveSpeed1;
if(hoop2.positionX < -100){
collided = true;
getRandNum = true;