2013-04-10 2 views
0

alors je suis un peu nouveau sur ce site, ainsi que pour la programmation avec C++ et allegro, mais j'ai eu ce problème en ce jeu que je fais, et je n'arrive pas à le comprendre. Cela ne semble pas être un débordement de tableau (ce que je pensais à l'origine), donc si quelqu'un sait quel est le problème, je serais ravi.Échec de l'exécution # 2 - La pile autour de la variable 'e_color' a été corrompue

Le code est long, je suis désolé. Je ne sais pas vraiment ce que je fais. J'ai laissé de côté, donc j'espère que cela n'affecte rien.

Merci d'avance.

void main() { 
    InitGraphics(); // see function above for Allegro initialization 
    srand(time(NULL)); 

    int x = 20, y = 20, e_color[9], color = 0x0000FF00; 
    int x_enemy[9], y_enemy[9]; 
    bool over = false, hit[9]; 
    BITMAP* buffer = create_bitmap(screen->w,screen->h); 

    initialization(x_enemy, y_enemy, x, y, e_color, hit, color, buffer); // begins the game, inculding initializing player and enemies 

    game_play(x_enemy, y_enemy, x, y, e_color, hit, color, over, buffer); // plays the game 

    end_message(x_enemy, y_enemy, x, y, e_color, hit, color, over, buffer); // ends the game 

    rest(750); 

} 
END_OF_MAIN() 

// the introduction and initialization of the game 
void initialization(int x_enemy[9], int y_enemy[9], int x, int y, int e_color[9], bool hit[9], int color, BITMAP* buffer) { 

    intro(buffer); 

    initialize_enemy(x_enemy, y_enemy, e_color, hit); 
    circlefill(buffer,x,y,10,color); // the player 
    draw_enemy(x_enemy, y_enemy, e_color, buffer); 

    countdown(buffer); 
} 

// calls the functions and runs the loop needed to actually play the game 
void game_play(int x_enemy[9], int y_enemy[9], int x, int y, int e_color[9], bool hit[9], int color, bool over, BITMAP* buffer) { 

    while(key[KEY_ESC] == 0 && over == false) { 

     enemy_movement(x_enemy, y_enemy, e_color, buffer); 
     rest(5); 
     draw_enemy(x_enemy, y_enemy, e_color, buffer); 
     test_contact(x_enemy, y_enemy, x, y, e_color, hit, buffer); 
     over = test_end(hit); 
     if (over == true) 
      the_end(e_color, buffer); 

     ///////// PLAYER MOVEMENT //////////// 

     circlefill(buffer,x,y,10,color); // the player 

     if (key[KEY_UP]) { 
      circlefill(buffer,x,y,10,0); 
      if (y < (0+10)) 
       y = y; 
      else 
       y = y-2; 
      circlefill(buffer,x,y,10,color); 
     } 
     if (key[KEY_DOWN]) { 
      circlefill(buffer,x,y,10,0); 
      if (y > (480-10)) 
       y = y; 
      else 
       y = y+2; 
      circlefill(buffer,x,y,10,color); 
     } 
     if (key[KEY_LEFT]) { 
      circlefill(buffer,x,y,10,0); 
      if (x < (0+10)) 
       x = x; 
      else 
       x = x-2; 
      circlefill(buffer,x,y,10,color); 
     } 
     if (key[KEY_RIGHT]) { 
      circlefill(buffer,x,y,10,0); 
      if (x > (640-10)) 
       x = x; 
      else 
       x = x+2; 
      circlefill(buffer,x,y,10,color); 
     } 
     /////// END PLAYER MOVEMENT ////////// 

     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    } 
} 

// a message to the user saying game over and thank you for playing, includes a re-play option 

void end_message(int x_enemy[9], int y_enemy[9], int x, int y, int e_color[9], bool hit[9], int color, bool over, BITMAP* buffer) { 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "You Won!"); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1500); 
    clear(buffer); 
    textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+15), 0x00FFFFFF, "Thank you for playing the Game."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+30), 0x00FFFFFF, "To play one more time, press 'n'."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+45), 0x00FFFFFF, "To quit, press any other key."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    clear_keybuf(); 
    char answer = readkey(); 
    clear(buffer); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 

    if (answer == 110) 
    { 
     textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "Starting new game."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(1000); 
     clear(buffer); 
     initialization(x_enemy, y_enemy, x, y, e_color, hit, color, buffer); 
     game_play(x_enemy, y_enemy, x, y, e_color, hit, color, over, buffer); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "You Won!"); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(1500); 
     clear(buffer); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+15), 0x00FFFFFF, "Thank you for playing the Game again."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+30), 0x00FFFFFF, "Press any key to quit."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     clear_keybuf(); 
     readkey(); 
     clear(buffer); 
    } 
} 

// text introduction message 
void intro(BITMAP* buffer = create_bitmap(screen->w,screen->h)) { 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "Welcome to the Game."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(500); 
    textprintf(buffer, font, ((screen->w)-225), ((screen->h)-15), 0x00FFFFFF, "Press any key to continue."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    readkey(); 
    clear(buffer); 
    /// 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "Press 'r' for rules or any other key to continue to game."); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    char ans = readkey(); 
    if (ans == 114) 
    { 
     // lots and lots of text, yay 
     clear(buffer); 
     textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "Rules"); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+15), 0x00FFFFFF, "Move your player using the arrow keys."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+30), 0x00FFFFFF, "Catch all the red enemies to turn them blue."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+45), 0x00FFFFFF, "Once all the enemies have been frozen, you win!"); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf_centre(buffer, font, screen->w/2, ((screen->h/2)+60), 0x00FFFFFF, "Press ESCAPE at any time to quit game."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     rest(750); 
     textprintf(buffer, font, ((screen->w)-225), ((screen->h)-15), 0x00FFFFFF, "Press any key to continue."); 
     blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
     readkey(); 
     clear(buffer); 
    } 
    clear(buffer); 
} 

// counts down from three 
void countdown(BITMAP* buffer = create_bitmap(screen->w,screen->h)) { 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "3"); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0, "3"); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "2"); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0, "2"); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "1"); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0, "1"); 
    textprintf_centre(buffer, font, screen->w/2, screen->h/2, 0x00FFFFFF, "Go"); 
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 
    rest(1000); 
    clear(buffer); 
} 

// the enemies intial spot 
void initialize_enemy(int x[9], int y[9], int color[9], bool hit[9]) { 

    //seed the random number generator 
    srand((unsigned)time(0)); 

    for(int a = 0 ; a < 10 ; a++) 
    { 
     hit[a] = false; 
     x[a] = rand()%620; 
     y[a] = rand()%460; 
     color[a] = 0x00FF0000; 
    } 
} 

Répondre

0

Tout d'abord, si vous initialisez un tableau avec 9 emplacements, vous obtenez seulement jouer avec eux dans la gamme 0-8 (9 au total)

void initialize_enemy(int x[9], int y[9], int color[9], bool hit[9]) { 

//seed the random number generator 
srand((unsigned)time(0)); 

    for(int a = 0 ; a < 9 ; a++) // The array size of every variable is not big enough 
    {       // So I changed it accordingly to 9 from 10 
     hit[a] = false; 
     x[a] = rand()%620; 
     y[a] = rand()%460; 
     color[a] = 0x00FF0000; 
    } 
} 

Vous avez laissé à 50% de la parties importantes du code et je pense que vous avez fait cette erreur partout, mais vous pouvez rechercher tout votre code et résoudre le problème facilement.

Questions connexes