![]() |
|
How to make my enemy disappear? (Killed) |
DJLad16
Member #14,857
January 2013
|
I have an enemy in which I need to disappear when the arrow hits it, I've got the collision working (or so it should be), but when the arrow hits the enemy, the arrow disappears as I want it. However, the enemy doesn't - even though I've set it to false. The current way I'm doing is is not really efficient, I'm just giving the enemy -x, y coordinates so it will just be off screen. (I also don't like doing that because ist like a 'cheat' way of doing it, and also not really help power if I were to do it in a bigger game). 1//Prototpyes
2void initPlayer(Player &player);
3
4void initEnemy1(Enemy &enemy1);
5void drawEnemy1(Enemy &enemy1, ALLEGRO_BITMAP *image);
6void updateEnemy1(Enemy &enemy1);
7bool collideArrow(Enemy &enemy1, Arrow &arrow);
8
9void initCoin(Coin &coin1);
10void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image);
11bool coinCollide(Coin &coin1,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, int coinCount);
12
13void initCoin2(Coin &coin2);
14void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image);
15bool coinCollide2(Coin &coin2,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, int coinCount);
16
17void initArrow(Arrow &arrow);
18void drawArrow(Arrow &arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2);
19void fireArrow(Arrow &arrow, Player &player, ALLEGRO_SAMPLE *bowShot);
20void updateArrow(Arrow &arrow);
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;
29//int arrowCount = 10;
30//const int NUM_ARROW = 10;
31const int NUM_ENEMY = 10;
32int groundHeight = 545;
33bool fired = false;
34bool coinLive = false;
35bool left, right = false;
36float cameraX, cameraY = 0;
37const float gravity = 1;
38
39int main()
40{
41
42 Player player;
43 Enemy enemy1;
44 Coin coin1;
45 Coin coin2;
46 Arrow arrow;
47 bool done = false, active = false, draw = true;
48 bool jump = false;
49 int sourceX = 32, sourceY = 0;
50 int coinCount = 0;
51 const float FPS = 70.0;
52 float gameFPS = 0;
53 float jumpSpeed = 15.0;
54 float velX = 0, velY = 0;
55
56 float cameraPosition[2] = {0,0};
57
58
59
60 al_init_image_addon();
61 al_init_primitives_addon();
62 al_install_keyboard();
63 al_init_font_addon();
64 al_init_ttf_addon();
65 al_install_audio();
66 al_init_acodec_addon();
67
68 initPlayer(player);
69 initEnemy1(enemy1);
70 initArrow(arrow);
71 initCoin(coin1);
72 initCoin2(coin2);
73
74 al_start_timer(timer);
75 while(!done)
76 {
77 ALLEGRO_EVENT event;
78 al_wait_for_event(event_queue, &event);
79 al_get_keyboard_state(&keystate);
80
81 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
82 {
83 done = true;
84 }
85 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
86 {
87 done = true;
88 }
89
90
91 else if(event.type == ALLEGRO_EVENT_TIMER)
92 {
93 active = true;
94 updateArrow(arrow);
95 updateEnemy1(enemy1);
96
97 if(al_key_down(&keystate, ALLEGRO_KEY_D))
98 {
99 velX = player.speed;
100 dir = RIGHT;
101
102 }
103 else if(al_key_down(&keystate, ALLEGRO_KEY_A))
104 {
105 velX = -player.speed;
106 dir = LEFT;
107 }
108
109 else
110 {
111 velX = 0;
112 active = false;
113 }
114 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
115 {
116 velY = -jumpSpeed;
117 jump = false;
118 }
119 if(active)
120 {
121 sourceX += al_get_bitmap_width(character) / 3;
122 }
123 else
124 {
125 sourceX = 32;
126 }
127 if(sourceX >= al_get_bitmap_width(character))
128 {
129 sourceX = 0;
130 }
131 sourceY = dir;
132 draw = true;
133 if(!jump)
134 velY += gravity;
135 else
136 velY = 0;
137 player.x += velX;
138 player.y += velY;
139
140 jump = (player.y >= groundHeight);
141
142 if(jump)
143 {
144 player.y = groundHeight;
145 }
146
147 cameraUpdate(cameraPosition, player, 32, 32);
148 al_identity_transform(&camera);
149 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
150 al_use_transform(&camera);
151
152 }
153
154 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
155 {
156 fireArrow(arrow, player, bowShot);
157 }
158
159 if(coinCollide(coin1, player, coin1.x, coin1.y, coin1.width, coin1.height, 32, 32, coinCount))
160 {
161 coin1.live = false;
162 ++coinCount;
163 coin1.x = 0;
164 coin1.y = 0;
165 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
166 }
167 if(coinCollide2(coin2, player, coin2.x, coin2.y, coin2.width, coin1.height, 32, 32, coinCount))
168 {
169 coin2.live = false;
170 ++coinCount;
171 coin2.x = 0;
172 coin2.y = 0;
173 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
174 }
175 if(collideArrow(enemy1, arrow))
176 {
177 enemy1.live = false;
178 enemy1.x = -500;
179 enemy1.y = -500;
180 enemy1.speedR = 0;
181 enemy1.speedL = 0;
182 enemy1.speed = 0;
183 arrow.live = false;
184 }
185
186 if(draw && al_is_event_queue_empty(event_queue))
187 {
188 draw = false;
189 al_draw_bitmap(background2, 0, 0, 0);
190 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, 0);
191 drawArrow(arrow, arrow_r, arrow_l);
192 drawCoin(coin1, coin);
193 drawCoin2(coin2, coin);
194 drawEnemy1(enemy1, enemy);
195 al_identity_transform(&identity);
196 al_use_transform(&identity);
197 al_draw_textf(numArrow, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrow.arrowCount);
198 al_draw_textf(numCoin, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount);
199 al_draw_textf(lives, al_map_rgb(252, 209, 22), 110, 10, 0, "Lives: %i", player.lives);
200 //al_draw_textf(fps, al_map_rgb(252, 209, 22), 120, 10, 0, "FPS: %i", gameFPS);
201 al_flip_display();
202 al_clear_to_color(al_map_rgb(255, 255, 255));
203
204 }
205 }
206
207 al_destroy_bitmap(character);
208 al_destroy_bitmap(ground);
209 al_destroy_bitmap(coin);
210 al_destroy_bitmap(background2);
211 al_destroy_bitmap(arrow_l);
212 al_destroy_bitmap(arrow_r);
213 al_destroy_sample(coinCollect);
214 al_destroy_sample(bowShot);
215 al_destroy_sample(bgSong);
216 al_destroy_sample_instance(songInstance);
217 al_destroy_font(numArrow);
218 al_destroy_font(numCoin);
219 al_destroy_font(fps);
220 al_destroy_display(display);
221 al_destroy_event_queue(event_queue);
222 al_destroy_timer(timer);
223 return 0;
224}
225void initPlayer(Player &player)
226{
227 player.x = 10;
228 player.y = 545;
229 player.ID = PLAYER;
230 player.lives = 3;
231 player.speed = 5;
232 player.score = 0;
233 dir = RIGHT;
234}
235
236void initEnemy1(Enemy &enemy1)
237{
238 enemy1.ID = ENEMY;
239 enemy1.live = false;
240 enemy1.speedR = 2;
241 enemy1.speedL = -2;
242 enemy1.speed = 2;
243 enemy1.x = 86;
244 enemy1.y = 391;
245 enemy1.width = 29;
246 enemy1.height = 29;
247}
248void drawEnemy1(Enemy &enemy1, ALLEGRO_BITMAP *image)
249{
250 enemy1.live = true;
251 if(enemy1.live)
252 {
253 al_draw_bitmap(image, enemy1.x, enemy1.y, 0);
254 }
255}
256void updateEnemy1(Enemy &enemy1)
257{
258 if(enemy1.live)
259 {
260 enemy1.x += enemy1.speed;
261 if(enemy1.x >= 238)
262 {
263 enemy1.x = 238;
264 enemy1.speed = enemy1.speedL;
265 }
266 if(enemy1.x <= 78)
267 {
268 enemy1.x = 78;
269 enemy1.speed = enemy1.speedR;
270 }
271 }
272}
273bool collideArrow(Enemy &enemy1, Arrow &arrow)
274{
275 if((arrow.x > enemy1.x + enemy1.width) || (arrow.y > enemy1.y + enemy1.height) ||
276 (enemy1.x > arrow.x + arrow.height) || (enemy1.y > arrow.y + arrow.height))
277 {
278 return false;
279 }
280 return true;
281}
282
283void initCoin(Coin &coin1)
284{
285 coin1.ID = COIN;
286 coin1.width = 16;
287 coin1.height = 16;
288 coin1.live = true;
289 coin1.x = 550;
290 coin1.y = 480;
291}
292void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image)
293{
294 if(coin1.live)
295 {
296 if(image == NULL)
297 al_draw_filled_circle(coin1.x - 10, coin1.y - 10, 20, al_map_rgb(255,0,255));
298 else
299 al_draw_bitmap(image, coin1.x, coin1.y, 0);
300 }
301}
302void initCoin2(Coin &coin2)
303{
304 coin2.ID = COIN;
305 coin2.width = 16;
306 coin2.height = 16;
307 coin2.live = true;
308 coin2.x = 120;
309 coin2.y = 395;
310}
311void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image)
312{
313 if(coin2.live)
314 {
315 if(image == NULL)
316 al_draw_filled_circle(coin2.x - 10, coin2.y - 10, 20, al_map_rgb(255,0,255));
317 else
318 al_draw_bitmap(image, coin2.x, coin2.y, 0);
319 }
320}
321bool coinCollide(Coin &coin1,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, int coinCount)
322{
323 if((player.x > cX + cWidth) || (player.y > cY + cHeight) ||
324 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin1.live))
325 {
326 return false;
327 }
328 return true;
329}
330bool coinCollide2(Coin &coin2,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, int coinCount)
331{
332 if((player.x > cX + cWidth) || (player.y > cY + cHeight) ||
333 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin2.live))
334 {
335 return false;
336 }
337 return true;
338}
339
340void initArrow(Arrow &arrow)
341{
342 arrow.ID = ARROW;
343 arrow.speed = 4.0;
344 arrow.live = false;
345 arrow.arrowCount = 10;
346 arrow.width = 16;
347 arrow.height = 5;
348 arrow.aVelX = 0;
349 arrow.aVelY = 0;
350}
351void drawArrow(Arrow &arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2)
352{
353 if(arrow.live && dir == RIGHT)
354 {
355 al_draw_bitmap(image, arrow.x, arrow.y, 0);
356 }
357 if(arrow.live && dir == LEFT)
358 {
359 al_draw_bitmap(image2, arrow.x, arrow.y, 0);
360 }
361
362}
363void fireArrow(Arrow &arrow, Player &player, ALLEGRO_SAMPLE *bowShot)
364{
365 if(!arrow.live)
366 {
367 --arrow.arrowCount;
368 arrow.live = true;
369 arrow.x = player.x + 17;
370 arrow.y = player.y + 22;
371 fired = true;
372 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
373
374 }
375
376}
377void updateArrow(Arrow &arrow)
378{
379 if(arrow.live && fired)
380 {
381 if(dir == RIGHT)
382 {
383 arrow.x += arrow.speed;
384 }
385 else if(dir == LEFT)
386 {
387 arrow.x -= arrow.speed;
388 }
389 if(arrow.x >= 2000 - 10)
390 {
391 arrow.live = false;
392 }
393 if(arrow.x <= 0 + 10)
394 {
395 arrow.live = false;
396 }
397 else if(arrow.arrowCount <= 0)
398 {
399 fired = false;
400 }
401
402 }
403}
404
405bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight)
406{
407 if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) ||
408 (ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10))
409 {
410 return false;
411 }
412 return true;
413}
414
415void cameraUpdate(float *cameraPosition, Player &player, float width, float height)
416{
417 cameraPosition[0] = -(ScreenWidth / 2) + (player.x + width / 2);
418 cameraPosition[1] = -(ScreenHeight) + (player.y + height / 2);
419
420 if(cameraPosition[0] < 0)
421 cameraPosition[0] = 0;
422 if(cameraPosition[1] < 0)
423 cameraPosition[1] = 0;
424 if(cameraPosition[0] > 2000 - ScreenWidth)
425 cameraPosition[0] = 2000 - ScreenWidth;
426
427}
|
beoran
Member #12,636
March 2011
|
void drawEnemy1(Enemy &enemy1, ALLEGRO_BITMAP *image) { enemy1.live = true;
You're setting it live on every redraw... |
Schyfis
Member #9,752
May 2008
![]() |
In drawEnemy1, you set enemy1.live = true. Then when updateEnemy1 is called, it sets the enemy's position again. Edit: I missed a post. :/ ________________________________________________________________________________________________________ |
Jeff Bernard
Member #6,698
December 2005
![]() |
Saw this thread in Recent Threads. Was disappointed that it was a programming question. -- |
|