0

所以我对这个站点以及使用 c++ 和 allegro 编程有点陌生,但是我在制作的这个游戏中一直遇到这个问题,我似乎无法弄清楚。它似乎不是数组溢出(这是我最初的想法),所以如果有人知道问题是什么,我会很激动。

代码很长,对不起。我真的不知道我在做什么。我遗漏了一些位,所以希望这不会影响任何事情。

提前致谢。

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

1 回答 1

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% 的代码重要部分,我认为您在任何地方都犯了这个错误,但您可以搜索所有代码并轻松解决问题。

于 2013-08-25T09:42:57.207 回答