2013-04-10 137 views
0

所以我對這個站點有點新,以及用C++和allegro進行編程,但我一直有這個問題我正在製作這款遊戲​​,而我似乎無法弄清楚。它似乎不是一個數組溢出(這是我原先想的),所以如果有人知道問題是什麼,我會很高興。運行時檢查失敗#2 - 變量'e_color'周圍的堆棧已損壞

代碼很長,對不起。我真的不知道我在做什麼。我遺漏了一些東西,所以希望這不會影響任何東西。

在此先感謝。

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; 
    } 
} 

回答

0

首先,如果你初始化9個槽數組,你只能得到在範圍0-8(總共9)和他們一起玩

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; 
    } 
} 

你忽略了50%代碼的重要部分,我認爲你到處都犯了這個錯誤,但是你可以搜索你所有的代碼並且很容易地解決這個問題。

相關問題