我有这样的问题:我想给你发个“你好!”在游戏开始前,靠近快板屏幕的中央。不知道为什么总是只有全窗口白色的颜色后,我编写的程序,而不是消息“你好!”我不知道为什么它不显示“你好!”但是如果我删除注释行//*之间的代码,程序就会显示消息"Hello!“井。有人能告诉我怎么解决这个问题吗?
#include<allegro5\allegro5.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_font.h>
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_primitives.h>
#include<iostream>
using namespace std;
int main(){
int ScreenWidth = 800, ScreenHeight = 600;
if (!al_init())
{
al_show_native_message_box(NULL, NULL, NULL, "Could not initialize Allegro 5", NULL, NULL);
return -1;
}
al_set_new_display_flags(ALLEGRO_WINDOWED);
ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);//creating window with dimensions: 800:600 px
al_set_window_position(display, 200, 100);//place from left top positioning the frame of display
al_set_window_title(display, "Try to catch me!");
if (!display)//show this message if sth wrong with display
{
al_show_native_message_box(display, "Sample Title", "Display Settings", "Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
al_init_font_addon();//initialization font addon
al_init_ttf_addon();//initialization ttf(true type font) addon
//INTRO
ALLEGRO_FONT *fontOrbionBlack36 = al_load_font("fonts/Orbitron Black.ttf", 36, NULL);
al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
al_rest(5.0);
//********************************************************************************
al_init_primitives_addon();
al_install_keyboard();
ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
bool done = false;
int x = 10, y = 10;
int moveSpeed = 5;
while (!done)
{
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);
if (events.type = ALLEGRO_EVENT_KEY_DOWN)
{
switch (events.keyboard.keycode)
{
case ALLEGRO_KEY_DOWN:
y += moveSpeed;
break;
case ALLEGRO_KEY_UP:
y -= moveSpeed;
break;
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
}
}
al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
//*****************************************************************************************
al_flip_display();
al_rest(10.0);//rest is very simmilar to Sleep() it is waitg function, waiting 3s then close the allegro display
//al_destroy_font(fontOrbionBlack18);
al_destroy_font(fontOrbionBlack36);
al_destroy_display(display);//destroy the display at the end of the programm
return 0;
}发布于 2014-12-07 23:04:49
这里有几个问题妨碍你看课文。
al_wait_for_event将无限期地停止,您的程序将永远无法到达对al_flip_display的调用。您可以阅读更多关于计时器这里的内容,但暂时您必须按下键盘键才能使该程序取得进展。在主循环中尝试这样的操作:
al_clear_to_color(al_map_rgb(0, 0, 0));`
al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
al_flip_display();换句话说,先弄清楚,然后画,然后翻转。当前您只绘制文本一次(在主循环之前)。这意味着,清除屏幕将清除文本--您需要在清除屏幕之后绘制文本的每一个帧,而在翻转显示之前绘制文本。
如果你改变了这两件事,你应该能够看到文本后,按下键盘按钮。然而,还有一些其他的修改可能会有所帮助:
al_rest的调用,除非它们具有有用的用途。目前,它们让您在启动主循环之前等待5秒(这意味着即使您按下一个键,也无法立即看到文本)。events.type == ALLEGRO_EVENT_KEY_DOWN而不是events.type = ALLEGRO_EVENT_KEY_DOWN。目前,您实际上是将值ALLEGRO_EVENT_KEY_DOWN分配给events.type,覆盖它的原始值。您的编译器可能(而且可能应该)已经警告过您了。https://stackoverflow.com/questions/27347883
复制相似问题