希望在游戏中删除一些文本

时间:2014-01-25 05:04:45

标签: c++ 2d-games allegro5

好的,所以只是想知道一个解决这个问题的技巧。

所以我希望文本“窗口将在10秒内关闭”,以便在每次循环时擦除并替换为倒计时的下一个数字。但我现在得到的只是重叠。

我只是想让它倒计时并显示它。

//FILE: Main.cpp
//PROGR: Hank Bates
//PURPOSE: To display text on screen for 10 seconds. 
//EXTRA ADD ON FILES: Slendermanswriting.ttf
//                    PrometheusSiren.wav


#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>


int main(void)
{
    //summon the fonts and stuff
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_FONT *font50;
    ALLEGRO_FONT *font36;
    ALLEGRO_FONT *font18;
    ALLEGRO_SAMPLE *song;
    int a = 100;


    if (!al_init())
    {
        al_show_native_message_box(NULL, NULL, NULL,
            "failed to initialize allegro!", NULL, NULL);
        return -1;
    }

    //set up some of the display settings
    al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
    display = al_create_display(640, 480);
    al_set_window_title(display, "A bad horror game");

    if (!display)
    {
        al_show_native_message_box(NULL, NULL, NULL,
            "failed to initialize display!", NULL, NULL);
        return -1;
    }

    al_init_font_addon();
    al_init_ttf_addon();

    //Install Slender Man font here
    font50 = al_load_font("Slendermanswriting.ttf", 50, 0);
    font36 = al_load_font("Slendermanswriting.ttf", 36, 0);
    font18 = al_load_font("Slendermanswriting.ttf", 18, 0);

    //set up music here
    al_install_audio();
    al_init_acodec_addon();
    al_reserve_samples(1);

    song = al_load_sample("PrometheusSiren.wav");

    //play song this will loop around and around like a record man!
    al_play_sample(song, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);

    int screen_w = al_get_display_width(display);
    int screen_h = al_get_display_height(display);

    al_clear_to_color(al_map_rgb(0, 0, 0));

    al_draw_text(font50, al_map_rgb(255, 0, 0), 12, 50, 0, "SLENDER MAN IS COMING");
    al_draw_text(font36, al_map_rgb(255, 5, 10), 200, 100, 0,  "RUN AND HIDE");
    al_draw_text(font18, al_map_rgb(100, 15, 18), 150, 150, 0,  "ENJOY THE PROMETHEUS SIREN MUSIC");

    int timer = 10;
while (timer != 0)
    {


al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 3, 300, ALLEGRO_ALIGN_CENTRE,
        "TURN UP YOUR VOLUME TO %i PRECENT!", a);

    al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, 400, ALLEGRO_ALIGN_CENTRE,
        "WINDOW WILL CLOSE IN %i seconds!", timer);

    al_flip_display();


        al_rest(1.0);
        timer = timer - 1;
    }



    al_rest(10.0);

    //destroy stuff
    al_destroy_font(font18);
    al_destroy_font(font50);
    al_destroy_font(font36);
    al_destroy_display(display);
    al_destroy_sample(song); 
    //pew pew pew, bang.... all destoryed :)

    return 0;
}

2 个答案:

答案 0 :(得分:1)

将背景清除并将前三个文本输出移动到主循环中。你需要在每一帧上重绘一切。

答案 1 :(得分:0)

//FILE: Main.cpp
//PROGR: Hank Bates
//PURPOSE: To display text on screen for 10 seconds. 
//EXTRA ADD ON FILES: Slendermanswriting.ttf
//                    PrometheusSiren.wav


#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>




int main(void)
{
    //summon the fonts and stuff
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_FONT *font50;
    ALLEGRO_FONT *font36;
    ALLEGRO_FONT *font18;
    ALLEGRO_SAMPLE *song;
    int a = 100;
    int time_left = 10;

    //test redaw
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    bool redraw = true;

    if (!al_init())
    {
        al_show_native_message_box(NULL, NULL, NULL,
            "failed to initialize allegro!", NULL, NULL);
        return -1;
    }

    //set up some of the display settings
    al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
    display = al_create_display(640, 480);
    al_set_window_title(display, "A bad horror game");

    if (!display)
    {
        al_show_native_message_box(NULL, NULL, NULL,
            "failed to initialize display!", NULL, NULL);
        return -1;
    }

    al_init_font_addon();
    al_init_ttf_addon();

    //Install Slender Man font here
    font50 = al_load_font("Slendermanswriting.ttf", 50, 0);
    font36 = al_load_font("Slendermanswriting.ttf", 36, 0);
    font18 = al_load_font("Slendermanswriting.ttf", 18, 0);

    //set up music here
    al_install_audio();
    al_init_acodec_addon();
    al_reserve_samples(1);

    //upload the song here
    song = al_load_sample("PrometheusSiren.wav");

    //play song this will loop around and around like a record man!
    al_play_sample(song, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);

    int screen_w = al_get_display_width(display);
    int screen_h = al_get_display_height(display);

    al_clear_to_color(al_map_rgb(0, 0, 0));

    while (time_left != 0)
    {
        al_flip_display();
        al_clear_to_color(al_map_rgb(0, 0, 0));

        al_draw_text(font50, al_map_rgb(255, 0, 0), 12, 50, 0, "SLENDER MAN IS COMING");
        al_draw_text(font36, al_map_rgb(255, 5, 10), 200, 100, 0,  "RUN AND HIDE");
        al_draw_text(font18, al_map_rgb(100, 15, 18), 150, 150, 0,  "ENJOY THE PROMETHEUS SIREN MUSIC");

        al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 3, 300, ALLEGRO_ALIGN_CENTRE,
        "TURN UP YOUR VOLUME TO %i PRECENT!", a);

        al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, 400, ALLEGRO_ALIGN_CENTRE,
            "WINDOW WILL CLOSE IN %i seconds!", time_left);

        al_rest(1.0);
        time_left = time_left - 1;
    }

    al_flip_display();
    al_rest(0.0);

    //destroy stuff
    al_destroy_font(font18);
    al_destroy_font(font50);
    al_destroy_font(font36);
    al_destroy_display(display);
    al_destroy_sample(song); 
    al_destroy_timer(timer);
    //pew pew pew, bang.... all destoryed :)

    return 0;
}
哟我明白了。感谢正确问题中的要点。