![]() |
|
This f***ing enemy won't move! |
DJLad16
Member #14,857
January 2013
|
I've been at this for quite some time now, and I still can't get it to work. My problem is that I created a second enemy and I wanted him moving on another platform. So for the sake of typing less code, I modified initEnemy(), updateEnemy(); so I only need to type it out once. And not do initEnemy2, initEnemy3 etc. When I finally went to putting the code in, the enemy wouldn't move. I tried tinkering around with it for a while with no results. So I resorted to doing initEnemy2, drawEnemy2 etc. I was thinking this should work 100% - it didn't. Went back to the original way, and I found out that changing enemy2 co-ordinates to the same as the enemy IT WOULD MOVE! But when I put it back to the co-ordinates I want it to be, IT WOULDN'T MOVE! And after that I had absolutely no idea what was going on. (I've removed some code for file size error, and any irrelevant code) 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}
|
Dizzy Egg
Member #10,824
March 2009
![]() |
I only read the first couple of lines, but since no-one else has answered can I suggest you Google "class arrays" or "arrays". Basically you'll have an array of instances of your class, that you'll cycle through using a 'for' loop or similar, so you don't have to write enemy_1, enemy_2, enemy_3 etc etc etc. It'll look something like: for(int i=0;i=TOTAL_ENEMIES;i++){ enemy[i]->doSomething(); } and that will go through all your enemies! If you want more help just ask, the Allegroids are AWESOME when they get their teeth stuck in!
---------------------------------------------------- |
Specter Phoenix
Member #1,425
July 2001
![]() |
Do you really need that many font declarations that point to the exact same font data? If you are going to use the same font why not just make one, load it, then use it across all the things that need that font rather than use memory to hold the same data. As for movement, I agree with Dizzy on this one.
|
Schyfis
Member #9,752
May 2008
![]() |
Let's see what happens when you call updateEnemy(enemy2, 1162, 1229). 1//the first call to updateEnemy for enemy2 will go like this:
2void updateEnemy(Enemy &enemy, int posX, int posX2)
3{
4 if(enemy.live) //enemy2.live is true, so enter block
5 {
6 enemy.x += enemy.speed; //1162 + 2. enemy2.x is now 1164
7 if(enemy.x >= posX) //1164 >= posX, so enter block
8 {
9 enemy.x = posX; //enemy.x is now 1162
10 enemy.speed = enemy.speedL; //enemy.speed is now -2
11 }
12 if(enemy.x <= posX2) //1162 <= posX2, so enter block
13 {
14 enemy.x = posX2; //enemy.x is now 1229
15 enemy.speed = enemy.speedR; //enemy.speed is now 2
16 }
17 }
18}
19
20//the second and all future calls to updateEnemy for enemy2 will go like this:
21void updateEnemy(Enemy &enemy, int posX, int posX2)
22{
23 if(enemy.live) //enemy2.live is true, so enter block
24 {
25 enemy.x += enemy.speed; //1229 - 2. enemy2.x is now 1227
26 if(enemy.x >= posX) //1227 >= posX, so enter block
27 {
28 enemy.x = posX; //enemy.x is now 1162
29 enemy.speed = enemy.speedL; //enemy.speed is now -2
30 }
31 if(enemy.x <= posX2) //1162 <= posX2, so enter block
32 {
33 enemy.x = posX2; //enemy.x is now 1229
34 enemy.speed = enemy.speedR; //enemy.speed is now 2
35 }
36 }
37}
Long story short, your enemy will always be stuck at 1229 because of the input to the function. How about giving the Enemy struct a minimum_x and a maximum_x? I think it would make your life easier. You will definitely benefit from Dizzy's suggestion. Simplification like that will lead to a lot less code, which means a lot less confusion. If you keep your enemies (and other game objects, for that matter) in arrays, you can keep track of them and update them in one stroke. You can even do collision functions for all of them in one loop, instead of writing something like: if(collision(player, enemy1)) if(collision(player, enemy2)) if(collision(player, enemy3)) //... if(collision(player, enemy498)) if(collision(player, enemy499)) if(collision(player, enemy500))
________________________________________________________________________________________________________ |
DJLad16
Member #14,857
January 2013
|
Here's what I did with class arrays. However, I couldn't get the first enemy to draw, so I instantiated and new enemy object Enemy enemy2[NUM_ENEMY];. But instantiating a new enemy kind of contradicts the idea of class arrays because I still need to do collision for each enemy. (It wouldn't let me post a reply for some reason, so I just had to edit this post) 1nclude <allegro5/allegro.h>
2#include <allegro5/allegro_native_dialog.h>
3#include <allegro5/allegro_font.h>
4#include <allegro5/allegro_ttf.h>
5#include <allegro5/allegro_primitives.h>
6#include <allegro5/allegro_image.h>
7#include <allegro5/allegro_audio.h>
8#include <allegro5/allegro_acodec.h>
9#include <time.h>
10#include <cmath>
11#include "objects.h"
12
13#define ScreenWidth 800
14#define ScreenHeight 600
15
16
17//////////////////////////////////////
18// SORT OUT ARROW PROBLEM //
19//////////////////////////////////////
20
21
22//Prototpyes
23void initPlayer(Player &player);
24
25
26void initEnemy(Enemy enemy[], int x, int y, int numEnemy);
27void drawEnemy(Enemy enemy[], ALLEGRO_BITMAP *image, int numEnemy);
28void updateEnemy(Enemy enemy[], int posX, int posX2, int numEnemy);
29bool collideArrow(Enemy enemy[], Arrow &arrow, bool eLive, bool aLive, int numEnemy);
30
31void initCoin(Coin &coin, int x, int y);
32void drawCoin(Coin &coin, ALLEGRO_BITMAP *image);
33bool coinCollide(Coin &coin,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
34
35void initArrow(Arrow &arrow);
36void drawArrow(Arrow &arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2);
37void fireArrow(Arrow &arrow, Player &player, ALLEGRO_SAMPLE *bowShot);
38void updateArrow(Arrow &arrow);
39
40void initKey(Key &key);
41void drawKey(Key &key, ALLEGRO_BITMAP *image);
42
43bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight);
44
45void cameraUpdate(float *cameraPosition,Player &player, float width, float height);
46
47//Global Variables
48enum direction {LEFT, RIGHT};
49int dir = LEFT;
50const int NUM_ENEMY = 2;
51bool fired = false, firedR = false, firedL = false;
52bool coinLive = false;
53bool left, right = false;
54const float gravity = 1;
55
56int main()
57{
58 ALLEGRO_DISPLAY *display;
59
60 if(!al_init())
61 {
62 al_show_native_message_box(0, 0, "Error", "Falied to initialize allegro", 0, 0);
63 return -1;
64 }
65
66 display = al_create_display(ScreenWidth, ScreenHeight);
67
68 if(!display)
69 {
70 al_show_native_message_box(0, 0, "Error", "Falied to initialize the display", 0, 0);
71 return -1;
72 }
73
74 Player player;
75 Enemy enemy[NUM_ENEMY];
76 Enemy enemy2[NUM_ENEMY];
77 Coin coin;
78 Coin coin2;
79 Arrow arrow;
80 Key key;
81 int groundHeight = 535;
82 bool done = false, active = false, draw = true;
83 bool jump = false;
84 bool gotKey = false;
85 int sourceX = 32, sourceY = 0;
86 int coinCount = 0;
87 const float FPS = 60.0;
88 const float frameFPS = 15.0;
89 float jumpSpeed = 15.0;
90 float velX = 0, velY = 0;
91
92 float cameraPosition[2] = {0,0};
93
94
95
96 al_init_image_addon();
97 al_init_primitives_addon();
98 al_install_keyboard();
99 al_init_font_addon();
100 al_init_ttf_addon();
101 al_install_audio();
102 al_init_acodec_addon();
103
104 initPlayer(player);
105 initEnemy(enemy, 86, 391, NUM_ENEMY);
106 initEnemy(enemy2, 1167, 333, NUM_ENEMY);
107 initArrow(arrow);
108 initCoin(coin, 550, 480);
109 initCoin(coin2, 120, 398);
110 initKey(key);
111
112 ALLEGRO_KEYBOARD_STATE keystate;
113 ALLEGRO_TRANSFORM camera;
114 ALLEGRO_TRANSFORM identity;
115 ALLEGRO_SAMPLE *coinCollect = al_load_sample("Files/coin collect sound.wav");
116 ALLEGRO_SAMPLE *bowShot = al_load_sample("Files/bow sound effect.wav");
117 ALLEGRO_SAMPLE *bgSong = al_load_sample("Files/Background song 2.wav");
118 ALLEGRO_SAMPLE *keyCollection = al_load_sample("Files/key collection.wav");
119 ALLEGRO_SAMPLE *crocSound = al_load_sample("Files/Crocodile sound effect.wav");
120 ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(bgSong);
121 ALLEGRO_BITMAP *enemyImg = al_load_bitmap("Files/enemy.png");
122 ALLEGRO_BITMAP *background2 = al_load_bitmap("Files/Background 2.png");
123 ALLEGRO_BITMAP *character = al_load_bitmap("Files/spritesheet(Bow & left + right).png");
124 ALLEGRO_BITMAP *arrow_r = al_load_bitmap("Files/Arrow(RIGHT).png");
125 ALLEGRO_BITMAP *arrow_l = al_load_bitmap("Files/Arrow(LEFT).png");
126 ALLEGRO_BITMAP *coinImg = al_load_bitmap("Files/coin2.png");
127 ALLEGRO_BITMAP *keyImg = al_load_bitmap("Files/key.png");
128 ALLEGRO_BITMAP *ground = al_load_bitmap("Files/Ground.png");
129 ALLEGRO_FONT *font = al_load_font("Files/JUNGBN__.TTF", 23, 0);
130 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
131 ALLEGRO_TIMER *frameTimer = al_create_timer(1.0/frameFPS);
132 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
133
134 al_reserve_samples(5); //Respresents how many audio clips I have in the game
135
136 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
137 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
138 al_set_window_title(display, "Australian Outback");
139 al_register_event_source(event_queue, al_get_keyboard_event_source());
140 al_register_event_source(event_queue, al_get_timer_event_source(timer));
141 al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
142 al_register_event_source(event_queue, al_get_display_event_source(display));
143
144 //al_play_sample_instance(songInstance);
145
146 al_start_timer(timer);
147 al_start_timer(frameTimer);
148 while(!done)
149 {
150 ALLEGRO_EVENT event;
151 al_wait_for_event(event_queue, &event);
152 al_get_keyboard_state(&keystate);
153
154 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
155 {
156 done = true;
157 }
158 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
159 {
160 done = true;
161 }
162
163
164 else if(event.type == ALLEGRO_EVENT_TIMER)
165 {
166 if(event.timer.source == timer)
167 {
168 active = true;
169 updateArrow(arrow);
170 updateEnemy(enemy, 238, 78, NUM_ENEMY);
171 updateEnemy(enemy2, 1236, 1167, NUM_ENEMY);
172
173 if(al_key_down(&keystate, ALLEGRO_KEY_D))
174 {
175 velX = player.speed;
176 dir = RIGHT;
177
178 }
179 else if(al_key_down(&keystate, ALLEGRO_KEY_A))
180 {
181 velX = -player.speed;
182 dir = LEFT;
183 }
184 else
185 {
186 velX = 0;
187 active = false;
188 }
189 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
190 {
191 velY = -jumpSpeed;
192 jump = false;
193 }
194 }
195 else if(event.timer.source = frameTimer)
196 {
197 if(active)
198 {
199 sourceX += al_get_bitmap_width(character) / 3;
200 }
201 else
202 {
203 sourceX = 32;
204 }
205 if(sourceX >= al_get_bitmap_width(character))
206 {
207 sourceX = 0;
208 }
209 sourceY = dir;
210
211
212 }
213
214 if(!jump)
215 velY += gravity;
216 else
217 velY = 0;
218 player.x += velX;
219 player.y += velY;
220
221 jump = (player.y >= groundHeight);
222
223 if(jump)
224 {
225 player.y = groundHeight;
226 } draw = true;
227
228
229 cameraUpdate(cameraPosition, player, 32, 32);
230 al_identity_transform(&camera);
231 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
232 al_use_transform(&camera);
233
234 }
235
236 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
237 {
238 fireArrow(arrow, player, bowShot);
239 }
240 if(collision(player, 371, 470, 325, 30, 32, 32))//First Platform (long)
241 {
242 if(player.y > 470)
243 {
244 groundHeight = 470;
245 player.y = groundHeight;
246 player.y += velY;
247 }
248 }
249 else if(collision(player, 70, 390, 200, 38, 32, 32))//Second Platform (short)
250 {
251 groundHeight = 390;
252 player.y = groundHeight;
253 player.y += velY;
254 }
255 else if(collision(player, 79, 275, 200, 30, 32, 32))//Third Platform (short)
256 {
257 if(player.y > 275)
258 {
259 groundHeight = 275;
260 player.y = groundHeight;
261 player.y += velY;
262 }
263 }
264 else if(collision(player, 313, 220, 250, 30, 32, 32))//Fourth Platform (Medium)
265 {
266 if(player.y > 220)
267 {
268 groundHeight = 220;
269 player.y = groundHeight;
270 player.y += velY;
271 }
272 }
273 else if(collision(player, 655, 165, 36, 30, 32, 32))//Fifth Platform (first tiny)
274 {
275 if(player.y > 165)
276 {
277 groundHeight = 165;
278 player.y = groundHeight;
279 player.y += velY;
280 }
281 }
282 else if(collision(player, 582, 70, 33, 30, 32, 32))//Sixth Platform (second tiny)
283 {
284 if(player.y > 70)
285 {
286 groundHeight = 70;
287 player.y = groundHeight;
288 player.y += velY;
289 }
290 }
291 else if(collision(player, 763, 30, 36, 30, 32, 32))//Seventh Platform (third tiny)
292 {
293 if(player.y > 30)
294 {
295 groundHeight = 30;
296 player.y = groundHeight;
297 player.y += velY;
298 }
299 }
300 else if(collision(player, 926, 77, 200, 38, 32, 32))//Eighth Platform (short)
301 {
302 if(player.y > 77)
303 {
304 groundHeight = 77;
305 player.y = groundHeight;
306 player.y += velY;
307 }
308 }
309 else if(collision(player, 926, 150, 200, 38, 32, 32))//Ninth platform (short)
310 {
311 if(player.y > 150)
312 {
313 groundHeight = 150;
314 player.y = groundHeight;
315 player.y += velY;
316 }
317 }
318 else if(collision(player, 1202, 120, 36, 30, 32, 32))//Tenth platform (fourth tiny)
319 {
320 if(player.y > 120)
321 {
322 groundHeight = 120;
323 player.y = groundHeight;
324 player.y += velY;
325 }
326 }
327 else if(collision(player, 1162, 335, 118, 38, 32, 32))//11th Platform (first short medium)
328 {
329 if(player.y > 335)
330 {
331 groundHeight = 335;
332 player.y = groundHeight;
333 player.y += velY;
334 }
335 }
336 else if(collision(player, 1045, 327, 36, 30, 32, 32))//12th Platform (fifth tiny)
337 {
338 if(player.y > 327)
339 {
340 groundHeight = 327;
341 player.y = groundHeight;
342 player.y += velY;
343 }
344 }
345 else if(collision(player, 916, 327, 36, 30, 32, 32))//13th Platform (sixth tiny)
346 {
347 if(player.y > 327)
348 {
349 groundHeight = 327;
350 player.y = groundHeight;
351 player.y += velY;
352 }
353 }
354 else
355 {
356 groundHeight = 535;
357 }
1if(coinCollide(coin, player, coin.x, coin.y, coin.width, coin.height, 32, 32, coinLive, coinCount))
2 {
3 coin.live = false;
4 ++coinCount;
5 coin.x = -500;
6 coin.y = -500;
7 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
8 }
9 if(coinCollide(coin2, player, coin2.x, coin2.y, coin2.width, coin2.height, 32, 32, coinLive, coinCount))
10 {
11 coin2.live = false;
12 ++coinCount;
13 coin2.x = -500;
14 coin2.y = -500;
15 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
16 }
17 if(collision(player, key.x, key.y, key.width, key.height, 32, 32))//Collide with key
18 {
19 al_play_sample(keyCollection, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
20 key.x = -500;
21 key.y = -500;
22 key.live = false;
23 gotKey = true;
24 }
25 /*if(collision(player, enemy.x, enemy.y, enemy.width, enemy.height, 32, 32))//Player collision with enemy
26 {
27 player.x = 10;
28 player.y = 546;
29 --player.lives;
30 arrow.live = false;
31 dir = RIGHT;
32 }
33 if(collision(player, enemy2.x, enemy2.y, enemy2.width, enemy2.width, 32, 32))//Player collision with enemy
34 {
35 player.x = 10;
36 player.y = 546;
37 --player.lives;
38 arrow.live = false;
39 dir = RIGHT;
40 }*/
41 if(collision(player, 1220, 535, 90, 61, 32, 32))//Crocodile collision
42 {
43 player.x = 10;
44 player.y = 535;
45 --player.lives;
46 }
47 if(player.x == 1090 && player.y == 535)
48 {
49 al_play_sample(crocSound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
50 }
51 /*if(collideArrow(enemy, arrow, enemy.live, arrow.live))//Arrow Collision with enemy
52 {
53 enemy.live = false;
54 enemy.x = 0;
55 enemy.y = 0;
56 arrow.live = false;
57 ++player.score;
58 }
59 if(collideArrow(enemy2, arrow, enemy2.live, arrow.live))//Arrow Collision with enemy
60 {
61 enemy2.live = false;
62 enemy2.x = 0;
63 enemy2.y = 0;
64 arrow.live = false;
65 ++player.score;
66 }*/
67
68 if(draw && al_is_event_queue_empty(event_queue))
69 {
70 draw = false;
71 al_draw_bitmap(background2, 0, 0, 0);
72 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, 0);
73 drawKey(key, keyImg);
74 drawArrow(arrow, arrow_r, arrow_l);
75 drawCoin(coin, coinImg);
76 drawCoin(coin2, coinImg);
77 drawEnemy(enemy, enemyImg, NUM_ENEMY);
78 drawEnemy(enemy2, enemyImg, NUM_ENEMY);
79 al_identity_transform(&identity);
80 al_use_transform(&identity);
81 al_draw_textf(font, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrow.arrowCount);
82 al_draw_textf(font, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount);
83 al_draw_textf(font, al_map_rgb(252, 209, 22), 110, 10, 0, "Lives: %i", player.lives);
84 //al_draw_textf(fps, al_map_rgb(252, 209, 22), 110, 50, 0, "FPS: %i", FPS);
85 al_flip_display();
86 al_clear_to_color(al_map_rgb(255, 255, 255));
87
88 }
89 }
90
91 al_destroy_bitmap(character);
92 al_destroy_bitmap(ground);
93 al_destroy_bitmap(coinImg);
94 al_destroy_bitmap(background2);
95 al_destroy_bitmap(arrow_l);
96 al_destroy_bitmap(arrow_r);
97 al_destroy_bitmap(keyImg);
98 al_destroy_sample(coinCollect);
99 al_destroy_sample(bowShot);
100 al_destroy_sample(bgSong);
101 al_destroy_sample(keyCollection);
102 al_destroy_sample_instance(songInstance);
103 al_destroy_font(font);
104 al_destroy_display(display);
105 al_destroy_event_queue(event_queue);
106 al_destroy_timer(timer);
107 return 0;
108}
109void initPlayer(Player &player)
110{
111 player.x = 10;
112 player.y = 535;
113 player.lives = 3;
114 player.speed = 5;
115 player.score = 0;
116 dir = RIGHT;
117}
118
119void initEnemy(Enemy enemy[], int x, int y, int numEnemy)
120{
121 for(int i = 0; i < numEnemy; i++)
122 {
123 enemy[i].live = true;
124 enemy[i].speedR = 2;
125 enemy[i].speedL = -2;
126 enemy[i].speed = 2;
127 enemy[i].x = x;
128 enemy[i].y = y;
129 enemy[i].width = 29;
130 enemy[i].height = 29;
131 }
132
133}
134void drawEnemy(Enemy enemy[], ALLEGRO_BITMAP *image, int numEnemy)
135{
136 for(int i = 0; i < numEnemy; i++)
137 {
138 if(enemy[i].live)
139 {
140 al_draw_bitmap(image, enemy[i].x, enemy[i].y, 0);
141 }
142 }
143
144}
145void updateEnemy(Enemy enemy[], int posX, int posX2, int numEnemy)
146{
147 for(int i = 0; i < numEnemy; i++)
148 {
149 if(enemy[i].live)
150 {
151 enemy[i].x += enemy[i].speed;
152 if(enemy[i].x >= posX)
153 {
154 enemy[i].x = posX;
155 enemy[i].speed = enemy[i].speedL;
156 }
157 if(enemy[i].x <= posX2)
158 {
159 enemy[i].x = posX2;
160 enemy[i].speed = enemy[i].speedR;
161 }
162 }
163 }
164
165}
166bool collideArrow(Enemy enemy[], Arrow &arrow, bool eLive, bool aLive, int numEnemy)
167{
168 for(int i = 0; i < numEnemy; i++)
169 {
170 if((arrow.x > enemy[i].x + enemy[i].width) || (arrow.y > enemy[i].y + enemy[i].height) ||
171 (enemy[i].x > arrow.x + arrow.height) || (enemy[i].y > arrow.y + arrow.height) && (enemy[i].live) && (arrow.live))
172 {
173 return false;
174 }
175 return true;
176 }
177
178}
179
180void initCoin(Coin &coin, int x, int y)
181{
182 coin.width = 16;
183 coin.height = 16;
184 coin.live = true;
185 coin.x = x;
186 coin.y = y;
187}
188void drawCoin(Coin &coin, ALLEGRO_BITMAP *image)
189{
190 float scale = 5;
191 float time_step = 0;
192 float time = 0;
193 time += time_step;
194 float offset_y = abs(sin(time)) * scale;
195 if(coin.live)
196 {
197 if(image == NULL)
198 al_draw_filled_circle(coin.x - 10, coin.y - 10, 20, al_map_rgb(255,0,255));
199 else
200 al_draw_bitmap(image, coin.x, coin.y - offset_y, 0);
201 }
202}
203bool coinCollide(Coin &coin,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount)
204{
205 if((player.x > cX + cWidth) || (player.y > cY + cHeight) ||
206 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin.live))
207 {
208 return false;
209 }
210 return true;
211}
212
213void initArrow(Arrow &arrow)
214{
215 arrow.speed = 4.0;
216 arrow.live = false;
217 arrow.arrowCount = 10;
218 arrow.width = 16;
219 arrow.height = 5;
220 arrow.aVelX = 0;
221 arrow.aVelY = 0;
222}
223void drawArrow(Arrow &arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2)
224{
225 if(arrow.live && dir == RIGHT)
226 {
227 al_draw_bitmap(image, arrow.x, arrow.y, 0);
228 }
229 if(arrow.live && dir == LEFT)
230 {
231 al_draw_bitmap(image2, arrow.x, arrow.y, 0);
232 }
233
234}
235void fireArrow(Arrow &arrow, Player &player, ALLEGRO_SAMPLE *bowShot)
236{
237 if(!arrow.live)
238 {
239 --arrow.arrowCount;
240 arrow.live = true;
241 arrow.x = player.x + 17;
242 arrow.y = player.y + 22;
243 fired = true;
244 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
245
246 }
247
248}
249void updateArrow(Arrow &arrow)
250{
251 if(arrow.live && fired)
252 {
253 if(dir == RIGHT)
254 {
255 arrow.x += arrow.speed;
256 }
257 if(dir == LEFT)
258 {
259 arrow.x -= arrow.speed;
260 }
261 if(arrow.x >= 2000 - 10)
262 {
263 arrow.live = false;
264 }
265 if(arrow.x <= 0 + 3)
266 {
267 arrow.live = false;
268 }
269 else if(arrow.arrowCount <= 0)
270 {
271 fired = false;
272 }
273
274 }
275}
276
277void initKey(Key &key)
278{
279 key.x = 979;
280 key.y = 151;
281 key.width = 32;
282 key.height = 32;
283 key.live = true;
284}
285void drawKey(Key &key, ALLEGRO_BITMAP *image)
286{
287 if(key.live)
288 {
289 al_draw_bitmap(image, 979, 151, 0);
290 }
291}
292
293bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight)
294{
295 if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) ||
296 (ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10))
297 {
298 return false;
299 }
300 return true;
301}
302
303void cameraUpdate(float *cameraPosition, Player &player, float width, float height)
304{
305 cameraPosition[0] = -(ScreenWidth / 2) + (player.x + width / 2);
306 cameraPosition[1] = -(ScreenHeight) + (player.y + height / 2);
307
308 if(cameraPosition[0] < 0)
309 cameraPosition[0] = 0;
310 if(cameraPosition[1] < 0)
311 cameraPosition[1] = 0;
312 if(cameraPosition[0] > 3000 - ScreenWidth)
313 cameraPosition[0] = 3000 - ScreenWidth;
314
315}
|
Schyfis
Member #9,752
May 2008
![]() |
DJLad16 said: Haha, switching the two numbers around in update enemy got it working. I've been at that all day and all I needed to do was that... FML. That's why you should always give your function parameters descriptive names, such as minimum_x and maximum_x! It will eliminate a lot of problems for you in the future if you remember to do this. Quote: And dizzy, I tried your suggestion with class arrays, but I couldn't get it to work. Can you show us how you were doing it? ________________________________________________________________________________________________________ |
DJLad16
Member #14,857
January 2013
|
I had it, but then undo'd it back to the original. And I've been programming and doing stuff for my game for quite some time now and I'm tired, so I'll give you a post tomorrow if I have the time |
|