[英]Allegro5 Problems with deleting a text and display a new one after pressing a button
所以我寫了這段代碼,如果我按下回車,它應該清除頁面,然后輸入一個新的文本,上面寫着“游戲開始”。 但它根本不會運行,只是保持不變,有人知道如何解決這個問題嗎?
PS。 額外的問題,如何在替換文本 5 秒后創建延遲然后再次清除文本?
感謝!
#include <iostream>
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("YARDSALE.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
bool done = false;
while (!done) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_UP)
{
switch (event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display;
break;
}
}
}
al_destroy_font(font);
al_destroy_display(display);
}
當您調用“al_flip_display”時,您只是錯過了“()”。 它應該是“al_flip_display();”。 但即使這樣,它也不會按預期工作,因為“游戲開始”只會出現片刻。 我建議添加游戲狀態。 而且我認為在主要 function 結束時,您還應該銷毀事件隊列。
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
enum class GAME_STATE { CLICK, INTRO, MAIN };
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("font_code_pro.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
ALLEGRO_TIMER* timer = al_create_timer(1 / 60.0);
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_timer_event_source(timer));
bool done = false;
bool draw = false;
unsigned delay = 0;
GAME_STATE state = GAME_STATE::CLICK;
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if(event.type == ALLEGRO_EVENT_TIMER)
{
draw = true;
if(state == GAME_STATE::INTRO && !((++delay) % (60 * 5)))state = GAME_STATE::MAIN;
}
if(event.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
if(state == GAME_STATE::CLICK)
state = GAME_STATE::INTRO;
break;
}
}
if(draw)
{
draw = false;
switch(state)
{
case GAME_STATE::CLICK:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
break;
case GAME_STATE::INTRO:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display();
break;
case GAME_STATE::MAIN:
al_clear_to_color(al_map_rgb(255, 255, 255));
//
al_flip_display();
break;
default:
break;
}
}
}
al_destroy_font(font);
al_destroy_timer(timer);
al_destroy_event_queue(queue);
al_destroy_display(display);
}
我希望它有所幫助::)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.