1#include <allegro5\allegro.h>
2#include <allegro5\allegro_primitives.h>
3#include <allegro5\allegro_image.h>
4#include <allegro5\allegro_ttf.h>
5#include <allegro5\allegro_font.h>
6#include "objects.h"
7
8// Buttons
9enum BUTON
{A,S,D,W,SPACE
};
10bool buton
[5] = {false,
false,
false,
false,
false};
11
12// Globals
13const int FPS
= 80, width
= 800, height
= 600;
14const int NUM_BULLETS
= 5;
15
16// Protorypes
17void InitShip
(SpaceShip
&ship
);
18void DrawShip
(SpaceShip
&ship
);
19void MoveShipUp
(SpaceShip
&ship
);
20void MoveShipDown
(SpaceShip
&ship
);
21void MoveShipLeft
(SpaceShip
&ship
);
22void MoveShipRight
(SpaceShip
&ship
);
23
24void InitBullet
(Bullet bullet
[],
int size
);
25void DrawBullet
(Bullet bullet
[],
int size
);
26void FireBullet
(Bullet bullet
[],
int size, SpaceShip
&ship
);
27void UpdateBullet
(Bullet bullet
[],
int size
);
28
29int main
()
30{
31 // Initialization
32 al_init();
33 al_init_primitives_addon();
34 al_init_image_addon();
35 al_init_font_addon();
36 al_init_ttf_addon();
37
38 // Installations
39 al_install_keyboard();
40 al_install_mouse();
41
42 // Variables
43 SpaceShip ship
;
44 Bullet bullets
[5];
45 // I don't think you are actually using this variable
46 int fireCooldown
= 0;
47
48 int x
= width
/ 2;
49 int y
= height
/ 2;
50
51 bool quit
= false;
52 bool draw
= true;
53
54 // ALLEGRO variables
55 ALLEGRO_DISPLAY *window = al_create_display(width, height
);
56 ALLEGRO_BITMAP *icon
= al_load_bitmap("image/1.png");
57 ALLEGRO_FONT *font16
= al_load_ttf_font("fonts/1.ttf",
80, ALLEGRO_ALIGN_CENTRE
);
58 ALLEGRO_TIMER *timer
= NULL
;
59 ALLEGRO_EVENT_QUEUE *event
= NULL
;
60
61 event
= al_create_event_queue();
62 timer
= al_create_timer(1.
0 / FPS
);
63
64 // Events
65 al_register_event_source(event,
al_get_keyboard_event_source());
66 al_register_event_source(event,
al_get_mouse_event_source());
67 al_register_event_source(event,
al_get_display_event_source(window));
68 al_register_event_source(event,
al_get_timer_event_source(timer
));
69
70 // Window title, icon and mouse hide
71 al_set_window_title(window,
"Game programming");
72 al_set_display_icon(window, icon
);
73 al_hide_mouse_cursor(window);
74
75 // Player initializations
76 InitShip
(ship
);
77 InitBullet
(bullets, NUM_BULLETS
);
78
79 // Start timer
80 al_start_timer(timer
);
81
82
83 // The drawing
84 while(!quit
)
85 {
86 // Call of the event
87 ALLEGRO_EVENT e
;
88 al_wait_for_event(event,
&e
);
89
90 // Drawing
91 if(e.type
== ALLEGRO_EVENT_TIMER
)
92 {
93 draw
= true;
94
95 if(buton
[A
])
96 MoveShipLeft
(ship
);
97 if(buton
[D
])
98 MoveShipRight
(ship
);
99 if(buton
[S
])
100 MoveShipDown
(ship
);
101 if(buton
[W
])
102 MoveShipUp
(ship
);
103 // Why didn't you apply the same logic to firing your bullets?
104 if(buton
[SPACE
])
105 {
106 // also it's button, you got it right in the comments though.
107 // Or is this a different language?
108 if(fireCooldown
== 0)
109 {
110 // We'll decrement this variable later
111 // This block will only get executed if fireCooldown equals 0
112 fireCooldown
= 10;
113 // Now that we've assigned 10 to fireCooldown we can be sure the next bullet is not going to be fired for a couple of frames
114 FireBullet
(bullets, NUM_BULLETS, ship
);
115 }
116 }
117
118 // Let's get the ship ready for the next shot
119 if(fireCooldown
> 0)
120 fireCooldown--
;
121
122
123 UpdateBullet
(bullets, NUM_BULLETS
);
124 }
125
126 // Key down or up
127 switch(e.type
)
128 {
129 case ALLEGRO_EVENT_KEY_DOWN:
130 switch(e.keyboard.keycode
)
131 {
132 case ALLEGRO_KEY_A:
133 buton
[A
] = true;
134 break;
135
136 case ALLEGRO_KEY_S:
137 buton
[S
] = true;
138 break;
139
140 case ALLEGRO_KEY_D:
141 buton
[D
] = true;
142 break;
143
144 case ALLEGRO_KEY_W:
145 buton
[W
] = true;
146 break;
147
148 case ALLEGRO_KEY_SPACE:
149 buton
[SPACE
] = true;
150 // Now you are going to fire a bullet each time you press your spacebar, so let's move this out of here
151 //FireBullet(bullets, NUM_BULLETS, ship);
152
153 break;
154
155 case ALLEGRO_KEY_ESCAPE:
156 quit
= true;
157 break;
158 }
159 }
160
161 switch(e.type
)
162 {
163 case ALLEGRO_EVENT_KEY_UP:
164 switch(e.keyboard.keycode
)
165 {
166 case ALLEGRO_KEY_A:
167 buton
[A
] = false;
168 break;
169
170 case ALLEGRO_KEY_S:
171 buton
[S
] = false;
172 break;
173
174 case ALLEGRO_KEY_D:
175 buton
[D
] = false;
176 break;
177
178 case ALLEGRO_KEY_W:
179 buton
[W
] = false;
180 break;
181
182 case ALLEGRO_KEY_SPACE:
183 buton
[SPACE
] = false;
184 break;
185 }
186 }
187
188
189 // Close display when X is pressed
190 if(e.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
191 {
192 quit
= true;
193 }
194
195 // Mouse movement
196 else if(e.type
== ALLEGRO_EVENT_MOUSE_AXES
)
197 {
198 ship.y
= e.mouse.y
;
199 }
200
201 // Bad idea, an ALLEGRO_EVENT is an union
202 // Meaning that multiple different members can take the same place in memory
203 // Other events might 'overwrite' e.mouse.button, causing it to falsely evaluate to true and this is more likely than you might think as it caused the firing to seem irregular with this not being commented
204 /*else if(e.mouse.button & 1)
205 {
206 FireBullet(bullets, NUM_BULLETS, ship);
207 }*/
208 if(e.type
== ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
)
209 {
210 if(e.mouse.button
& 1)
211 {
212 buton
[SPACE
] = true; // Ugly hack but it works
213 }
214 }
215 else if(e.type
== ALLEGRO_EVENT_MOUSE_BUTTON_UP
)
216 {
217 if(e.mouse.button
& 1)
218 {
219 buton
[SPACE
] = false;
220 }
221 }
222
223 if(draw
&& al_event_queue_is_empty
(event
))
224 {
225 draw
= false;
226 DrawShip
(ship
);
227 DrawBullet
(bullets, NUM_BULLETS
);
228
229 al_flip_display();
230 al_clear_to_color(al_map_rgb(0,
0,
0));
231 }
232
233 }
234
235 // Destroy
236 al_destroy_event_queue(event
);
237 al_destroy_bitmap(icon
);
238 al_destroy_display(window);
239}
240
241void InitShip
(SpaceShip
&ship
)
242{
243 ship.x
= 20;
244 ship.y
= height
/ 2;
245 ship.ID
= PLAYER
;
246 ship.lives
= 3;
247 ship.speed
= 7;
248 ship.boundx
= 6;
249 ship.boundy
= 7;
250 ship.score
= 0;
251}
252
253void DrawShip
(SpaceShip
&ship
)
254{
255 al_draw_filled_rectangle(ship.x
- 10, ship.y-15, ship.x
+ 35, ship.y,
al_map_rgb(255,
128,
0));
256 al_draw_filled_rectangle(ship.x
+ 35, ship.y-15, ship.x
+ 130, ship.y,
al_map_rgb(255,
255,
255));
257}
258
259void MoveShipUp
(SpaceShip
&ship
)
260{
261 ship.y
-= ship.speed
;
262 if(ship.y
< 15)
263 ship.y
= 15;
264}
265
266void MoveShipDown
(SpaceShip
&ship
)
267{
268 ship.y
+= ship.speed
;
269 if(ship.y
> height
)
270 ship.y
= height
;
271}
272
273void MoveShipLeft
(SpaceShip
&ship
)
274{
275 ship.x
-= ship.speed
;
276 if(ship.x
< 10)
277 ship.x
= 10;
278}
279
280void MoveShipRight
(SpaceShip
&ship
)
281{
282 ship.x
+= ship.speed
;
283 if(ship.x
> 100)
284 ship.x
= 100;
285}
286
287void InitBullet
(Bullet bullet
[],
int size
)
288{
289 for(int i
= 0; i
< size
; i
++)
290 {
291 bullet
[i
].ID
= BULLET
;
292 bullet
[i
].speed
= 10;
293 bullet
[i
].live
= false;
294 }
295}
296
297void DrawBullet
(Bullet bullet
[],
int size
)
298{
299 for(int i
= 0; i
< size
; i
++)
300 {
301 if(bullet
[i
].live
)
302 al_draw_filled_circle(bullet
[i
].x, bullet
[i
].y,
5,
al_map_rgb(128,
128,
128));
303 }
304}
305
306void FireBullet
(Bullet bullet
[],
int size, SpaceShip
&ship
)
307{
308 for(int i
= 0; i
< size
; i
++)
309 {
310 if(!bullet
[i
].live
)
311 {
312 bullet
[i
].x
= ship.x
+ 120;
313 bullet
[i
].y
= ship.y
- 7;
314 bullet
[i
].live
= true;
315 break;
316 }
317 }
318}
319
320void UpdateBullet
(Bullet bullet
[],
int size
)
321{
322 for(int i
= 0; i
< size
; i
++)
323 {
324 if(bullet
[i
].live
)
325 {
326 bullet
[i
].x
+= bullet
[i
].speed
;
327 if(bullet
[i
].x
> width
)
328 {
329 bullet
[i
].live
= false;
330 break;
331 }
332 }
333 }
334}