Como fazer colisão do personagem com o cenário ?
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_image.h>
const float FPS = 60;
const int SCREEN_W = 850;
const int SCREEN_H = 640;
const int BLUE = 64;
enum TECLADO { CIMA, BAIXO, DIREITA, ESQUERDA, END };
int main()
{
//_______VARIAVEIS DO JOGO________
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *blue = NULL;
ALLEGRO_BITMAP *Cenario = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
float bouncer_x = SCREEN_W / 2.0 - BLUE / 2.0;
float bouncer_y = SCREEN_H / 2.0 - BLUE / 2.0;
bool key[] = { false, false, false, false };
bool redraw = true;
bool doexit = false;
bool desenha = true;
bool fim = false;
//________INICIALIZAÇÃO DO ALLEGRO 5__________
if (!al_init()) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize allegro!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
if (!al_init_image_addon()) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
//al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(800, 600);
if (!display) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
blue = al_load_bitmap("Blue.bmp");
Cenario = al_load_bitmap("Cubo.bmp");
if (!Cenario) {
al_show_native_message_box(display, "Error", "Error", "Failed to load image!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
al_destroy_display(display);
return 0;
}
event_queue = al_create_event_queue();
if (!event_queue) {
al_show_native_message_box(display, "Error", "Error", "Failed to create event_queue!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
al_destroy_display(display);
return 0;
}
timer = al_create_timer(1.0 / FPS);
if (!timer) {
al_show_native_message_box(display, "Error", "Error", "failed to create timer!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
al_destroy_display(display);
return 0;
}
al_set_target_bitmap(al_get_backbuffer(display));
//______INSTALAÇÃO________
al_install_keyboard();
//________REGISTRO___________
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_flip_display();
al_start_timer(timer);
//_________LOOP PRINCIPAL__________
while (!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
fim = true;
}
if (ev.type == ALLEGRO_EVENT_TIMER) {
desenha = false;
if (key[CIMA] && bouncer_y >= 4.0) {
bouncer_y -= 4.0;
}
if (key[bAIXO] && bouncer_y <= SCREEN_H - BLUE - 4.0) {
bouncer_y += 4.0;
}
if (key[ESQUERDA] && bouncer_x >= 4.0) {
bouncer_x -= 4.0;
}
if (key[DIREITA] && bouncer_x <= SCREEN_W - BLUE - 4.0) {
bouncer_x += 4.0;
}
if (key[END])
redraw = true;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_W:
key[CIMA] = true;
break;
case ALLEGRO_KEY_S:
key[bAIXO] = true;
break;
case ALLEGRO_KEY_A:
key[ESQUERDA] = true;
break;
case ALLEGRO_KEY_D:
key[DIREITA] = true;
break;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_W:
key[CIMA] = false;
break;
case ALLEGRO_KEY_S:
key[bAIXO] = false;
break;
case ALLEGRO_KEY_A:
key[ESQUERDA] = false;
break;
case ALLEGRO_KEY_D:
key[DIREITA] = false;
break;
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(Cenario, 80, 0, 0);
al_draw_bitmap(blue, bouncer_x, bouncer_y, 0);
al_flip_display();
}
}
while (1);
//________DESTRUIR_________
al_destroy_display(display);
//al_destroy_bitmap(Correndo);
al_destroy_bitmap(Cenario);
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
al_destroy_bitmap(blue);
return 0;
}Discussão (0)
Carregando comentários...