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