My game bugs out when in release mode?
DJLad16

When I run my program in release mode, the game bugs out bad. If you kill and enemy, it kills all of them and the score just constantly increases. But when I run it in debug mode, it all works fine, the code is the same so I can't see why it would do this. I had the "killing all enemies" problem before, but fixed easily. (I'm not sure if you need the code)

Thomas Fjellstrom

I'm betting you aren't initializing variables consistently.

DJLad16

???

#SelectExpand
1void initPlayer(Player &player); 2 3void initEnemy(Enemy enemy[], int speedR, int speedL, int speed, int array_size); 4void enemyAnimation(Enemy enemy[], ALLEGRO_BITMAP *image, int array_size); 5void drawEnemy(Enemy enemy[], ALLEGRO_BITMAP *image, int array_size); 6void updateEnemy(Enemy enemy[], int array_size); 7bool collideArrow(Player & player, int eX, int eY, int eWidth, int eHeight, int aX, int aY, int aWidth, int aHeight, bool eLive, bool aLive); 8 9void initCoin(Coin coin[], int array_size); 10void drawCoin(Coin coin[], ALLEGRO_BITMAP *image, int array_size); 11void coinAnimation(Coin coin[], ALLEGRO_BITMAP *image, int array_size); 12void coinCollision(Coin coin[], Player &player, int coinCount, int cSize, int pWidth, int pHeight, ALLEGRO_SAMPLE *sample); 13 14void initArrow(Arrow arrow[], int array_size); 15void drawArrow(Arrow arrow[], int array_size, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2, Player &player); 16void fireArrow(Arrow arrow[], int array_size, Player &player, ALLEGRO_SAMPLE *bowShot); 17void updateArrow(Arrow arrow[], int array_size, Player &player); 18 19void initKey(Key &key); 20void drawKey(Key &key, ALLEGRO_BITMAP *image); 21 22void initText(Text &text); 23void drawText(Text &text, ALLEGRO_COLOR yellow); 24 25void initTeleport(Teleport &teleport); 26void teleportAnimation(Teleport &teleport, ALLEGRO_BITMAP *image); 27void drawTeleport(Teleport &teleport, ALLEGRO_BITMAP *image); 28 29bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight); 30void handlePlatCollision(Player &player, int ex, int ey, int ewidth, int eheight, int pwidth, int pheight, int &groundheight, float vely); 31void handleArrowCollision(Enemy enemy[], Arrow arrow[], Player &player, int array_size, int array_size2, int enemyKilled); 32void handleCoinCollision(Player &player, Coin coin[], ALLEGRO_SAMPLE *coincollect, int array_size); 33void handleEnemyCollision(Player &player, Enemy &enemy, Arrow arrow[]); 34 35void cameraUpdate(float *cameraPosition,Player &player, float width, float height); 36 37void changeState(int &state, int newState); 38void changeLevel(int &level, int newLevel); 39//void changeLevelPart(int &lvPart, int newLvPart); 40 41//Global Variables 42enum direction {LEFT, RIGHT}; 43enum aDirection {aLEFT, aRIGHT}; 44enum eDirection {eLEFT, eRIGHT}; 45enum GameStaes {MENU, PLAYING, DEATH}; 46enum Levels {LVL1, LVL2, LV2P2, LVL3, LV3P2}; 47//enum lpChange {LV2P2, LV3P2}; 48const int NUM_ENEMY = 11; 49const int NUM_ARROW = 10; 50const int NUM_COIN = 11; 51//int sourceX = 12; 52//int sourceY = 0; 53int tSourceX = 32; 54int tSourceY = 0; 55static bool fired = false; 56bool coinLive = false; 57bool left, right = false; 58 59Player player; 60 61Enemy enemy[NUM_ENEMY]; 62 63Coin coin[NUM_COIN]; 64 65Arrow arrow[NUM_ARROW]; 66 67Key key; 68 69Text text; 70 71Teleport teleport; 72 73 74int main() 75{ 76 ALLEGRO_DISPLAY *display; 77 78 if(!al_init()) 79 { 80 al_show_native_message_box(0, 0, "Error", "Falied to initialize allegro", 0, 0); 81 return -1; 82 } 83 84 display = al_create_display(ScreenWidth, ScreenHeight); 85 86 if(!display) 87 { 88 al_show_native_message_box(0, 0, "Error", "Falied to initialize the display", 0, 0); 89 return -1; 90 } 91 92 93 94 int groundHeight = 518; 95 int state = -1; 96 int level = -1; 97 int newLvPart = -1; 98 bool done = false, active = false, draw = true; 99 bool jump = false; 100 bool gotKey = false; 101 /*bool arrowCollision = false; 102 bool platformCollision = false; 103 bool enemyCollision = false; 104 bool coinCollision = false;*/ 105 bool lv2P2 = false; 106 bool Lvl2 = false; 107 int coinCount = 0; 108 int enemyKilled = 0; 109 const float FPS = 60.0; 110 const float frameFPS = 15.0; 111 const float enemyFPS = 10.0; 112 const float gravity = 1.0; 113 float jumpSpeed = 15.0; 114 float velX = 0, velY = 0; 115 float gameTime = 0; 116 int frames = 0; 117 int gameFPS = 0; 118 119 float cameraPosition[2] = {0,0}; 120 121 std::string workingDirectory = al_get_current_directory(); 122 123 al_init_image_addon(); 124 if(!al_init_image_addon()) 125 { 126 al_show_native_message_box(0, 0, "Error", "Failed to initalize image addon", 0, 0); 127 return -1; 128 } 129 al_init_primitives_addon(); 130 if(!al_init_primitives_addon()) 131 { 132 al_show_native_message_box(0, 0, "Error", "Failed to initalize primitives addon", 0, 0); 133 return -1; 134 } 135 al_install_keyboard(); 136 if(!al_install_keyboard()) 137 { 138 al_show_native_message_box(0, 0, "Error", "Failed to initalize keyboard", 0, 0); 139 return -1; 140 } 141 al_init_font_addon(); 142 al_init_ttf_addon(); 143 al_install_audio(); 144 if(!al_install_audio()) 145 { 146 al_show_native_message_box(0, 0, "Error", "Failed to initalize audio", 0, 0); 147 return -1; 148 } 149 al_init_acodec_addon(); 150 if(!al_init_acodec_addon()) 151 { 152 al_show_native_message_box(0, 0, "Error", "Failed to initalize codec", 0, 0); 153 return -1; 154 } 155 156 changeState(state, MENU); 157 158 initPlayer(player); 159 initEnemy(enemy, 3, -3, 3, NUM_ENEMY); 160 initArrow(arrow, NUM_ARROW); 161 initCoin(coin, NUM_COIN); 162 initKey(key); 163 initText(text); 164 initTeleport(teleport); 165 166 ALLEGRO_KEYBOARD_STATE keystate; 167 ALLEGRO_TRANSFORM camera; 168 ALLEGRO_TRANSFORM identity; 169 ALLEGRO_COLOR yellow = al_map_rgb(252, 209, 22); 170 ALLEGRO_SAMPLE *coinCollect = al_load_sample("Files/coin collect sound.wav"); 171 ALLEGRO_SAMPLE *bowShot = al_load_sample("Files/bow sound effect.wav"); 172 ALLEGRO_SAMPLE *bgSong = al_load_sample("Files/Background song 2.wav"); 173 ALLEGRO_SAMPLE *keyCollection = al_load_sample("Files/key collection.wav"); 174 ALLEGRO_SAMPLE *jumpSound = al_load_sample("Files/jump sound.wav"); 175 ALLEGRO_SAMPLE *snakeSound = al_load_sample("Files/rattlesnake sound effect.wav"); 176 ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(bgSong); 177 ALLEGRO_BITMAP *enemyImg = al_load_bitmap("Files/enemy.png"); 178 ALLEGRO_BITMAP *enemyImg2 = al_load_bitmap("Files/Kangaroo(left + right).png"); 179 ALLEGRO_BITMAP *lvl1 = al_load_bitmap("Files/Level 1.png"); 180 ALLEGRO_BITMAP *lvl2 = al_load_bitmap("Files/level 2.png"); 181 ALLEGRO_BITMAP *lvl2P2 = al_load_bitmap("Files/Level 2 Part 2.png"); 182 ALLEGRO_BITMAP *img = al_load_bitmap("Files/spritesheet(Bow & left + right).png"); 183 ALLEGRO_BITMAP *arrow_r = al_load_bitmap("Files/Arrow(RIGHT).png"); 184 ALLEGRO_BITMAP *arrow_l = al_load_bitmap("Files/Arrow(LEFT).png"); 185 ALLEGRO_BITMAP *coinImg = al_load_bitmap("Files/coin2.png"); 186 ALLEGRO_BITMAP *coinImg2 = al_load_bitmap("Files/coin img2.png"); 187 ALLEGRO_BITMAP *keyImg = al_load_bitmap("Files/key.png"); 188 ALLEGRO_BITMAP *icon = al_load_bitmap("Files/game icon.png"); 189 ALLEGRO_BITMAP *menu = al_load_bitmap("Files/menu.png"); 190 ALLEGRO_BITMAP *death = al_load_bitmap("Files/death.png"); 191 ALLEGRO_BITMAP *teleImg = al_load_bitmap("Files/teleport sprite.png"); 192 ALLEGRO_FONT *font = al_load_font("Files/JUNGBN__.TTF", 23, 0); 193 ALLEGRO_FONT *score = al_load_font("Files/JUNGBN__.TTF", 50, 0); 194 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS); 195 ALLEGRO_TIMER *frameTimer = al_create_timer(1.0/frameFPS); 196 ALLEGRO_TIMER *enemyTimer = al_create_timer(1.0/enemyFPS); 197 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 198 199 al_reserve_samples(6); //Respresents how many audio clips I have in the game 200 al_set_display_icon(display, icon); 201 al_hide_mouse_cursor(display); 202 203 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP); 204 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer()); 205 al_set_window_title(display, "Australian Outback"); 206 al_register_event_source(event_queue, al_get_keyboard_event_source()); 207 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 208 al_register_event_source(event_queue, al_get_timer_event_source(frameTimer)); 209 al_register_event_source(event_queue, al_get_timer_event_source(enemyTimer)); 210 al_register_event_source(event_queue, al_get_display_event_source(display)); 211 212 al_play_sample_instance(songInstance); 213 214 al_start_timer(timer); 215 al_start_timer(frameTimer); 216 al_start_timer(enemyTimer); 217 while(!done) 218 { 219 ALLEGRO_EVENT event; 220 al_wait_for_event(event_queue, &event); 221 al_get_keyboard_state(&keystate); 222 223 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE)) 224 { 225 done = true; 226 } 227 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 228 { 229 done = true; 230 } 231 232 233 else if(event.type == ALLEGRO_EVENT_TIMER) 234 { 235 if(event.timer.source == timer) 236 { 237 active = true; 238 frames++; 239 if(al_current_time() - gameTime >= 1) 240 { 241 gameTime = al_current_time(); 242 gameFPS = frames; 243 frames = 0; 244 } 245 246 if(al_key_down(&keystate, ALLEGRO_KEY_ENTER) && state == MENU) 247 { 248 changeState(state, PLAYING); 249 changeLevel(level, LVL1); 250 } 251 if(al_key_down(&keystate, ALLEGRO_KEY_ENTER) && state == DEATH) 252 { 253 changeState(state, PLAYING); 254 } 255 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE)) 256 { 257 fireArrow(arrow, NUM_ARROW, player, bowShot); 258 } 259 else 260 fired = false; 261 if(al_key_down(&keystate, ALLEGRO_KEY_S)) 262 { 263 player.x = 755; 264 player.y = 0; 265 }

#SelectExpand
1if(al_key_down(&keystate, ALLEGRO_KEY_F)) 2 { 3 player.x = 2700; 4 player.y = 535; 5 player.lives = 10; 6 coinCount = 11; 7 enemyKilled = 11; 8 gotKey = true; 9 } 10 if(al_key_down(&keystate, ALLEGRO_KEY_ENTER) && level == LVL2) 11 { 12 changeLevel(level, LV2P2); 13 lv2P2 = true; 14 } 15 if(al_key_down(&keystate, ALLEGRO_KEY_BACKSPACE) && level == LV2P2) 16 { 17 changeLevel(level, LVL2); 18 } 19 if(al_key_down(&keystate, ALLEGRO_KEY_D)) 20 { 21 velX = player.speed; 22 player.dir = RIGHT; 23 } 24 else if(al_key_down(&keystate, ALLEGRO_KEY_A)) 25 { 26 velX = -player.speed; 27 player.dir = LEFT; 28 } 29 else 30 { 31 velX = 0; 32 active = false; 33 } 34 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump) 35 { 36 velY = -jumpSpeed; 37 jump = false; 38 al_play_sample(jumpSound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 39 } 40 41 player.playerAnimation(active); 42 } 43 if(!jump) 44 velY += gravity; 45 else 46 velY = 0; 47 player.x += velX; 48 player.y += velY; 49 50 jump = (player.y + 1 >= groundHeight); 51 52 if(jump) 53 { 54 player.y = groundHeight - 1; 55 } draw = true; 56 57 if(event.timer.source == enemyTimer) 58 { 59 enemyAnimation(enemy, enemyImg2, NUM_ENEMY); 60 coinAnimation(coin, coinImg2, NUM_COIN); 61 teleportAnimation(teleport, teleImg); 62 } 63 64 65 66 cameraUpdate(cameraPosition, player, 32, 32); 67 al_identity_transform(&camera); 68 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 69 al_use_transform(&camera); 70 71 } 72 73 if(state == MENU) 74 {} 75 else if(state == PLAYING && level == LVL1) 76 { 77 updateArrow(arrow, NUM_ARROW, player); 78 updateEnemy(enemy, NUM_ENEMY); 79 } 80 else if(state == PLAYING && level == LVL2) 81 { 82 updateArrow(arrow, NUM_ARROW, player); 83 } 84 else if(state == PLAYING && level == LV2P2) 85 { 86 updateArrow(arrow, NUM_ARROW, player); 87 } 88 if(level == LVL1) 89 { 90 if(collision(player, 377, 461, 308, 28, 32, 32))//First Platform (long) 91 { 92 handlePlatCollision(player, 377, 461, 308, 28, 32, 32, groundHeight, velY); 93 } 94 else if(collision(player, 74, 380, 196, 28, 32, 32))//Second Platform (short) 95 { 96 handlePlatCollision(player, 74, 380, 196, 28, 32, 32, groundHeight, velY); 97 } 98 else if(collision(player, 74, 260, 196, 28, 32, 32))//Third Platform (short) 99 { 100 handlePlatCollision(player, 74, 260, 196, 28, 32, 32, groundHeight, velY); 101 } 102 else if(collision(player, 312, 206, 252, 28, 32, 32))//Fourth Platform (Medium) 103 { 104 handlePlatCollision(player, 312, 206, 252, 28, 32, 32, groundHeight, velY); 105 } 106 else if(collision(player, 656, 155, 28, 28, 32, 32)) //Fifth Platform (first tiny) 107 { 108 handlePlatCollision(player, 655, 155, 28, 28, 32, 32, groundHeight, velY); 109 } 110 else if(collision(player, 584, 63, 28, 28, 32, 32))//Sixth Platform (second tiny) 111 { 112 handlePlatCollision(player, 584, 63, 28, 28, 32, 32, groundHeight, velY); 113 } 114 else if(collision(player, 751, 18, 28, 28, 32, 32))//Seventh Platform (third tiny) 115 { 116 handlePlatCollision(player, 751, 18, 28, 28, 32, 32, groundHeight, velY); 117 } 118 else if(collision(player, 928, 63, 196, 28, 32, 32))//Eighth Platform (short) 119 { 120 handlePlatCollision(player, 928, 63, 196, 28, 32, 32, groundHeight, velY); 121 } 122 else if(collision(player, 928, 140, 196, 28, 32, 32))//Ninth platform (short) 123 { 124 handlePlatCollision(player, 928, 140, 200, 38, 32, 32, groundHeight, velY); 125 } 126 else if(collision(player, 1203, 110, 28, 28, 32, 32))//Tenth platform (fourth tiny) 127 { 128 handlePlatCollision(player, 1203, 110, 28, 28, 32, 32, groundHeight, velY); 129 } 130 else if(collision(player, 1158, 318, 112, 28, 32, 32))//11th Platform (first short medium) 131 { 132 handlePlatCollision(player, 1158, 318, 112, 28, 32, 32, groundHeight, velY); 133 } 134 else if(collision(player, 1047, 317, 28, 28, 32, 32))//12th Platform (fifth tiny) 135 { 136 handlePlatCollision(player, 1047, 317, 28, 28, 32, 32, groundHeight, velY); 137 } 138 else if(collision(player, 917, 317, 28, 28, 32, 32))//13th Platform (sixth tiny) 139 { 140 handlePlatCollision(player, 917, 317, 28, 28, 32, 32, groundHeight, velY); 141 } 142 else if(collision(player, 1295, 217, 252, 28, 32, 32))//14th Platform (medium) 143 { 144 handlePlatCollision(player, 1395, 217, 252, 28, 32, 32, groundHeight, velY); 145 } 146 else if(collision(player, 1478, 139, 28, 28, 32, 32))//15th platform (seventh tiny) 147 { 148 handlePlatCollision(player, 1478, 139, 28, 28, 32, 32, groundHeight, velY); 149 } 150 else if(collision(player, 1407, 64, 28, 28, 32, 32))//16th platform (eighth tiny) 151 { 152 handlePlatCollision(player, 1407, 64, 28, 28, 32, 32, groundHeight, velY); 153 } 154 else if(collision(player, 1578, 47, 616, 28, 32, 32))//17th platform (ex long) 155 { 156 handlePlatCollision(player, 1578, 47, 616, 28, 32, 32, groundHeight, velY); 157 } 158 else if(collision(player, 2372, 168, 28, 28, 32, 32))//18th platform (ninth tiny) 159 { 160 handlePlatCollision(player, 2372, 168, 28, 28, 32, 32, groundHeight, velY); 161 } 162 else if(collision(player, 2169, 321, 112, 28, 32, 32))//19th platform (second short medium) 163 { 164 handlePlatCollision(player, 2169, 321, 112, 28, 32, 32, groundHeight, velY); 165 } 166 else if(collision(player, 2389, 371, 28, 28, 32, 32))//20th platform (10th tiny) 167 { 168 handlePlatCollision(player, 2389, 371, 28, 28, 32, 32, groundHeight, velY); 169 } 170 else if(collision(player, 2498, 424, 28, 28, 32, 32))//21th platform (11th tiny) 171 { 172 handlePlatCollision(player, 2498, 424, 28, 28, 32, 32, groundHeight, velY); 173 } 174 else if(collision(player, 2618, 484, 28, 28, 32, 32))//22th platform (12th tiny) 175 { 176 handlePlatCollision(player, 2618, 484, 28, 28, 32, 32, groundHeight, velY); 177 } 178 else 179 { 180 groundHeight = 518; 181 } 182 183 for(int i = 0; i < NUM_COIN; i++) 184 { 185 if(collision(player, coin[i].x, coin[i].y, coin[i].width, coin[i].height, player.width, player.height)) 186 { 187 coin[i].live = false; 188 coin[i].x = -500; 189 coin[i].y = -500; 190 player.score += 5; 191 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 192 } 193 } 194 195 for(int i = 0; i < NUM_ARROW; i++) 196 { 197 for(int j = 0; j < NUM_ENEMY; j++) 198 { 199 if(collideArrow(player, enemy[j].x, enemy[j].y, enemy[j].width, enemy[j].height, 200 arrow[i].x, arrow[i].y, arrow[i].width, arrow[i].height, enemy[j].live, arrow[i].live)) 201 { 202 enemy[j].live = false; 203 enemy[j].x = -500; 204 enemy[j].y = -500; 205 arrow[i].live = false; 206 player.score += 10; 207 ++enemyKilled; 208 } 209 } 210 } 211 212 if(collision(player, key.x, key.y, key.width, key.height, player.width, player.height))//Collide with key 213 { 214 al_play_sample(keyCollection, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 215 key.x = -500; 216 key.y = -500; 217 key.live = false; 218 gotKey = true; 219 player.score += 25; 220 } 221 for(int i = 0; i < NUM_ENEMY; i++) 222 { 223 if(collision(player, enemy[i].x, enemy[i].y, enemy[i].width, enemy[i].height, player.width, player.height)) 224 { 225 player.x = 10; 226 player.y = 518; 227 --player.lives; 228 player.dir = RIGHT; 229 } 230 } 231 232 if(collision(player, 712, 535, 110, 18, player.width, player.height)) //Snake collision 233 { 234 player.x = 10; 235 player.y = 535; 236 --player.lives; 237 al_play_sample(snakeSound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 238 } 239 if(collision(player, 1618, 518, 1017, 58, player.width, player.height))//Void thing colllision 240 { 241 player.x = 10; 242 player.y = 518; 243 --player.lives; 244 } 245 if(collision(player, 2200, 330, 32, 32, player.width, player.height)) //High jump thing 246 { 247 jumpSpeed = 25.0; 248 } 249 else 250 jumpSpeed = 15.0; 251 if(player.x <= -10)//Collision with left side of screen 252 { 253 player.x += player.speed; 254 active = false; 255 } 256 if(player.x >= 2980 && enemyKilled != 10 && coinCount != 11 && gotKey == false)/*If player hasn't killed all enemies, 257 got all coins and not got the key. Cannot to proceed to next level*/ 258 { 259 player.x -= player.speed; 260 } 261 else if(player.x >= 2980 && enemyKilled == 11 && coinCount == 11 && gotKey) 262 { 263 changeLevel(level, LVL2);//Change level to level 2 264 } 265 } 266 ////////////////////////////////////////////////////////////////////////////// 267 // THE LIVE CHANGES BACK TO 3 WHEN CHANGING TO LEVEL 2 - FIX THAT!! // 268 ////////////////////////////////////////////////////////////////////////////// 269 else if(level == LVL2 && level != LV2P2) 270 { 271 text.animateText(); 272 if(collision(player, 84, 340, 196, 28, player.width, player.height)) 273 { 274 handlePlatCollision(player, 84, 340, 196, 28, player.width, player.height, groundHeight, velY); 275 } 276 else if(collision(player, 458, 235, 196, 28, player.width, player.height)) 277 { 278 handlePlatCollision(player, 458, 235, 196, 28, player.width, player.height, groundHeight, velY); 279 } 280 else if(collision(player, 70, 42, 112, 28, player.width, player.height)) 281 { 282 handlePlatCollision(player, 70, 42, 112, 28, player.width, player.height, groundHeight, velY); 283 } 284 else 285 groundHeight = 518; 286 287 if(collision(player, 675, 518, 315, 50, player.width, player.height)) 288 { 289 player.x = 10; 290 player.y = 518; 291 --player.lives; 292 } 293 else if(collision(player, 1486, 518, 524, 50, player.width, player.height)) 294 { 295 player.x = 10; 296 player.y = 518; 297 --player.lives; 298 } 299 else if(collision(player, 2570, 518, 253, 50, player.width, player.height)) 300 { 301 player.x = 10; 302 player.y = 518; 303 --player.lives; 304 } 305 306 if(collision(player, teleport.x, teleport.y, teleport.width, teleport.height, player.width, player.height)) 307 { 308 player.x = 1020; 309 player.y = 518; 310 } 311 }

#SelectExpand
1else if(level == LV2P2 && level != LVL2) 2 { 3 if(collision(player, 272, 520, 400, 49, player.width, player.height))//First void 4 { 5 player.x = 10; 6 player.y = 518; 7 --player.lives; 8 } 9 else if(collision(player, 989, 518, 493, 49, player.width, player.height))//Second Void 10 { 11 player.x = 10; 12 player.y = 518; 13 --player.lives; 14 } 15 else if(collision(player, 2008, 518, 554, 49, player.width, player.height))//Third Void 16 { 17 player.x = 10; 18 player.y = 518; 19 --player.lives; 20 } 21 if(collision(player, 69, 438, 112, 28, 32, 32))//First platform short medium 22 { 23 handlePlatCollision(player, 69, 438, 112, 28, 32, 32, groundHeight, velY); 24 } 25 else if(collision(player, 340, 298, 28, 28, 32, 32))//Second platform tiny 26 { 27 handlePlatCollision(player, 340, 298, 28, 28, 32, 32, groundHeight, velY); 28 } 29 else if(collision(player, 458, 235, 196, 28, player.width, player.height))//third platform short medium 30 { 31 handlePlatCollision(player, 458, 235, 196, 28, player.width, player.height, groundHeight, velY); 32 } 33 else if(collision(player, 403, 147, 28, 28, player.width, player.height))//4th platform tiny 34 { 35 handlePlatCollision(player, 403, 147, 28, 28, player.width, player.height, groundHeight, velY); 36 } 37 else if(collision(player, 323, 87, 28, 28, player.height, player.width)) 38 { 39 handlePlatCollision(player, 323, 87, 28, 28, player.height, player.width, groundHeight, velY); 40 } 41 else 42 groundHeight = 518; 43 44 text.animateText(); 45 } 46 else if(level == LVL3) 47 {} 48 else if(level == LV3P2) 49 {} 50 else if(state == DEATH) 51 {} 52 if(player.lives <= 0) 53 { 54 changeState(state, DEATH); 55 } 56 if(draw && al_is_event_queue_empty(event_queue)) 57 { 58 draw = false; 59 if(state == MENU) 60 { 61 al_draw_bitmap(menu, 0, 0, 0); 62 /*for(int i = 0; i < NUM_ARROW; i++) 63 { 64 arrow[i].live = false; 65 }*/ 66 player.speed = 0; 67 fired = false; 68 } 69 else if(state == PLAYING && level == LVL1) 70 { 71 al_draw_bitmap(lvl1, 0, 0, 0); 72 player.drawPlayer(player.img); 73 drawKey(key, keyImg); 74 drawArrow(arrow, NUM_ARROW, arrow_r, arrow_l, player); 75 drawCoin(coin, coinImg2, NUM_COIN); 76 drawEnemy(enemy, enemyImg2, NUM_ENEMY); 77 al_identity_transform(&identity); 78 al_use_transform(&identity); 79 al_draw_textf(font, yellow, 10, 10, 0, "Coins: %i", coinCount); 80 al_draw_textf(font, yellow, 110, 10, 0, "Lives: %i", player.lives); 81 al_draw_textf(font, yellow, 10, 45, 0, "Killed: %i", enemyKilled); 82 al_draw_textf(font, yellow, 110, 45, 0, "Score: %i", player.score); 83 al_draw_textf(font, yellow, 210, 45, 0, "FPS: %i", gameFPS); 84 } 85 else if(state == DEATH) 86 { 87 al_draw_bitmap(death, 0, 0, 0); 88 al_draw_textf(score, yellow, 280, 320, 0, "SCORE: %i", player.score); 89 player.speed = 0; 90 enemyKilled = 0; 91 coinCount = 0; 92 fired = false; 93 } 94 else if(state == PLAYING && level == LVL2) 95 { 96 if(player.x >= 2980) 97 { 98 player.x = 10; 99 player.y = 518; 100 } 101 coinCount = 0; 102 enemyKilled = 0; 103 player.lives = 3; 104 al_draw_bitmap(lvl2, 0, 0, 0); 105 player.drawPlayer(player.img); 106 drawTeleport(teleport, teleImg); 107 drawText(text, yellow); 108 drawArrow(arrow, NUM_ARROW, arrow_r, arrow_l, player); 109 al_identity_transform(&identity); 110 al_use_transform(&identity); 111 al_draw_textf(font, yellow, 10, 10, 0, "Coins: %i", coinCount); 112 al_draw_textf(font, yellow, 110, 10, 0, "Lives: %i", player.lives); 113 al_draw_textf(font, yellow, 10, 45, 0, "Killed: %i", enemyKilled); 114 } 115 else if(state == PLAYING && level == LV2P2) 116 { 117 al_draw_bitmap(lvl2P2, 0, 0, 0); 118 player.drawPlayer(player.img); 119 drawText(text, yellow); 120 drawArrow(arrow, NUM_ARROW, arrow_r, arrow_l, player); 121 al_identity_transform(&identity); 122 al_use_transform(&identity); 123 al_draw_textf(font, yellow, 10, 10, 0, "Coins: %i", coinCount); 124 al_draw_textf(font, yellow, 110, 10, 0, "Lives: %i", player.lives); 125 al_draw_textf(font, yellow, 10, 45, 0, "Killed: %i", enemyKilled); 126 } 127 128 al_flip_display(); 129 al_clear_to_color(al_map_rgb(255, 255, 255)); 130 } 131 } 132 133 al_destroy_bitmap(img); 134 al_destroy_bitmap(coinImg); 135 al_destroy_bitmap(lvl1); 136 al_destroy_bitmap(arrow_l); 137 al_destroy_bitmap(arrow_r); 138 al_destroy_bitmap(menu); 139 al_destroy_bitmap(death); 140 al_destroy_bitmap(keyImg); 141 al_destroy_bitmap(enemyImg); 142 al_destroy_bitmap(enemyImg2); 143 al_destroy_bitmap(icon); 144 al_destroy_sample(coinCollect); 145 al_destroy_sample(bowShot); 146 al_destroy_sample(bgSong); 147 al_destroy_sample(keyCollection); 148 al_destroy_sample(snakeSound); 149 al_destroy_sample_instance(songInstance); 150 al_destroy_font(font); 151 al_destroy_font(score); 152 al_destroy_display(display); 153 al_destroy_event_queue(event_queue); 154 al_destroy_timer(timer); 155 al_destroy_timer(frameTimer); 156 al_destroy_timer(enemyTimer); 157 return 0; 158} 159void initPlayer(Player &player) 160{ 161 //player.dir = LEFT; 162 player.x = 10; 163 player.y = 518; 164 player.width = 32; 165 player.height = 32; 166 player.lives = 3; 167 player.speed = 5; 168 player.score = 0; 169 player.live = false; 170 player.dir = RIGHT; 171 player.img = al_load_bitmap("Files/spritesheet(Bow & left + right).png"); 172} 173 174void initEnemy(Enemy enemy[], int speedR, int speedL, int speed, int array_size) 175{ 176 std::ifstream enemy_coords; 177 enemy_coords.open("Files/enemy coords.dat"); 178 179 std::ifstream enemy_positions; 180 enemy_positions.open("Files/enemy update position.dat"); 181 182 for(int i = 0; i < array_size; i++) 183 { 184 enemy[i].live = true; 185 enemy[i].speedR = speedR; 186 enemy[i].speedL = speedL; 187 enemy[i].speed = speed; 188 enemy_coords >> enemy[i].x; 189 enemy_coords >> enemy[i].y; 190 enemy_positions >> enemy[i].posX; 191 enemy_positions >> enemy[i].posX2; 192 enemy[i].dir = eLEFT; 193 enemy[i].eSourceX = 32; 194 enemy[i].eSourceY = 0; 195 enemy[i].acitve = true; 196 enemy[i].width = 29; 197 enemy[i].height = 29; 198 } 199 enemy_coords.close(); 200} 201void drawEnemy(Enemy enemy[], ALLEGRO_BITMAP *image, int array_size) 202{ 203 for(int i = 0; i < array_size; i++) 204 { 205 if(enemy[i].live) 206 { 207 al_draw_bitmap_region(image, enemy[i].eSourceX, enemy[i].eSourceY * al_get_bitmap_height(image) / 2, 32, 32, enemy[i].x, enemy[i].y, 0); 208 } 209 } 210 211} 212void enemyAnimation(Enemy enemy[], ALLEGRO_BITMAP *image, int array_size) 213{ 214 for(int i = 0; i < array_size; i++) 215 { 216 if(enemy[i].acitve) 217 { 218 enemy[i].eSourceX += al_get_bitmap_width(image) / 3; 219 } 220 else 221 { 222 enemy[i].eSourceX = 0; 223 } 224 if(enemy[i].eSourceX >= al_get_bitmap_width(image)) 225 { 226 enemy[i].eSourceX = 0; 227 } 228 enemy[i].eSourceY = enemy[i].dir; 229 } 230 231} 232void updateEnemy(Enemy enemy[], int array_size) 233{ 234 for(int i = 0; i < array_size; i++) 235 { 236 if(enemy[i].live) 237 { 238 enemy[i].x += enemy[i].speed; 239 if(enemy[i].x >= enemy[i].posX) 240 { 241 enemy[i].dir = eLEFT; 242 enemy[i].x = enemy[i].posX; 243 enemy[i].speed = enemy[i].speedL; 244 } 245 if(enemy[i].x <= enemy[i].posX2) 246 { 247 enemy[i].dir = eRIGHT; 248 enemy[i].x = enemy[i].posX2; 249 enemy[i].speed = enemy[i].speedR; 250 } 251 } 252 } 253} 254bool collideArrow(Player & player, int eX, int eY, int eWidth, int eHeight, int aX, int aY, int aWidth, int aHeight, bool eLive, bool aLive) 255{ 256 if(aLive) 257 { 258 if((aX > eX + eWidth) || (aY > eY + eHeight) || 259 (aX + aHeight < eX) || (aY + aHeight < eY) && (eLive) && (aLive)) 260 { 261 return false; 262 } 263 return true; 264 } 265 266} 267 268void initCoin(Coin coin[], int array_size) 269{ 270 std::ifstream coin_coords; 271 coin_coords.open("Files/coin coords.dat"); 272 273 for(int i = 0; i < array_size; i++) 274 { 275 coin[i].width = 16; 276 coin[i].height = 16; 277 coin[i].live = true; 278 coin_coords >> coin[i].x; 279 coin_coords >> coin[i].y; 280 coin[i].sourceX = 32; 281 coin[i].sourceY = 0; 282 coin[i].active = true; 283 } 284 285 coin_coords.close(); 286 287} 288void drawCoin(Coin coin[], ALLEGRO_BITMAP *image, int array_size) 289{ 290 for(int i = 0; i < array_size; i++) 291 { 292 if(coin[i].live) 293 { 294 if(image == NULL) 295 al_draw_filled_circle(coin[i].x - 10, coin[i].y - 10, 20, al_map_rgb(255,0,255)); 296 else 297 al_draw_bitmap_region(image, coin[i].sourceX, coin[i].sourceY, 32, 32, coin[i].x, coin[i].y, 0); 298 } 299 } 300 301} 302void coinAnimation(Coin coin[], ALLEGRO_BITMAP *image, int array_size) 303{ 304 for(int i = 0; i < array_size; i++) 305 { 306 if(coin[i].active) 307 { 308 coin[i].sourceX += al_get_bitmap_width(image) / 7; 309 } 310 else 311 { 312 coin[i].sourceX = 0; 313 } 314 if(coin[i].sourceX >= al_get_bitmap_width(image)) 315 { 316 coin[i].sourceX = 0; 317 } 318 } 319 320 321} 322void coinCollision(Coin coin[], Player &player, int coinCount, int cSize, int pWidth, int pHeight, ALLEGRO_SAMPLE *sample) 323{ 324 for(int i = 0; i < cSize; i++) 325 { 326 if((player.x > coin[i].x + coin[i].width - 10) || (player.y > coin[i].y + coin[i].height - 10) || 327 (coin[i].x > player.x + pWidth - 10) || (coin[i].y > player.y + pHeight - 10)) 328 { 329 coin[i].live = false; 330 coin[i].x = -500; 331 coin[i].y = -500; 332 al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 333 } 334 335 } 336} 337 338void initArrow(Arrow arrow[], int array_size) 339{ 340 for(int i = 0; i < array_size; i++) 341 { 342 arrow[i].speed = 5.0; 343 arrow[i].live = false; 344 arrow[i].width = 16; 345 arrow[i].height = 5; 346 arrow[i].fired = false; 347 arrow[i].aDir = aRIGHT; 348 } 349 350} 351void drawArrow(Arrow arrow[], int array_size, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2, Player &player) 352{ 353 for(int i = 0; i < array_size; i++) 354 { 355 if(arrow[i].live && arrow[i].aDir == aRIGHT && arrow[i].fired) 356 { 357 al_draw_bitmap(image, arrow[i].x, arrow[i].y, 0); 358 } 359 if(arrow[i].live && arrow[i].aDir == aLEFT && arrow[i].fired) 360 { 361 al_draw_bitmap(image2, arrow[i].x, arrow[i].y, 0); 362 } 363 } 364} 365void fireArrow(Arrow arrow[], int array_size, Player &player, ALLEGRO_SAMPLE *bowShot) 366{ 367 for(int i = 0; i < array_size; i++) 368 { 369 if(!arrow[i].live && !fired) 370 { 371 arrow[i].live = true; 372 arrow[i].x = player.x + 17; 373 arrow[i].y = player.y + 22; 374 arrow[i].fired = true; 375 fired = true; 376 arrow[i].aDir = player.dir; 377 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 378 } 379 } 380} 381void updateArrow(Arrow arrow[], int array_size, Player &player) 382{ 383 for(int i = 0; i < array_size; i++) 384 { 385 if(arrow[i].live && arrow[i].fired) 386 { 387 if(arrow[i].aDir == aRIGHT) 388 { 389 arrow[i].x += arrow[i].speed; 390 if(arrow[i].x >= 2990) 391 { 392 arrow[i].live = false; 393 } 394 } 395 if(arrow[i].aDir == aLEFT) 396 { 397 arrow[i].x -= arrow[i].speed; 398 if(arrow[i].x <= 5) 399 { 400 arrow[i].live = false; 401 } 402 } 403 } 404 } 405} 406 407void initKey(Key &key) 408{ 409 key.x = 979; 410 key.y = 151; 411 key.width = 32; 412 key.height = 32; 413 key.live = true; 414} 415void drawKey(Key &key, ALLEGRO_BITMAP *image) 416{ 417 if(key.live) 418 { 419 al_draw_bitmap(image, 979, 151, 0); 420 } 421} 422 423void initText(Text &text) 424{ 425 text.x = 1540; 426 text.y = 400; 427 text.speed = 2; 428 text.live = true; 429 text.text = al_load_font("Files/JUNGBN__.TTF", 23, 0); 430} 431void drawText(Text &text, ALLEGRO_COLOR yellow) 432{ 433 al_draw_text(text.text, al_map_rgb(0,0,0), text.x, text.y, 0, "Enter & Backspace :D"); 434} 435 436void initTeleport(Teleport &teleport) 437{ 438 teleport.x = 65; 439 teleport.y = 42; 440 teleport.width = 32; 441 teleport.height = 32; 442 teleport.sourceX = 48; 443 teleport.sourceY = 0; 444 teleport.active = true; 445} 446void teleportAnimation(Teleport &teleport, ALLEGRO_BITMAP *image) 447{ 448 if(teleport.active) 449 { 450 teleport.sourceX += al_get_bitmap_width(image) / 10; 451 } 452 else 453 teleport.sourceX = 48; 454 if(teleport.sourceX >= al_get_bitmap_width(image)) 455 { 456 teleport.sourceX = 0; 457 } 458} 459void drawTeleport(Teleport &teleport, ALLEGRO_BITMAP *image) 460{ 461 if(teleport.active) 462 { 463 al_draw_bitmap_region(image, teleport.sourceX, teleport.sourceY * al_get_bitmap_height(image), 48, 48, teleport.x, teleport.y, 0); 464 } 465} 466 467bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight) 468{ 469 if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) || 470 (ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10)) 471 { 472 return false; 473 } 474 return true; 475} 476void handlePlatCollision(Player &player, int ex, int ey, int ewidth, int eheight, int pwidth, int pheight, int &groundheight, float vely) 477{ 478 if(player.y > ey) 479 { 480 player.y += vely; 481 groundheight = ey; 482 player.y = groundheight; 483 } 484} 485void handleArrowCollision(Enemy enemy[], Arrow arrow[], Player &player, int aSize, int eSize, int enemyKilled) 486{ 487 for(int i = 0; i < aSize; i++) 488 { 489 for(int j = 0; j < eSize; j++) 490 { 491 enemy[j].live = false; 492 enemy[j].x = -500; 493 enemy[j].y = -500; 494 arrow[i].live = false; 495 ++player.score; 496 ++enemyKilled; 497 } 498 } 499} 500void handleCoinCollision(Player &player, Coin coin[], ALLEGRO_SAMPLE *coincollect, int array_size) 501{ 502 for(int i = 0; i < array_size; i++) 503 { 504 coin[i].live = false; 505 coin[i].x = -500; 506 coin[i].y = -500; 507 al_play_sample(coincollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 508 } 509 510 511} 512void handleEnemyCollision(Player &player, Enemy &enemy, Arrow arrow[], bool enemyCollision) 513{ 514 player.x = 10; 515 player.y = 535; 516 --player.lives; 517 for(int i = 0; i < NUM_ARROW; i++) 518 { 519 if(arrow[i].live) 520 { 521 arrow[i].live = false; 522 } 523 } 524 player.dir = RIGHT; 525} 526 527void cameraUpdate(float *cameraPosition, Player &player, float width, float height) 528{ 529 cameraPosition[0] = -(ScreenWidth / 2) + (player.x + width / 2); 530 cameraPosition[1] = -(ScreenHeight) + (player.y + height / 2); 531 532 if(cameraPosition[0] < 0) 533 cameraPosition[0] = 0; 534 if(cameraPosition[1] < 0) 535 cameraPosition[1] = 0; 536 if(cameraPosition[0] > 3000 - ScreenWidth) 537 cameraPosition[0] = 3000 - ScreenWidth; 538 539} 540 541void changeState(int &state, int newState) 542{ 543 if(state == MENU) 544 {} 545 if(state == PLAYING) 546 {} 547 if(state == DEATH) 548 {} 549 550 state = newState; 551 552 if(state == MENU) 553 {} 554 if(state == PLAYING) 555 { 556 initPlayer(player); 557 initEnemy(enemy, 3, -3, 3, NUM_ENEMY); 558 initArrow(arrow, NUM_ARROW); 559 initCoin(coin, NUM_COIN); 560 initKey(key); 561 initText(text); 562 initTeleport(teleport); 563 564 } 565 if(state == DEATH) 566 {} 567} 568void changeLevel(int &level, int newLevel) 569{ 570 if(level == LVL1) 571 {} 572 if(level == LVL2) 573 {} 574 if(level == LV2P2) 575 {} 576 if(level == LVL3) 577 {} 578 if(level == LV3P2) 579 {} 580 581 level = newLevel; 582 583 if(level == LVL1) 584 {} 585 if(level == LVL2) 586 { 587 588 } 589 if(level == LV2P2) 590 {} 591 if(level == LVL3) 592 { 593 player.x = 10; 594 player.y = 518; 595 player.lives = 3; 596 } 597 if(level == LV3P2) 598 {} 599} 600void changeLevelPart(int &lvPart, int newLvPart) 601{ 602 if(lvPart == LV2P2) 603 {} 604 if(lvPart == LV3P2) 605 {} 606 607 lvPart = newLvPart; 608 609 if(lvPart == LV2P2) 610 {} 611 if(lvPart == LV3P2) 612 {} 613 614}

Thomas Fjellstrom

I mean when you define them. Say in a function where you put in a new variable: int foo; you should probably initialize it. Same with C++ constructors, make sure you always give class members a valid value.

I notice you may not be initializing your player/coin/enemy/arrow/platform arrays.

DJLad16

Ok (I don't need the platform arrays anymore fyi), but what do I declare them as?

Thomas Fjellstrom

Well its fine the way they are declared other than being globals (que bambams tirade).

In your init functions, just make sure you're initializing all members of your structs. You may be using one of your objects/items without fully clearing it. Like maybe reusing one of them without clearing state?

Thread #612314. Printed from Allegro.cc