-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1.c
2525 lines (2263 loc) · 66.6 KB
/
p1.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
/*
CSE 102 - SUMMER HOMEWORK
131044023 - MUHAMMED SELIM DURSUN
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.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>
#define WIDTH 460 /* width of screen */
#define HEIGHT 710 /* height of screen */
#define FPS 60 /* frame per second */
#define ONE_SIDE_R 59 /* Side of rectangular */
#define ONE_SIDE_T 56 /* Side of triangle */
#define HOR_OBJECT_NUM 7 /* Number Of horizontal object*/
#define VER_OBJECT_NUM 9 /* Number Of vertical object */
#define CIRCLE_RADIUS 8 /* Ball's Radius */
#define MAX_BALL_SPEED 8.5 /* Ball's x or y axis's max */
#define MIN_BALL_SPEED 8 /* Ball's x or y axis's min */
#define MAX_POSSIBILITY 112649 /* max pos. in object.txt */
#define ADD_DESTROYER_R 17.5/* adding ball ve destroyer radius*/
#define BOUNCE_GAP 9 /* it should be max ball speed + object thickness */
#define OBJECT_THICKNESS 3 /* objects's(triangles and rectangulars) thinckness*/
#define OBJECT_SCROLL_STEP 66
#define SCORES_TXT "objects/txts/scores.txt"
#define OBJECTS_TXT "objects/txts/object.txt"
/*this variables changing when in game loops */
int LEVEL = 1; /*The initial number of level*/
int NUMBER_OF_BALL = 1; /*The initial number of balls*/
int REMAINING_BALL = 0; /*number of will send ball*/
int TOP_SCORE = 0; /*maximum level between played games*/
int SOUND = 1 ; /*1 is sound_on - 0 is sound_off */
bool CLOSE = false; /* if true stops all loop */
int NEW_INDEX = 0; /* The index of first ball arrived to the starting line */
int NEW_BALL = 0; /*The number of add ball collected in a round*/
int FRAMES = 0; /*total frame number the beginning*/
int VISIBLE = 0; /*flashing efect variable*/
double new_ball_x = 230, new_ball_y = 640; /*The initial position of the balls*/
typedef struct mouse {
double first_mouse_x, first_mouse_y; /*when initialize mouse click down*/
double second_mouse_x, second_mouse_y; /*when initialize mouse click up*/
} mouse_t;
/*
side 1 > up to left
side 2 > down to left
side 3 > up to right
side 4 > down to right
side 5 > down to up
side 6 > up to down
side 7 > right to left
side 8 > left to right
*/
typedef struct ball{
int side; /* direction the ball will go */
double x; /* ball's X location on map */
double y; /* ball's Y location on map */
double axis_x; /* the way the ball will be in the x-axis */
double axis_y; /* the way the ball will be in the y-axis */
bool life; /* if ball in map and active or inactive */
bool destroy; /* is ball go into the destroyer */
bool circulation;
} ball_t;
enum ball_life{ DIE, LIVING};
/*
object>>everything on map (squares,triangles,ball adder,destroyer)
object ID 0 > square
ID 1 > triangle, hypotenuse facing left
ID 2 > triangle, hypotenuse facing right
ID 3 > ball adder
ID 4 > horizontal destroyer
ID 5 > vertical destroyer
*/
typedef struct object
{
int x; /* object's X location on map */
int y; /* object's Y location on map */
int health; /* object's health */
int ID; /* object's ID () */
bool visible; /* if ball bounce it, object is shine momentary*/
} object_t;
/* The man throws the ball */
typedef struct man
{
int x; /* Man bitmap's X location */
int y; /* Man bitmap's Y location */
} man_t;
/* The coordinates of number of balls remaining to will be send */
typedef struct remaining_ball
{
int x;
int y;
} remaining_ball_t;
int menu();
/*
void game_loop();
updating main variables (levels, ball number vs) and call inner_game_loop func.
allocating memory for ball structure
*/
void game_loop();
/*
void inner_game_loop(ball_t *ball, remaining_ball_t remaining_ball, man_t *man);
The main operations are done here; sending the ball process, balls
collision with objects and walls, balls moving, scroll object...
*/
void inner_game_loop(ball_t *ball, remaining_ball_t remaining_ball, man_t *man);
/*
void new_level_scheme();
Pc keep the number of rand and takes mode by MAX_POSSIBILITY (as defined 112649)
and read object.txt (previously created, txt layout as follows below)
if read number equal kept number, read follows seven number.
if number 0 or 1, will draw space
if number 2 or 3, will draw square
if number 4, will draw triangle
if number 5, will draw ball adder
if number 6, will draw horizontal destroyer
if number 7, will draw vertical destroyer
According to this, init object ID and coordinates.
Squares ID 0
Triangles ID 1
Ball Adder ID 2
Vertical or Horizontal destroyer ID 3
object.txt layout
all sequence have max two triangles
all sequence have at least one squares
all sequence have at least 3 object, most 7 object
all sequence have one ball adder
all sequence have max one destroyer and one out of every five
(destroyer in which mode 5)
*/
void new_level_scheme();
/*
void init_mouse_axis( mouse_t *mouse );
init all mouse struct data
*/
void init_mouse_axis( mouse_t *mouse );
/*
void init_ball( ball_t *ball );
init all ball struct data
*/
void init_ball( ball_t *ball );
/*
void init_object();
init all objects's data
*/
void init_object();
/*
void init_colors_fonts_bitmaps_voices();
init all colors, fonts, bitmaps and voices
*/
void init_colors_fonts_bitmaps_voices();
/*
void find_top_score();
this function read scores.txt and find the high score
*/
void find_top_score();
/*
void scroll_object_id();
Carries all the object data to a lower row at the end of each round
*/
void scroll_object_id();
/*
void ball_speed_fixing( ball_t *ball );
ball's speed should be between BOUNCE_GAP and MIN_BALL_SPEED
that function fix all ball's speed
*/
void ball_speed_fixing( ball_t *ball );
/*
void color_changer( ALLEGRO_COLOR *color, int health );
change pointer color by the number of objects
*/
void color_changer( ALLEGRO_COLOR *color, int health );
/*
void draw_text(remaining_ball_t remaining_ball);
draw all required text and numbers
*/
void draw_text(remaining_ball_t remaining_ball);
/*
double distance_func( int x1, int y1, int x2, int y2 );
calculate between two coordinates
return double distance of between two coordinates
*/
double distance_func( int x1, int y1, int x2, int y2 );
/*
void change_location( ball_t *ball );
this function change ball's current location by ball's side
*/
void change_location( ball_t *ball );
/*
void adding_ball_control( ball_t *ball, int i, int j );
check that ball is within adding ball obje coordinates or not.
if yes, a number of balls is increased
*/
void adding_ball_control( ball_t *ball, int i, int j );
/*
void square_ball_control( ball_t *ball, int i, int j );
check between square and ball distance
if two objects touch, change ball's side and 1 point reduce square health
*/
void square_ball_control( ball_t *ball, int i, int j );
/*
void triangle_ball_control( ball_t *ball, int i, int j );
check between triangle and ball distance
if two objects touch, change ball's side and 1 point reduce square health
*/
void triangle_ball_control( ball_t *ball, int i, int j );
/*
void destroyer_control( ball_t *ball, int i, int j );
check that destroyers did it cut any object or not
*/
void destroyer_control( ball_t *ball, int i, int j );
/*
void is_destroyer_used();
check that any ball exist on the destroyer and cut any object
*/
void is_destroyer_used();
/*
void bounce_from_wall( ball_t *ball );
control that ball bounce wall or not
if ball enough near to wall, bounce as it's supposed to be
*/
void bounce_from_wall( ball_t *ball );
/*
int is_balls_inlife(ball_t *ball);
check that are all balls DIE or not.
if all balls die;
return 0;
else
return 1;
*/
int is_balls_inlife(ball_t *ball);
/*
int is_game_over();
check game is over or not
return 1 game not over
return 0 game over
*/
int is_game_over();
/*
void destroy_process();
destroy all allegro proccess
*/
void destroy_process();
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *whitrabt_font30 = NULL;
ALLEGRO_FONT *batman_font23 = NULL;
ALLEGRO_FONT *arial_font20 = NULL;
ALLEGRO_FONT *arial_font17 = NULL;
ALLEGRO_FONT *whitrabt_font35 = NULL;
ALLEGRO_MOUSE_STATE state;
ALLEGRO_BITMAP *player = NULL;
ALLEGRO_BITMAP *game_backround = NULL;
ALLEGRO_BITMAP *bitmap_ball = NULL;
ALLEGRO_BITMAP *destroyer_hor = NULL;
ALLEGRO_BITMAP *destroyer_ver = NULL;
ALLEGRO_BITMAP *adding_ball = NULL;
ALLEGRO_BITMAP *menu_backround = NULL;
ALLEGRO_BITMAP *menu_man = NULL;
ALLEGRO_BITMAP *play_again = NULL;
ALLEGRO_BITMAP *restart_backround = NULL;
ALLEGRO_BITMAP *exit_door = NULL;
ALLEGRO_BITMAP *sound_on = NULL;
ALLEGRO_BITMAP *sound_off = NULL;
ALLEGRO_SAMPLE *bounce_audio = NULL;
ALLEGRO_SAMPLE *game_over_audio = NULL;
ALLEGRO_SAMPLE *destroy_audio = NULL;
ALLEGRO_SAMPLE *add_ball_audio = NULL;
ALLEGRO_SAMPLE *entry_audio = NULL;
ALLEGRO_SAMPLE *mouse_on_button_audio = NULL;
ALLEGRO_SAMPLE *transitions_audio = NULL;
ALLEGRO_COLOR yellow;
ALLEGRO_COLOR light_yellow;
ALLEGRO_COLOR orange;
ALLEGRO_COLOR light_orange;
ALLEGRO_COLOR light_green;
ALLEGRO_COLOR red1;
ALLEGRO_COLOR red2;
ALLEGRO_COLOR red3;
ALLEGRO_COLOR red4;
ALLEGRO_COLOR red5;
ALLEGRO_COLOR red6;
ALLEGRO_COLOR blue1;
ALLEGRO_COLOR blue2;
ALLEGRO_COLOR blue3;
ALLEGRO_COLOR blue4;
ALLEGRO_COLOR purple;
ALLEGRO_COLOR cyan;
ALLEGRO_COLOR black;
ALLEGRO_COLOR white;
ALLEGRO_COLOR greenish_yellow;
ALLEGRO_COLOR spring_green;
/*
object: triangles, squares, ball adder, destroyer
object_t structure carries this objects's datas
globally defined as it used for each function
*/
object_t object[VER_OBJECT_NUM][HOR_OBJECT_NUM];
int main( int argc, char **argv ){
srand( time( NULL ) );
/* init and install allegro event */
if( !al_init() )
{
printf( "failed to initialize allegro!\n" );
}
/* creating display */
display = al_create_display( WIDTH, HEIGHT );
if( !display ) {
printf( "failed to create display!\n" );
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
al_init_image_addon();
al_install_mouse();
al_install_keyboard();
al_install_audio();
al_init_acodec_addon();
init_object();
init_colors_fonts_bitmaps_voices();
/* frequency will play at some time */
al_reserve_samples( 10 );
if( menu() )
game_loop();
/* destroy process */
destroy_process();
return 0;
} /*end of main.c */
int menu()
{
bool done = false; /*loop control variable*/
int man_x = 150, man_y = 116; /* moving man's starting coordinates */
int ball_x = 355, ball_y = 430; /* moving ball's starting coordinates */
bool m_left = false, m_right = true; /* limits of man's moving */
bool b_down = false, b_up = true; /* limits of ball's moving */
bool mouse_button = false; /* if mouse in play key, key's around shines */
event_queue = al_create_event_queue();
if( !event_queue ) {
printf( "failed to create event_queue!\n" );
return -1;
}
find_top_score();
timer = al_create_timer( 1.0 / FPS );
al_register_event_source(event_queue, al_get_mouse_event_source() );
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_start_timer( timer );
do
{
ALLEGRO_EVENT ev;
al_wait_for_event( event_queue, &ev );
al_get_mouse_state( &state );
if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
{
done = true;
al_play_sample(game_over_audio, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);
al_rest( 0.3 );
}
/*if click play button by mouse*/
if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button & 1)
{
if( ev.mouse.x > 254 && ev.mouse.x < 392 &&
ev.mouse.y > 499 && ev.mouse.y < 562 )
{
al_play_sample(entry_audio, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0 );
return 1;
}
}
if( al_is_event_queue_empty( event_queue ) )
{
/*man is moving*/
if( man_x != 50 && !m_left)
man_x -= 2;
else
{
m_left = true;
m_right = false;
}
if( man_x != 344 && !m_right )
man_x += 2;
else
{
m_left = false;
m_right = true;
}
/*ball is moving*/
if( ball_y != 490 && !b_down )
ball_y += 4;
else
{
b_down = true;
b_up = false;
}
if( ball_y != 430 && !b_up )
ball_y -= 3;
else
{
b_down = false;
b_up = true;
}
al_draw_bitmap( menu_backround, 0, 0, 0 );
/*play button coordinates*/
/*if mouse on play button coordinates */
if(state.x > 254 && state.x < 392 && state.y > 499 && state.y < 562)
{
al_draw_rectangle( 254, 499, 392, 562,
greenish_yellow, 5 );
if( !mouse_button )
{
al_play_sample( mouse_on_button_audio, 1, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
mouse_button = true;
}
}
else
mouse_button = false;
al_draw_bitmap( menu_man, man_x, man_y, 0 );
al_draw_filled_circle( ball_x, ball_y, CIRCLE_RADIUS, white );
al_flip_display();
al_clear_to_color(black);
}
}while( !done );
}
/*
updating main variables (levels, ball number vs) and call inner_game_loop func.
allocating memory for ball structure
*/
void game_loop()
{
FILE *inp_score;
double distance;
bool done = false;
bool key = false;
bool mouse_button = false;
ball_t *ball;
man_t man;
remaining_ball_t remaining_ball;
/* man's coordinates set specially the best position */
man.x = new_ball_x - 2;
man.y = new_ball_y - 55;
timer = al_create_timer( 1.0 / FPS );
/* register event source */
al_register_event_source( event_queue, al_get_mouse_event_source());
al_register_event_source( event_queue, al_get_timer_event_source(timer));
al_register_event_source( event_queue, al_get_display_event_source(display));
al_start_timer(timer);
/* allocating memory */
ball = ( ball_t* )malloc( 300*sizeof( ball_t ) );
init_ball(ball);
find_top_score();
do{
/* number of the will send ball left the man */
REMAINING_BALL = NUMBER_OF_BALL;
remaining_ball.x = ball[NEW_INDEX].x;;
remaining_ball.y = ball[NEW_INDEX].y;
/*reallocating*/
if( LEVEL % 250 == 0 )
{
ball = (ball_t*)realloc(ball, 250*sizeof(ball_t));
}
/*init new level objects ID*/
new_level_scheme();
/*all objects scrool 1 step by horizontal */
scroll_object_id();
/*if ball in game area and active, this func is working*/
inner_game_loop(ball, remaining_ball, &man);
/*next level*/
LEVEL++;
/* if how many ball collect, so it should improve ball numbers */
NUMBER_OF_BALL += NEW_BALL;
NEW_BALL = 0;
} while( is_game_over() );
al_rest( 0.8 );
/*all scores saving in a file, appeding score after game over*/
inp_score = fopen(SCORES_TXT, "a");
fprintf( inp_score, "%d\n", LEVEL - 1 );
fclose( inp_score );
if( CLOSE ) return;
/*
will play screen when game over.
User have to decide 'play again' or 'exit' game
*/
do{
ALLEGRO_EVENT ev;
al_wait_for_event( event_queue, &ev );
al_get_mouse_state( &state );
/* distance between mouse and play again key */
distance = sqrt( pow( state.x - 225, 2 ) + pow( state.y - 360, 2 ) );
if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
{
done = true;
if( SOUND )
{
al_play_sample( game_over_audio, 1, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
al_rest( 0.3 );
}
}
else if( ev.type == ALLEGRO_EVENT_TIMER )
{
key = true;
al_draw_bitmap( restart_backround, 0, 0, 0 );
al_draw_bitmap( play_again, 140, 250, 0 );
al_draw_bitmap( exit_door, 184, 580, 0 );
/*if mouse on exit button*/
if( state.x > 188 && state.x < 276 && state.y > 583 && state.y < 710 )
{
al_draw_rectangle( 188, 580, 273, 710,
greenish_yellow, 5 );
if( !mouse_button && SOUND )
{
al_play_sample( mouse_on_button_audio, 1, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
mouse_button = true;
}
}
/*if mouse on play button */
else if( distance <= 60 )
{
al_draw_circle( 225, 360, 60, spring_green, 10 );
if( !mouse_button && SOUND )
{
al_play_sample( mouse_on_button_audio, 1, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
mouse_button = true;
}
}
else
mouse_button = false;
}
if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
{
if( ev.mouse.button & 1 )
{
/*if click exit button*/
if( state.x > 188 && state.x < 276 &&
state.y > 583 && state.y < 710 )
{
done = true;
if( SOUND )
{
al_play_sample( game_over_audio, 1, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
al_rest( 0.3 );
}
}
/*if click play again button*/
else if( distance <= 60 )
{
done = true;
init_object();
LEVEL = 1;
NUMBER_OF_BALL = 1;
game_loop(ball);
return;
}
}
}
if( key && al_is_event_queue_empty( event_queue ) )
{
key = false;
al_flip_display();
al_clear_to_color(black);
}
}while( !done );
free(ball);
}
/*
The main operations are done here; sending the ball process, balls
collision with objects and walls, balls moving, scroll object...
*/
void inner_game_loop(ball_t *ball, remaining_ball_t remaining_ball, man_t *man)
{
int j, i, k, l, m; /*loop control variables*/
int distance; /*distance between balls and objects*/
bool key = false; /* provide that loop working yield */
bool done = false; /*loop termination variable */
bool flag = false; /*permisson var. for check that ball die or alive*/
bool permisson = false; /*give permisson after objects scroll*/
bool GAME = false; /* check that all balls send or not */
bool TICKET = false; /*check man is where it should be or not*/
bool transitions = false; /*transition sound control variable*/
bool flag2 = false;
ALLEGRO_COLOR color; /*objects's color*/
mouse_t mouse;
init_mouse_axis( &mouse );
init_ball( ball );
/*initialization*/
i = 0;
j = 0;
k = 0;
l = 0;
m = 0;
do
{
ALLEGRO_EVENT ev;
al_wait_for_event( event_queue, &ev );
al_get_mouse_state( &state );
if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
{
done = true;
CLOSE = true;
}
/*objects scrolling*/
if( permisson == false )
{
if( !transitions && SOUND )
{
al_play_sample( transitions_audio, 3, 0, 1,
ALLEGRO_PLAYMODE_ONCE, 0 );
transitions = true;
}
for( i = 0; i < VER_OBJECT_NUM; i++ )
{
for( j = 0; j < HOR_OBJECT_NUM; j++ )
{
if( object[i][j].ID != -1 &&
object[i][j].y != -1 &&
object[i][j].x != -1 )
{
object[i][j].y += 1;
}
}
}
m++;
}
if( m == OBJECT_SCROLL_STEP )
{
if( !is_game_over() )
done = true;
else
permisson = true;
}
if( permisson )
{
if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
{
if( ev.mouse.button & 1 )
{
mouse.first_mouse_x = ev.mouse.x;
mouse.first_mouse_y = ev.mouse.y;
}
if( ev.mouse.button & 1 &&
state.x > 5 && state.x < 50 &&
state.y > 5 && state.y < 50 )
{
if( SOUND )
SOUND = 0;
else
SOUND = 1;
}
}
else if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP )
{
if( ev.mouse.button & 1 )
{
mouse.second_mouse_x = ev.mouse.x;
mouse.second_mouse_y = ev.mouse.y;
flag = true;
}
}
}
if( ev.type == ALLEGRO_EVENT_TIMER )
{
key = true;
if( mouse.first_mouse_x != 0 && mouse.second_mouse_x != 0 )
{
if( mouse.second_mouse_y > mouse.first_mouse_y &&
ball[0].axis_x == 0 && ball[0].axis_y == 0 )
{
for( j = 0; j < NUMBER_OF_BALL; j++ )
{
ball[j].axis_x = ( mouse.first_mouse_x - mouse.second_mouse_x );
ball[j].axis_y = ( mouse.first_mouse_y - mouse.second_mouse_y );
ball_speed_fixing( &ball[j] );
if( mouse.first_mouse_x > mouse.second_mouse_x )
{
ball[j].side = 3;
}
else if( mouse.first_mouse_x < mouse.second_mouse_x )
{
ball[j].side = 1;
}
else if( mouse.first_mouse_x == mouse.second_mouse_x &&
mouse.first_mouse_y < mouse.second_mouse_y )
{
ball[j].side = 5;
}
}
init_mouse_axis( &mouse );
}
else
init_mouse_axis( &mouse );
}
}
if( key && al_is_event_queue_empty( event_queue ) )
{
FRAMES++;
key = false;
al_draw_bitmap( game_backround, 0, 0, 0 );
if( FRAMES % 6 == 0 && ball[0].side!= -1 && l < NUMBER_OF_BALL )
{
ball[l].life = LIVING;
ball[l].circulation = true;;
REMAINING_BALL -= 1;
l++;
}
if( l == NUMBER_OF_BALL )
{
GAME = true;
}
/*Controls the ball against the whole object here is done
one by one.
*/
for( i = 0; i < NUMBER_OF_BALL; i++ )
{
while(!ball[i].life && i < NUMBER_OF_BALL)
i++;
for( k = 1; k < VER_OBJECT_NUM; k++ )
{
for( j = 0; j < HOR_OBJECT_NUM; j++ )
{
color_changer( &color, object[k][j].health );
if( object[k][j].ID == 0 && object[k][j].health > 0 )
{
if( object[k][j].health < 10 )
al_draw_textf( arial_font20, color,
object[k][j].x + 23,
object[k][j].y + 15, 0,
"%d", object[k][j].health );
else if( object[k][j].health < 100 )
al_draw_textf( arial_font20, color,
object[k][j].x + 17,
object[k][j].y + 15, 0,
"%d", object[k][j].health );
else if( object[k][j].health >= 100 )
al_draw_textf( arial_font20, color,
object[k][j].x + 11,
object[k][j].y + 15, 0,
"%d", object[k][j].health );
if( ball[i].life == LIVING )
{
square_ball_control( &ball[i], k, j );
}
if( object[k][j].visible )
{
al_draw_rectangle( object[k][j].x,
object[k][j].y,
object[k][j].x + ONE_SIDE_R,
object[k][j].y + ONE_SIDE_R,
color, OBJECT_THICKNESS );
}
if( !object[k][j].visible && FRAMES == VISIBLE + 1 )
{
object[k][j].visible = true;
}
}
else if( object[k][j].ID == 1 && object[k][j].health > 0)
{
if( object[k][j].health < 10 )
al_draw_textf( arial_font20, color,
object[k][j].x + 38,
object[k][j].y + 4, 0,
"%d", object[k][j].health );
else if( object[k][j].health < 100 )
al_draw_textf( arial_font20, color,
object[k][j].x + 31,
object[k][j].y + 4, 0,
"%d", object[k][j].health );
else if( object[k][j].health >= 100 )
al_draw_textf( arial_font20, color,
object[k][j].x + 21,
object[k][j].y + 4, 0,
"%d", object[k][j].health );
if( ball[i].life == LIVING )
{
triangle_ball_control( &ball[i], k, j );
}
if( object[k][j].visible )
al_draw_triangle( object[k][j].x,
object[k][j].y,
object[k][j].x + ONE_SIDE_T,
object[k][j].y,
object[k][j].x + ONE_SIDE_T,
object[k][j].y + ONE_SIDE_T,
color, OBJECT_THICKNESS );
if( !object[k][j].visible && FRAMES == VISIBLE + 1 )
{
object[k][j].visible = true;
}
}
else if( object[k][j].ID == 2 && object[k][j].health > 0 )
{
al_draw_textf( arial_font20, color,
object[k][j].x + 3,
object[k][j].y + 3, 0,
"%d", object[k][j].health );
if( ball[i].life == LIVING )
{
triangle_ball_control( &ball[i], k, j );
}
if( object[k][j].visible == true )
al_draw_triangle( object[k][j].x,
object[k][j].y,
object[k][j].x + ONE_SIDE_T,
object[k][j].y, object[k][j].x,
object[k][j].y + ONE_SIDE_T,
color, OBJECT_THICKNESS );
if( !object[k][j].visible && FRAMES == VISIBLE + 1)
{
object[k][j].visible = true;
}
}
else if( object[k][j].ID == 3 )
{
al_draw_bitmap( adding_ball, object[k][j].x,
object[k][j].y, 0 );
if( ball[i].life == LIVING )
{
adding_ball_control( &ball[i], k, j );
}
}
else if( object[k][j].ID == 4 )
{
al_draw_bitmap( destroyer_hor, object[k][j].x,
object[k][j].y, 0 );
if( ball[i].life == LIVING )
{
destroyer_control( &ball[i], k, j );
}
}
else if( object[k][j].ID == 5 )
{
al_draw_bitmap( destroyer_ver, object[k][j].x,
object[k][j].y, 0 );
if( ball[i].life == LIVING )
{
destroyer_control( &ball[i], k, j );
}
}
/* Init the deceased objects*/
else if(object[k][j].ID==0 || object[k][j].ID==1 ||
object[k][j].ID==2)
{
if(object[k][j].health<=0)
{
object[k][j].x = -1;
object[k][j].y = -1;
object[k][j].ID = -1;