想要一些文字在游戏中消除自己的自我

Want some text to erase its self in game

本文关键字:自己的 自我 游戏 文字      更新时间:2023-10-16

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

因此,我希望文本"窗口将在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 <allegro5allegro.h>
#include <allegro5allegro_font.h>
#include <allegro5allegro_ttf.h>
#include <allegro5allegro_native_dialog.h>
#include <allegro5allegro_audio.h>
#include <allegro5allegro_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;
}

将背景清除和前三个文本输出移动到主循环中。您需要在每个帧上重新绘制所有内容。

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

#include <allegro5allegro.h>
#include <allegro5allegro_font.h>
#include <allegro5allegro_ttf.h>
#include <allegro5allegro_native_dialog.h>
#include <allegro5allegro_audio.h>
#include <allegro5allegro_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;
}

yo我明白了。感谢您在正确的问题中的观点。