我正在使用 allegro_primitives.h 头文件,当我用 al_draw_filled_rectangle 绘制一个矩形并用键移动矩形时,矩形的轮廓在矩形前进的方向上正在改变颜色。这是代码:
#include <allegro5\allegro5.h>
#include <allegro5\allegro_primitives.h>
#include <iostream>
#include "Globals.h"
using namespace std;
bool keys[5] = {false, false, false, false, false};
enum KEYS {UP, DOWN, LEFT, RIGHT, SPACE};
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 600;
int main()
{
bool done = false;
bool redraw = true;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
if(!al_init())
return -1;
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
display = al_create_display(WINDOW_WIDTH,WINDOW_HEIGHT);
if(!display)
return -1;
al_init_primitives_addon();
al_install_keyboard();
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / 60);
if(!timer)
return -1;
if(!event_queue)
return -1;
al_register_event_source(event_queue,al_get_keyboard_event_source());
al_register_event_source(event_queue,al_get_timer_event_source(timer));
al_register_event_source(event_queue,al_get_display_event_source(display));
Character player;
Character lover;
player.x1 = 100;
player.x2 = player.x1 + 40;
player.y1 = (WINDOW_HEIGHT / 2) - 20;
player.y2 = player.y1 + 40;
lover.x1 = WINDOW_WIDTH - 140;
lover.x2 = lover.x1 + 40;
lover.y1 = (WINDOW_HEIGHT / 2) - 20;
lover.y2 = lover.y1 + 40;
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.timer.source == timer)
{
if(keys[UP])
{
player.y1 -= 5;
player.y2 -= 5;
}
if(keys[DOWN])
{
player.y1 += 5;
player.y2 += 5;
}
if(keys[LEFT])
{
player.x1 -= 5;
player.x2 -= 5;
}
if(keys[RIGHT])
{
player.x1 += 5;
player.x2 += 5;
}
if(player.x1 <= 0)
{
player.x1 = 0;
player.x2 = 40;
}
if(player.x2 >= WINDOW_WIDTH)
{
player.x1 = WINDOW_WIDTH - 40;
player.x2 = WINDOW_WIDTH;
}
if(player.y1 <= 0)
{
player.y1 = 0;
player.y2 = player.y1 + 40;
}
if(player.y2 >= WINDOW_HEIGHT)
{
player.y1 = WINDOW_HEIGHT - 40;
player.y2 = WINDOW_HEIGHT;
}
}
if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
}
}
if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
al_draw_filled_rectangle(player.x1,player.y1,player.x2,player.y2,al_map_rgb(0,0,127));
al_draw_filled_rectangle(lover.x1,lover.y1,lover.x2,lover.y2,al_map_rgb(127,0,0));
al_flip_display();
al_clear_to_color(al_map_rgb(255,255,255));
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
有人可以帮我吗?“Globals.h”只是一个小头文件,其中包含一个用于定义其 x1、x2、y1 和 y2 变量的 Character 结构。谢谢。