1//Prototpyes
2void initPlayer
(Player
&player
);
3
4
5void initEnemy
(Enemy
&enemy,
int x,
int y
);
6void drawEnemy
(Enemy
&enemy,
ALLEGRO_BITMAP *image
);
7void updateEnemy
(Enemy
&enemy,
int posX,
int posX2
);
8bool collideArrow
(Enemy
&enemy, Arrow
&arrow,
bool eLive,
bool aLive
);
9
10void initCoin
(Coin
&coin,
int x,
int y
);
11void drawCoin
(Coin
&coin,
ALLEGRO_BITMAP *image
);
12bool coinCollide
(Coin
&coin,Player
&player ,
int cX,
int cY,
int cWidth,
int cHeight,
int pWidth,
int pHeight,
bool live,
int coinCount
);
13
14void initArrow
(Arrow
&arrow
);
15void drawArrow
(Arrow
&arrow,
ALLEGRO_BITMAP *image,
ALLEGRO_BITMAP *image2
);
16void fireArrow
(Arrow
&arrow, Player
&player,
ALLEGRO_SAMPLE *bowShot
);
17void updateArrow
(Arrow
&arrow
);
18
19void initKey
(Key
&key);
20void drawKey
(Key
&key,
ALLEGRO_BITMAP *image
);
21
22bool collision
(Player
&player,
int ex,
int ey,
int eWidth,
int eHeight,
int pWidth,
int pHeight
);
23
24void cameraUpdate
(float *cameraPosition,Player
&player,
float width,
float height
);
25
26//Global Variables
27enum direction
{LEFT, RIGHT
};
28int dir
= LEFT
;
29const int NUM_ENEMY
= 10;
30int groundHeight
= 545;
31bool fired
= false, firedR
= false, firedL
= false;
32bool coinLive
= false;
33bool left, right
= false;
34const float gravity
= 1;
35
36int main
()
37{
38
39 Player player
;
40 Enemy enemy
;
41 Enemy enemy2
;
42 Coin coin
;
43 Coin coin2
;
44 Arrow arrow
;
45 Key
key;
46 bool done
= false, active
= false, draw
= true;
47 bool jump
= false;
48 bool gotKey
= false;
49 int sourceX
= 32, sourceY
= 0;
50 int coinCount
= 0;
51 const float FPS
= 60.
0;
52 const float frameFPS
= 15.
0;
53 float jumpSpeed
= 15.
0;
54 float velX
= 0, velY
= 0;
55
56 float cameraPosition
[2] = {0,
0};
57
58 initPlayer
(player
);
59 initEnemy
(enemy,
86,
391);
60 initEnemy
(enemy2,
1162,
338);
61 initArrow
(arrow
);
62 initCoin
(coin,
550,
480);
63 initCoin
(coin2,
120,
395);
64 initKey
(key);
65
66 ALLEGRO_KEYBOARD_STATE keystate
;
67 ALLEGRO_TRANSFORM camera
;
68 ALLEGRO_TRANSFORM identity
;
69 ALLEGRO_SAMPLE *coinCollect
= al_load_sample("Files/coin collect sound.wav");
70 ALLEGRO_SAMPLE *bowShot
= al_load_sample("Files/bow sound effect.wav");
71 ALLEGRO_SAMPLE *bgSong
= al_load_sample("Files/Background song.wav");
72 ALLEGRO_SAMPLE *keyCollection
= al_load_sample("Files/key collection.wav");
73 ALLEGRO_SAMPLE_INSTANCE *songInstance
= al_create_sample_instance(bgSong
);
74 ALLEGRO_BITMAP *enemyImg
= al_load_bitmap("Files/enemy.png");
75 ALLEGRO_BITMAP *background2
= al_load_bitmap("Files/Background.png");
76 ALLEGRO_BITMAP *character
= al_load_bitmap("Files/spritesheet(Bow & left + right).png");
77 ALLEGRO_BITMAP *arrow_r
= al_load_bitmap("Files/Arrow(RIGHT).png");
78 ALLEGRO_BITMAP *arrow_l
= al_load_bitmap("Files/Arrow(LEFT).png");
79 ALLEGRO_BITMAP *coinImg
= al_load_bitmap("Files/coin2.png");
80 ALLEGRO_BITMAP *keyImg
= al_load_bitmap("Files/key.png");
81 ALLEGRO_BITMAP *ground
= al_load_bitmap("Files/Ground.png");
82 ALLEGRO_FONT *numArrow
= al_load_font("Files/JUNGBN__.TTF",
23,
0);
83 ALLEGRO_FONT *fps
= al_load_font("Files/JUNGBN__.TTF",
23,
0);
84 ALLEGRO_FONT *numCoin
= al_load_font("Files/JUNGBN__.TTF",
23,
0);
85 ALLEGRO_FONT *random = al_load_font("Files/JUNGBN__.TTF",
23,
0);
86 ALLEGRO_FONT *lives
= al_load_font("Files/JUNGBN__.TTF",
23,
0);
87 ALLEGRO_TIMER *timer
= al_create_timer(1.
0/FPS
);
88 ALLEGRO_TIMER *frameTimer
= al_create_timer(1.
0/frameFPS
);
89 ALLEGRO_EVENT_QUEUE *event_queue
= al_create_event_queue();
90
91 al_reserve_samples(4); //Respresents how many audio clips I have in the game
92
93 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP
);
94 al_attach_sample_instance_to_mixer(songInstance,
al_get_default_mixer());
95 al_set_window_title(display,
"Australian Outback");
96 al_register_event_source(event_queue,
al_get_keyboard_event_source());
97 al_register_event_source(event_queue,
al_get_timer_event_source(timer
));
98 al_register_event_source(event_queue,
al_get_timer_event_source(frameTimer
));
99 al_register_event_source(event_queue,
al_get_display_event_source(display
));
100
101 al_play_sample_instance(songInstance
);
102
103 al_start_timer(timer
);
104 al_start_timer(frameTimer
);
105 while(!done
)
106 {
107 ALLEGRO_EVENT event
;
108 al_wait_for_event(event_queue,
&event
);
109 al_get_keyboard_state(&keystate
);
110
111 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE
))
112 {
113 done
= true;
114 }
115 else if(event.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
116 {
117 done
= true;
118 }
119
120
121 else if(event.type
== ALLEGRO_EVENT_TIMER
)
122 {
123 if(event.timer.source
== timer
)
124 {
125 active
= true;
126 updateArrow
(arrow
);
127 updateEnemy
(enemy,
238,
78);
128 updateEnemy
(enemy2,
1162,
1229);
129
130 if(al_key_down(&keystate, ALLEGRO_KEY_D
))
131 {
132 velX
= player.speed
;
133 dir
= RIGHT
;
134
135 }
136 else if(al_key_down(&keystate, ALLEGRO_KEY_A
))
137 {
138 velX
= -player.speed
;
139 dir
= LEFT
;
140 }
141 else
142 {
143 velX
= 0;
144 active
= false;
145 }
146 if(al_key_down(&keystate, ALLEGRO_KEY_W
) && jump
)
147 {
148 velY
= -jumpSpeed
;
149 jump
= false;
150 }
151 }
152 else if(event.timer.source
= frameTimer
)
153 {
154 if(active
)
155 {
156 sourceX
+= al_get_bitmap_width(character
) / 3;
157 }
158 else
159 {
160 sourceX
= 32;
161 }
162 if(sourceX
>= al_get_bitmap_width(character
))
163 {
164 sourceX
= 0;
165 }
166 sourceY
= dir
;
167
168
169 }
170
171 if(!jump
)
172 velY
+= gravity
;
173 else
174 velY
= 0;
175 player.x
+= velX
;
176 player.y
+= velY
;
177
178 jump
= (player.y
>= groundHeight
);
179
180 if(jump
)
181 {
182 player.y
= groundHeight
;
183 } draw
= true;
184
185
186 cameraUpdate
(cameraPosition, player,
32,
32);
187 al_identity_transform(&camera
);
188 al_translate_transform(&camera,
-cameraPosition
[0],
-cameraPosition
[1]);
189 al_use_transform(&camera
);
190
191 }
192
193 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE
))
194 {
195 fireArrow
(arrow, player, bowShot
);
196 }
197
198 if(coinCollide
(coin, player, coin.x, coin.y, coin.width, coin.height,
32,
32, coinLive, coinCount
))
199 {
200 coin.live
= false;
201 ++coinCount
;
202 coin.x
= -500;
203 coin.y
= -500;
204 al_play_sample(coinCollect,
1.
0,
0.
0,
1.
0, ALLEGRO_PLAYMODE_ONCE,
0);
205 }
206 if(coinCollide
(coin2, player, coin2.x, coin2.y, coin2.width, coin2.height,
32,
32, coinLive, coinCount
))
207 {
208 coin.live
= false;
209 ++coinCount
;
210 coin2.x
= -500;
211 coin2.y
= -500;
212 al_play_sample(coinCollect,
1.
0,
0.
0,
1.
0, ALLEGRO_PLAYMODE_ONCE,
0);
213 }
214 if(collision
(player, key.x, key.y, key.width, key.height,
32,
32))//Collide with key
215 {
216 al_play_sample(keyCollection,
1.
0,
0.
0,
1.
0, ALLEGRO_PLAYMODE_ONCE,
0);
217 key.x
= -500;
218 key.y
= -500;
219 key.live
= false;
220 }
221 if(collision
(player, enemy.x, enemy.y, enemy.width, enemy.height,
32,
32))//Player collision with enemy
222 {
223 player.x
= 10;
224 player.y
= 546;
225 --player.lives
;
226 arrow.live
= false;
227 dir
= RIGHT
;
228 }
229
230 if(collideArrow
(enemy, arrow, enemy.live, arrow.live
))
231 {
232 enemy.live
= false;
233 enemy.x
= 0;
234 enemy.y
= 0;
235 arrow.live
= false;
236 gotKey
= true;
237 ++player.score
;
238 }
239
240 if(draw
&& al_is_event_queue_empty(event_queue
))
241 {
242 draw
= false;
243 al_draw_bitmap(background2,
0,
0,
0);
244 al_draw_bitmap_region(character, sourceX, sourceY
* al_get_bitmap_height(character
) / 2,
32,
32, player.x, player.y,
0);
245 drawKey
(key, keyImg
);
246 drawArrow
(arrow, arrow_r, arrow_l
);
247 drawCoin
(coin, coinImg
);
248 drawCoin
(coin2, coinImg
);
249 drawEnemy
(enemy, enemyImg
);
250 drawEnemy
(enemy2, enemyImg
);
251 al_identity_transform(&identity
);
252 al_use_transform(&identity
);
253 al_draw_textf(numArrow,
al_map_rgb(252,
209,
22),
10,
10,
0,
"Arrows: %i", arrow.arrowCount
);
254 al_draw_textf(numCoin,
al_map_rgb(252,
209,
22),
10,
50,
0,
"Coins: %i", coinCount
);
255 al_draw_textf(lives,
al_map_rgb(252,
209,
22),
110,
10,
0,
"Lives: %i", player.lives
);
256 //al_draw_textf(fps, al_map_rgb(252, 209, 22), 110, 50, 0, "FPS: %i", FPS);
257 al_flip_display();
258 al_clear_to_color(al_map_rgb(255,
255,
255));
259
260 }
261 }
262
263
264}
265void initPlayer
(Player
&player
)
266{
267 player.x
= 1162;
268 player.y
= 545;
269 player.lives
= 3;
270 player.speed
= 5;
271 player.score
= 0;
272 dir
= RIGHT
;
273}
274
275void initEnemy
(Enemy
&enemy,
int x,
int y
)
276{
277 enemy.live
= true;
278 enemy.speedR
= 2;
279 enemy.speedL
= -2;
280 enemy.speed
= 2;
281 enemy.x
= x
;
282 enemy.y
= y
;
283 enemy.width
= 29;
284 enemy.height
= 29;
285}
286void drawEnemy
(Enemy
&enemy,
ALLEGRO_BITMAP *image
)
287{
288 if(enemy.live
)
289 {
290 al_draw_bitmap(image, enemy.x, enemy.y,
0);
291 }
292}
293void updateEnemy
(Enemy
&enemy,
int posX,
int posX2
)
294{
295 if(enemy.live
)
296 {
297 enemy.x
+= enemy.speed
;
298 if(enemy.x
>= posX
)
299 {
300 enemy.x
= posX
;
301 enemy.speed
= enemy.speedL
;
302 }
303 if(enemy.x
<= posX2
)
304 {
305 enemy.x
= posX2
;
306 enemy.speed
= enemy.speedR
;
307 }
308 }
309}
310
311void initArrow
(Arrow
&arrow
)
312{
313 arrow.speed
= 4.
0;
314 arrow.live
= false;
315 arrow.arrowCount
= 10;
316 arrow.width
= 16;
317 arrow.height
= 5;
318 arrow.aVelX
= 0;
319 arrow.aVelY
= 0;
320}
321void drawArrow
(Arrow
&arrow,
ALLEGRO_BITMAP *image,
ALLEGRO_BITMAP *image2
)
322{
323 if(arrow.live
&& dir
== RIGHT
)
324 {
325 al_draw_bitmap(image, arrow.x, arrow.y,
0);
326 }
327 if(arrow.live
&& dir
== LEFT
)
328 {
329 al_draw_bitmap(image2, arrow.x, arrow.y,
0);
330 }
331
332}
333void fireArrow
(Arrow
&arrow, Player
&player,
ALLEGRO_SAMPLE *bowShot
)
334{
335 if(!arrow.live
)
336 {
337 --arrow.arrowCount
;
338 arrow.live
= true;
339 arrow.x
= player.x
+ 17;
340 arrow.y
= player.y
+ 22;
341 fired
= true;
342 al_play_sample(bowShot,
1.
0,
0.
0,
1.
0, ALLEGRO_PLAYMODE_ONCE,
0);
343
344 }
345
346}
347void updateArrow
(Arrow
&arrow
)
348{
349 if(arrow.live
&& fired
)
350 {
351 if(dir
== RIGHT
)
352 {
353 arrow.x
+= arrow.speed
;
354 }
355 if(dir
== LEFT
)
356 {
357 arrow.x
-= arrow.speed
;
358 }
359 if(arrow.x
>= 2000 - 10)
360 {
361 arrow.live
= false;
362 }
363 if(arrow.x
<= 0 + 3)
364 {
365 arrow.live
= false;
366 }
367 else if(arrow.arrowCount
<= 0)
368 {
369 fired
= false;
370 }
371
372 }
373}