1
2/***********************
3 GAME LOOP
4************************/
5 while(!done
)
6 {
7 ALLEGRO_EVENT ev
;
8 al_wait_for_event(event_queue,
&ev
);
9
10
11 time_remaining
--;
12 if((time_remaining
/60) %
3 == 0)
13 inc_speed
+=0.
001;
14
15
16 if(ev.type
== ALLEGRO_EVENT_KEY_DOWN
)
17 {
18 switch(ev.keyboard.keycode
)
19 {
20 case ALLEGRO_KEY_LEFT:
21 keys
[LEFT
] = true;
22 break;
23 case ALLEGRO_KEY_RIGHT:
24 keys
[RIGHT
] = true;
25 break;
26 case ALLEGRO_KEY_SPACE:
27 keys
[SPACE
] = true;
28 if(state
== TITLE
)
29 ChangeState
(state, PLAYING
);
30 else if(state
== LOST
)
31 ChangeState
(state, PLAYING
);
32
33 break;
34
35 }
36
37 }
38 else if(ev.type
== ALLEGRO_EVENT_KEY_UP
)
39 {
40 switch(ev.keyboard.keycode
)
41 {
42 case ALLEGRO_KEY_LEFT:
43 keys
[LEFT
] = false;
44 break;
45 case ALLEGRO_KEY_RIGHT:
46 keys
[RIGHT
] = false;
47 break;
48 case ALLEGRO_KEY_ESCAPE:
49 done
=true;
50 break;
51 case ALLEGRO_KEY_SPACE:
52 keys
[SPACE
] = false;
53 break;
54 }
55 }
56 else if(ev.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
57 {
58 done
= true;
59 }
60
61 /***********************
62 UPDATE
63 ************************/
64
65
66 else if(ev.type
== ALLEGRO_EVENT_TIMER
)
67 {
68 redraw
= true;
69 /*if(keys[LEFT])
70 MoveLeftBall(shapeB);
71 if(keys[RIGHT])
72 MoveRightBall(shapeB);*/
73
74
75
76 if (shapeB.isOnGround
|| shapeB.isOnSquare
)
77 { // means the ball is on the ground
78 if(keys
[SPACE
])
79 {
80 if (shapeB.canJump
) //CAN JUMP
81 {
82 shapeB.dy
= -10; //VELOCITY UPWARDS
83 shapeB.canJump
= false;
84 }
85 }
86 else
87 shapeB.canJump
= true;
88 }
89
90
91
92 if(!shapeB.isOnGround
|| !shapeB.isOnSquare
)
93 {
94 shapeB.dy
+= 0.
5;
95 speed
= 5+inc_speed
;
96 }
97 else
98 speed
= 3+inc_speed
;
99
100 if (keys
[SPACE
] && shapeB.isOnGround
&& shapeB.isOnSquare
&& shapeB.dy
> 0)
101 shapeB.dy
-= 0.
1; // got to be LESS than 0.5 here.
102
103 if(shapeB.dy>5
)
104 shapeB.dy
= 5;
105
106 shapeB.pos_Y
+= shapeB.dy
;
107
108 if(shapeB.pos_Y
> HEIGHT-20
)
109 {
110 shapeB.pos_Y
= HEIGHT-20
;
111 shapeB.dy
= 0;
112 }
113
114
115 if(state
== TITLE
)
116 {}
117 else if(state
== PLAYING
)
118 {
119 if(shapeB.LeftCollision
== false)
120 {
121
122 UpdateBackground
(BG
);
123
124 UpdateTriangle
(shapeT,NUM_TRIANGLES, shapeB
);
125
126 UpdateSquare
(shapeS,NUM_TRIANGLES, shapeB
);
127 }
128 UpdateExplosions
(explosions
);
129 UpdateBall
(shapeB
);
130 StartTriangle
(shapeT, NUM_TRIANGLES
);
131 StartSquare
(shapeS, NUM_TRIANGLES
);
132 CollideTriangle
(shapeT,shapeS, NUM_TRIANGLES, shapeB, explosions
);
133 CollideSquare
(shapeS, NUM_TRIANGLES, shapeB, explosions
);
134 if(shapeB.lives
<=0 || time_remaining
< 1)
135 ChangeState
(state, LOST
);
136 }
137 else if(state
== LOST
)
138 {}
139
140
141 /***********************
142 RENDERING
143 ************************/
144
145
146 if(redraw
&& al_is_event_queue_empty(event_queue
))
147 {
148 redraw
= false;
149
150 if(state
== TITLE
)
151 {
152 al_draw_bitmap(title,
0 ,
0 ,
0);
153 }
154 else if(state
== PLAYING
)
155 {
156 DrawBackground
(BG
);
157 DrawBall
(shapeB
);
158 DrawTriangle
(shapeT,NUM_TRIANGLES
);
159 DrawSquare
(shapeS,NUM_TRIANGLES
);
160 DrawExplosions
(explosions
);
161
162 al_draw_textf(font18,
al_map_rgb(255,
255,
255),
0,
0,
0,
163 "Player has %i lives. Score : %i",shapeB.lives,shapeB.score
);
164
165 al_draw_textf(font18,
al_map_rgb(255,
255,
255),
600,
0,
0,
166 "Time Remaining: %i",time_remaining
/60);
167
168 }
169 else if(state
== LOST
)
170 {
171 al_draw_bitmap(lost,
0,
0,
0);
172 al_draw_textf(font18,
al_map_rgb(0,
255,
255),WIDTH
- 10 ,
20,ALLEGRO_ALIGN_RIGHT,
173 "Final Score : %i",shapeB.score
);
174 }
175
176
177 al_flip_display();
178 al_clear_to_color(al_map_rgb(0,
0,
0));
179
180 }
181 }
182
183 }
184
185 /**********************************
186 DESTROYING GAME OBJECTS
187 **********************************/
188
189 al_destroy_sample(boom
);
190 al_destroy_sample_instance(songInstance
);
191 al_destroy_bitmap(bgImage
);
192 al_destroy_bitmap(title
);
193 al_destroy_bitmap(lost
);
194 al_destroy_bitmap(expImage
);
195 al_destroy_bitmap(ballImage
);
196 al_destroy_event_queue(event_queue
);
197 al_destroy_timer(timer
);
198 al_destroy_font(font18
);
199 al_destroy_display(display
);
200
201 return 0;
202}
203
204
205
206void InitBall
(Ball
&shapeB,
ALLEGRO_BITMAP *image
= NULL
)
207{
208 shapeB.ID
=BALL
;
209 shapeB.pos_X
= WIDTH
/2;
210 shapeB.pos_Y
= HEIGHT
;
211 shapeB.lives
= 3;
212 shapeB.speed
= 6;
213 shapeB.score
= 0;
214 shapeB.boundx
= 23;
215 shapeB.boundy
= 16;
216 shapeB.dx
= 1;
217 shapeB.dy
= 1;
218 shapeB.canJump
= false;
219 shapeB.isOnGround
= true;
220 shapeB.isOnSquare
= false;
221 shapeB.LeftCollision
= false;
222
223 shapeB.maxFrame
= 8;
224 shapeB.curFrame
= 0;
225 shapeB.frameCount
= 0;
226 shapeB.frameDelay
= 5;
227 shapeB.frameWidth
= 64;
228 shapeB.frameHeight
= 48;
229 shapeB.animationColumns
= 8;
230 shapeB.animationDirection
= 1;
231 shapeB.animationRow
= 0;
232
233 time_remaining
= 10800; //A calculated value to display remainging time
234 speed
=0;
235 inc_speed
=0;
236
237 if(image
!= NULL
)
238 shapeB.image
= image
;
239
240}
241void DrawBall
(Ball
&shapeB
)
242{
243
244 int fx
= (shapeB.curFrame % shapeB.animationColumns
) * shapeB.frameWidth
;
245 int fy
= shapeB.animationRow
* shapeB.frameHeight
;
246
247 al_draw_bitmap_region(shapeB.image, fx, fy, shapeB.frameWidth,
248 shapeB.frameHeight,
(shapeB.pos_X
- shapeB.frameWidth
/2),
(shapeB.pos_Y
- shapeB.frameHeight
/2),
0);
249
250 //al_draw_filled_rectangle(shapeB.pos_X - shapeB.boundx, shapeB.pos_Y - shapeB.boundy,
251 // shapeB.pos_X + shapeB.boundx, shapeB.pos_Y + shapeB.boundy, al_map_rgba(255,0,255,100));
252
253
254}
255//void MoveLeftBall(Ball &shapeB)
256//{
257// shapeB.pos_X -=shapeB.speed;
258// if(shapeB.pos_X <(WIDTH/2) - 100)
259// shapeB.pos_X = (WIDTH/2) - 100;
260//}
261//void MoveRightBall(Ball &shapeB)
262//{
263// shapeB.pos_X += shapeB.speed;
264// if(shapeB.pos_X >(WIDTH/2)+100)
265// shapeB.pos_X = (WIDTH/2)+100 ;
266//}
267
268
269void UpdateBall
(Ball
&shapeB
)
270{
271 if(shapeB.pos_Y
< HEIGHT-20
)
272 shapeB.isOnGround
= false;
273 else
274 shapeB.isOnGround
= true;
275
276 if(!shapeB.LeftCollision
)
277 {
278 if(++shapeB.frameCount
>= shapeB.frameDelay
)
279 {
280 if(++shapeB.curFrame
>= shapeB.maxFrame
)
281 {
282 shapeB.curFrame
= 0;
283
284 }
285
286 shapeB.frameCount
= 0;
287
288 }
289
290 }
291}
292
293
294void InitTriangle
(Triangle shapeT
[],
int size
)
295{
296 for(int i
=0; i
<size
; i
++)
297 {
298 shapeT
[i
].ID
= TRIANGLE
;
299 shapeT
[i
].live
= false;
300
301
302 shapeT
[i
].bound_triangle_x
= 10;
303 shapeT
[i
].bound_triangle_y
= 16;
304 shapeT
[i
].collidingS
= false;
305 shapeT
[i
].collidingT
= false;
306
307 }
308}
309
310void DrawTriangle
(Triangle shapeT
[],
int size
)
311{
312 for(int i
=0; i
<size
; i
++)
313 {
314 if(shapeT
[i
].live
&& shapeT
[i
].collidingT
== false)
315 {
316
317
318 al_draw_triangle(shapeT
[i
].pos_x1-8,shapeT
[i
].pos_y1
+ 10,shapeT
[i
].pos_x1,shapeT
[i
].pos_y1-10,
319 shapeT
[i
].pos_x1
+8,shapeT
[i
].pos_y1
+10,
al_map_rgb(255,
0,
255),
2);
320
321
322 }
323 }
324}
325
326void StartTriangle
(Triangle shapeT
[],
int size
)
327{
328 for (int i
= 0 ; i
< size
; i
++)
329 {
330 if(!shapeT
[i
].live
)
331 {
332 if(rand() %
400 == 0)
333 {
334 if(time_remaining %
2 == 0 && time_remaining %
25 != 0)
335 {
336 shapeT
[i
].live
= true;
337 shapeT
[i
].pos_x1
= WIDTH
;
338 shapeT
[i
].pos_y1
= HEIGHT-10
;
339
340 break;
341 }
342 }
343 }
344 }
345}
346
347void UpdateTriangle
(Triangle shapeT
[],
int size, Ball
&shapeB
)
348{
349 for(int i
=0; i
<size
; i
++)
350 {
351 if((shapeT
[i
].live
) && shapeT
[i
].collidingT
== false)
352 {
353 shapeT
[i
].pos_x1
-= speed
;
354
355
356
357 if(shapeT
[i
].pos_x1-15
< 0)
358 {
359 shapeT
[i
].live
= false;
360 shapeB.score
++;
361 }
362 }
363 }
364}
365
366void CollideTriangle
(Triangle shapeT
[], Square shapeS
[],
int csize, Ball
&shapeB, Explosion
&explosion
)
367{
368 for(int i
= 0; i
< csize
; i
++)
369 {
370 if(shapeT
[i
].live
)
371 {
372
373
374 if((shapeT
[i
].pos_x1
- shapeT
[i
].bound_triangle_x
< shapeB.pos_X
+ shapeB.boundx
)
375 && (shapeT
[i
].pos_x1
+ shapeT
[i
].bound_triangle_x
> shapeB.pos_X
- shapeB.boundx
)
376 &&(shapeT
[i
].pos_y1
- shapeT
[i
].bound_triangle_y
< shapeB.pos_Y
+ shapeB.boundy
)
377 &&(shapeT
[i
].pos_y1
+ shapeT
[i
].bound_triangle_y
> shapeB.pos_Y
- shapeB.boundy
))
378 {
379
380 shapeT
[i
].live
= false;
381 shapeB.pos_Y
= shapeT
[i
].pos_y1
- shapeT
[i
].bound_triangle_y
- shapeB.boundy
- 20;
382
383 StartExplosions
(explosion,shapeB.pos_X, shapeB.pos_Y
);
384 al_play_sample(boom,
1,
0,
1,ALLEGRO_PLAYMODE_ONCE,
0);
385 shapeB.lives--
;
386
387 }
388 /*if(time_remaining % 25 == 0)
389 {
390 if(shapeT[i].pos_x1 - shapeT[i].bound_triangle_x < shapeS[i].pos_rect_x1 + 40
391 && shapeT[i].pos_y1 - shapeT[i].bound_triangle_y < shapeS[i].pos_rect_y1+40
392 && shapeS[i].pos_rect_x1 < shapeT[i].pos_x1+shapeT[i].bound_triangle_x
393 && shapeS[i].pos_rect_y1 < shapeT[i].pos_y1+shapeT[i].bound_triangle_y)
394 {
395 shapeT[i].collidingT = true;
396
397 }
398
399 }*/
400
401
402
403
404
405
406
407
408 }
409 }
410
411}
412
413
414void InitSquare
(Square shapeS
[],
int size
)
415{
416 for(int i
= 0; i
< size
; i
++)
417 {
418 shapeS
[i
].ID
= SQUARE
;
419 shapeS
[i
].live
= false;
420 shapeS
[i
].collidingS
= false;
421 }
422}
423void DrawSquare
(Square shapeS
[],
int size
)
424{
425 for(int i
= 0; i
<size
; i
++)
426 {
427 if(shapeS
[i
].live
)
428 {
429 al_draw_rounded_rectangle(shapeS
[i
].pos_rect_x1,shapeS
[i
].pos_rect_y1,
430 shapeS
[i
].pos_rect_x1
+40,shapeS
[i
].pos_rect_y1
+40,
6,
6,
al_map_rgb(255,
255,
255),
2);
431 }
432 }
433}
434void StartSquare
(Square shapeS
[],
int size
)
435{
436 for (int i
= 0 ; i
< size
; i
++)
437 {
438 if(!shapeS
[i
].live
)
439 {
440 if(rand() %
100 == 0)
441 {
442 if((time_remaining %
25) == 0)
443 shapeS
[i
].live
= true;
444 shapeS
[i
].pos_rect_x1
= WIDTH
+40;
445 shapeS
[i
].pos_rect_y1
= HEIGHT-40
;
446
447 break;
448
449 }
450 }
451 }
452}
453void UpdateSquare
(Square shapeS
[],
int size, Ball
&shapeB
)
454{
455 for(int i
=0; i
<size
; i
++)
456 {
457 if(shapeS
[i
].live
)
458 {
459 shapeS
[i
].pos_rect_x1
-= speed
;
460
461 if(shapeS
[i
].pos_rect_x1
+40 <= 0)
462 {
463 shapeS
[i
].live
= false;
464 shapeB.score
++;
465 }
466 }
467 }
468}
469
470void CollideSquare
(Square shapeS
[],
int ssize, Ball
&shapeB, Explosion
&explosion
)
471{
472 for(int i
=0; i
< ssize
; i
++)
473 {
474 if(shapeS
[i
].live
)
475 {
476 if(((shapeS
[i
].pos_rect_x1
< shapeB.pos_X
+ shapeB.boundx
+7)
477 &&(shapeS
[i
].pos_rect_x1
+ 40 > shapeB.pos_X
- shapeB.boundx
))
478 &&!((shapeS
[i
].pos_rect_y1
< shapeB.pos_Y
+ shapeB.boundy
)
479 &&(shapeS
[i
].pos_rect_y1
+40 > shapeB.pos_Y
- shapeB.boundy
)))
480 {
481 shapeB.LeftCollision
= false;
482 shapeB.pos_Y
= shapeS
[i
].pos_rect_y1
- shapeB.boundy
- 7 ;
483 shapeB.isOnSquare
= true;
484 }
485
486 else if((shapeS
[i
].pos_rect_x1
< shapeB.pos_X
+ shapeB.boundx
+ 7)
487 &&(shapeS
[i
].pos_rect_x1
> shapeB.pos_X
- shapeB.boundx
))
488 {
489 shapeB.LeftCollision
= true;
490 }
491
492 else
493 {
494 shapeB.LeftCollision
= false;
495
496 }
497 }
498 }
499}