Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » My kangaroos are high...

This thread is locked; no one can reply to it. rss feed Print
My kangaroos are high...
DJLad16
Member #14,857
January 2013

IGNORE THIS POST! I GOT IT WORKING :D

Now I've finished implementing the enemies as black boxes, I had to change them to the actually enemies I wanted which was no problem. But when I ran it, the animation the kangaroos are supposed to do is going really fast. I tried to them in my timer where my character animation is but that didn't work. So I created another timer for the enemy animation, but that didn't work either - even at 1 fps. I have no idea what is wrong with it. (I've removed some code, and yes I know "coin1, coin2, etc..." and "enemy1, enemy2, etc..." I'm going to do a lot of refactoring after this is sorted)

#SelectExpand
1int main() 2{ 3 ALLEGRO_DISPLAY *display; 4 5 if(!al_init()) 6 { 7 al_show_native_message_box(0, 0, "Error", "Falied to initialize allegro", 0, 0); 8 return -1; 9 } 10 11 display = al_create_display(ScreenWidth, ScreenHeight); 12 13 if(!display) 14 { 15 al_show_native_message_box(0, 0, "Error", "Falied to initialize the display", 0, 0); 16 return -1; 17 } 18 19 Player player; 20 Enemy enemy; 21 Enemy enemy2; 22 Enemy enemy3; 23 Enemy enemy4; 24 Enemy enemy5; 25 Enemy enemy6; 26 Enemy enemy7; 27 Enemy enemy8; 28 Enemy enemy9; 29 Enemy enemy10; 30 Coin coin; 31 Coin coin2; 32 Coin coin3; 33 Coin coin4; 34 Coin coin5; 35 Coin coin6; 36 Coin coin7; 37 Coin coin8; 38 Coin coin9; 39 Coin coin10; 40 Coin coin11; 41 Arrow arrow; 42 Key key; 43 int groundHeight = 535; 44 bool done = false, active = false, draw = true; 45 bool jump = false; 46 bool gotKey = false; 47 int sourceX = 32, sourceY = 0; 48 int coinCount = 0; 49 const float FPS = 60.0; 50 const float frameFPS = 15.0; 51 const float enemyFPS = 0.1; 52 const float gravity = 1; 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 if(!al_init_image_addon()) 62 { 63 al_show_native_message_box(0, 0, "Error", "Failed to initalize image addon", 0, 0); 64 return -1; 65 } 66 al_init_primitives_addon(); 67 if(!al_init_primitives_addon()) 68 { 69 al_show_native_message_box(0, 0, "Error", "Failed to initalize primitives addon", 0, 0); 70 return -1; 71 } 72 al_install_keyboard(); 73 if(!al_install_keyboard()) 74 { 75 al_show_native_message_box(0, 0, "Error", "Failed to initalize keyboard", 0, 0); 76 return -1; 77 } 78 al_init_font_addon(); 79 al_init_ttf_addon(); 80 al_install_audio(); 81 if(!al_install_audio()) 82 { 83 al_show_native_message_box(0, 0, "Error", "Failed to initalize audio", 0, 0); 84 return -1; 85 } 86 al_init_acodec_addon(); 87 if(!al_init_acodec_addon()) 88 { 89 al_show_native_message_box(0, 0, "Error", "Failed to initalize codec", 0, 0); 90 return -1; 91 } 92 93 initPlayer(player); 94 initEnemy(enemy, 86, 391); 95 initEnemy(enemy2, 1167, 333); 96 initEnemy(enemy3, 378, 473); 97 initEnemy(enemy4, 649, 473); 98 initEnemy(enemy5, 1608, 60); 99 initEnemy(enemy6, 1850, 60); 100 initEnemy(enemy7, 2130, 60); 101 initEnemy(enemy8, 1762, 60); 102 initEnemy(enemy9, 2000, 60); 103 initEnemy(enemy10, 1306, 230); 104 initArrow(arrow); 105 initCoin(coin, 550, 480); 106 initCoin(coin2, 120, 398); 107 initCoin(coin3, 1413, 80); 108 initCoin(coin4, 1736, 68); 109 initCoin(coin5, 1970, 68); 110 initCoin(coin6, 431, 229); 111 initCoin(coin7, 769, 38); 112 initCoin(coin8, 1051, 341); 113 initCoin(coin9, 922, 343); 114 initCoin(coin10, 2377, 192); 115 initCoin(coin11, 2502, 446); 116 initKey(key); 117 118 119 al_reserve_samples(5); //Respresents how many audio clips I have in the game 120 al_set_display_icon(display, icon); 121 122 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP); 123 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer()); 124 al_set_window_title(display, "Australian Outback"); 125 al_register_event_source(event_queue, al_get_keyboard_event_source()); 126 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 127 al_register_event_source(event_queue, al_get_timer_event_source(frameTimer)); 128 al_register_event_source(event_queue, al_get_timer_event_source(enemyTimer)); 129 al_register_event_source(event_queue, al_get_display_event_source(display)); 130 131 //al_play_sample_instance(songInstance); 132 133 al_start_timer(timer); 134 al_start_timer(frameTimer); 135 al_start_timer(enemyTimer); 136 while(!done) 137 { 138 ALLEGRO_EVENT event; 139 al_wait_for_event(event_queue, &event); 140 al_get_keyboard_state(&keystate); 141 142 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE)) 143 { 144 done = true; 145 } 146 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 147 { 148 done = true; 149 } 150 151 152 else if(event.type == ALLEGRO_EVENT_TIMER) 153 { 154 if(event.timer.source == timer) 155 { 156 active = true; 157 updateArrow(arrow, player); 158 159 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE)) 160 { 161 fireArrow(arrow, player, bowShot); 162 } 163 if(al_key_down(&keystate, ALLEGRO_KEY_D)) 164 { 165 velX = player.speed; 166 player.dir = RIGHT; 167 168 } 169 else if(al_key_down(&keystate, ALLEGRO_KEY_A)) 170 { 171 velX = -player.speed; 172 player.dir = LEFT; 173 } 174 else 175 { 176 velX = 0; 177 active = false; 178 } 179 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump) 180 { 181 velY = -jumpSpeed; 182 jump = false; 183 } 184 } 185 if(active) 186 { 187 sourceX += al_get_bitmap_width(character) / 3; 188 } 189 else 190 { 191 sourceX = 32; 192 } 193 if(sourceX >= al_get_bitmap_width(character)) 194 { 195 sourceX = 0; 196 } 197 sourceY = player.dir; 198 199 200 201 if(!jump) 202 velY += gravity; 203 else 204 velY = 0; 205 player.x += velX; 206 player.y += velY; 207 208 jump = (player.y >= groundHeight); 209 210 if(jump) 211 { 212 player.y = groundHeight; 213 } draw = true; 214 215 updateEnemy(enemy, 238, 78); 216 updateEnemy(enemy2, 1236, 1167); 217 updateEnemy(enemy3, 660, 378); 218 updateEnemy(enemy4, 660, 378); 219 updateEnemy(enemy5, 2163, 1599); 220 updateEnemy(enemy6, 2163, 1599); 221 updateEnemy(enemy7, 2163, 1599); 222 updateEnemy(enemy8, 2163, 1599); 223 updateEnemy(enemy9, 2163, 1599); 224 updateEnemy(enemy10, 1506, 1306); 225 226 227 cameraUpdate(cameraPosition, player, 32, 32); 228 al_identity_transform(&camera); 229 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 230 al_use_transform(&camera); 231 232 } 233 234 if(collision(player, 1480, 295, 692, 305, 32, 32))//Mountain collision 235 { 236 player.x -= player.speed; 237 } 238 if(collision(player, 1505, 300, 580, 300, 32, 32))//Mountain collision 2 239 { 240 player.x = 10; 241 player.y = 535; 242 --player.lives; 243 } 244 if(collision(player, 2135, 535, 530, 50, 32, 32))//Quicksand colllision 245 { 246 player.x = 10; 247 player.y = 535; 248 --player.lives; 249 } 250 251 if(draw && al_is_event_queue_empty(event_queue)) 252 { 253 draw = false; 254 255 al_draw_bitmap(background2, 0, 0, 0); 256 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, 0); 257 drawKey(key, keyImg); 258 drawArrow(arrow, arrow_r, arrow_l, player); 259 drawCoin(coin, coinImg); 260 drawCoin(coin2, coinImg); 261 drawCoin(coin3, coinImg); 262 drawCoin(coin4, coinImg); 263 drawCoin(coin5, coinImg); 264 drawCoin(coin6, coinImg); 265 drawCoin(coin7, coinImg); 266 drawCoin(coin8, coinImg); 267 drawCoin(coin9, coinImg); 268 drawCoin(coin10, coinImg); 269 drawCoin(coin11, coinImg); 270 drawEnemy(enemy, enemyImg2); 271 drawEnemy(enemy2, enemyImg2); 272 drawEnemy(enemy3, enemyImg2); 273 drawEnemy(enemy4, enemyImg2); 274 drawEnemy(enemy5, enemyImg2); 275 drawEnemy(enemy6, enemyImg2); 276 drawEnemy(enemy7, enemyImg2); 277 drawEnemy(enemy8, enemyImg2); 278 drawEnemy(enemy9, enemyImg2); 279 drawEnemy(enemy10, enemyImg2); 280 al_identity_transform(&identity); 281 al_use_transform(&identity); 282 al_draw_textf(font, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrowCount); 283 al_draw_textf(font, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount); 284 al_draw_textf(font, al_map_rgb(252, 209, 22), 110, 10, 0, "Lives: %i", player.lives); 285 //al_draw_textf(fps, al_map_rgb(252, 209, 22), 110, 50, 0, "FPS: %i", FPS); 286 al_flip_display(); 287 al_clear_to_color(al_map_rgb(255, 255, 255)); 288 289 } 290 } 291} 292void initPlayer(Player &player) 293{ 294 player.dir = LEFT; 295 player.x = 1316; 296 player.y = 0; 297 player.lives = 3; 298 player.speed = 5; 299 player.score = 0; 300 player.dir = RIGHT; 301} 302 303void initEnemy(Enemy &enemy, int x, int y) 304{ 305 enemy.live = true; 306 enemy.speedR = 2; 307 enemy.speedL = -2; 308 enemy.speed = 2; 309 enemy.x = x; 310 enemy.y = y; 311 enemy.dir = eLEFT; 312 enemy.eSourceX = 32; 313 enemy.eSourceY = 0; 314 enemy.acitve = false; 315 enemy.width = 29; 316 enemy.height = 29; 317} 318void drawEnemy(Enemy &enemy, ALLEGRO_BITMAP *image) 319{ 320 if(enemy.live) 321 { 322 enemy.acitve = true; 323 if(enemy.acitve) 324 { 325 enemy.eSourceX += al_get_bitmap_width(image) / 3; 326 } 327 else 328 { 329 enemy.eSourceX = 0; 330 } 331 if(enemy.eSourceX >= al_get_bitmap_width(image)) 332 { 333 enemy.eSourceX = 0; 334 } 335 enemy.eSourceY = enemy.dir; 336 337 al_draw_bitmap_region(image, enemy.eSourceX, enemy.eSourceY * al_get_bitmap_height(image) / 2, 32, 32, enemy.x, enemy.y, 0); 338 339 } 340} 341void updateEnemy(Enemy &enemy, int posX, int posX2) 342{ 343 if(enemy.live) 344 { 345 enemy.x += enemy.speed; 346 if(enemy.x >= posX) 347 { 348 enemy.dir = eLEFT; 349 enemy.x = posX; 350 enemy.speed = enemy.speedL; 351 } 352 if(enemy.x <= posX2) 353 { 354 enemy.dir = eRIGHT; 355 enemy.x = posX2; 356 enemy.speed = enemy.speedR; 357 } 358 } 359}

Dizzy Egg
Member #10,824
March 2009
avatar

Best first line of a Friday night drunken Allegro.cc post ever, nice one DJLad! 8-)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

LennyLen
Member #5,313
December 2004
avatar

Quote:

My kangaroos are high...

Better tie 'em down then, sport.

Go to: