Ir ao conteúdo
  • Cadastre-se

C Adicionar imagens que percorram a tela


Posts recomendados

olá, este codigo é um projeto de jogo utilizando a biblioteca allegro, gostaria de uma ajuda para saber como adicionar imagens que ''fiquem descendo a tela'' constantemente de tempo em tempo com o objetivo de cria obstáculos com elas.

 

 

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_audio.h>


const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int AVIAO_SIZE = 80;
enum MYKEYS {
   KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_SPACE
};

int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;
   ALLEGRO_TIMER *timer = NULL;
   ALLEGRO_BITMAP *aviao_principal = NULL;
   ALLEGRO_SAMPLE *sample=NULL;//Audio
   float bouncer_x = SCREEN_W/2 - AVIAO_SIZE / 2.0;
   float bouncer_y = SCREEN_H-90 - AVIAO_SIZE / 2.0;
   bool key[4] = { false, false, false, false };
   bool redraw = true;
   bool doexit = false;

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

   if(!al_install_keyboard()) {
      fprintf(stderr, "failed to initialize the keyboard!\n");
      return -1;
   }

   timer = al_create_timer(1.0 / FPS);
   if(!timer) {
      fprintf(stderr, "failed to create timer!\n");
      return -1;
   }
   if(!al_install_audio()){
      fprintf(stderr, "failed to initialize audio!\n");
      return -1;
   }

   if(!al_init_acodec_addon()){
      fprintf(stderr, "failed to initialize audio codecs!\n");
      return -1;
   }

   if (!al_reserve_samples(1)){
      fprintf(stderr, "failed to reserve samples!\n");
      return -1;
   }

   sample = al_load_sample("audio/somambiente.wav");
    if (!sample)
    {
        fprintf(stderr, "Falha ao carregar sample.\n");
        al_destroy_display(display);
        return false;
    }

   display = al_create_display(SCREEN_W, SCREEN_H);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      al_destroy_timer(timer);
      return -1;
   }

    al_init_image_addon();
   aviao_principal =  al_load_bitmap("imagens/aviaoprincipal.png");
   if(!aviao_principal) {
      fprintf(stderr, "failed to create avio_principal bitmap!\n");
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }

   al_set_target_bitmap(aviao_principal);

  // al_clear_to_color(al_map_rgb(255, 0, 255));

   al_set_target_bitmap(al_get_backbuffer(display));

   event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_bitmap(aviao_principal);
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }

    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_clear_to_color(al_map_rgb(0,0,0));

   al_flip_display();

   al_start_timer(timer);

   while(!doexit)
   {
      ALLEGRO_EVENT ev;
      al_wait_for_event(event_queue, &ev);
      al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);

      if(ev.type == ALLEGRO_EVENT_TIMER) {
         if(key[KEY_UP] && bouncer_y >= SCREEN_H/2) {
                aviao_principal=al_load_bitmap("imagens/aviaoup.png");
            bouncer_y -= 4.0;
         }

         if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - AVIAO_SIZE - 50) {
                aviao_principal=al_load_bitmap("imagens/aviaoprincipal.png");
            bouncer_y += 4.0;
         }

         if(key[KEY_LEFT] && bouncer_x >= 7.0) {
                aviao_principal=al_load_bitmap("imagens/aviaoleft.png");
                bouncer_x -= 4.0;
         }

         if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - AVIAO_SIZE - 7) {
                aviao_principal=al_load_bitmap("imagens/aviaoright.png");
                bouncer_x += 4.0;
         }

         redraw = true;
      }
      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }
      else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
         switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
               key[KEY_UP] = true;
               break;

            case ALLEGRO_KEY_DOWN:
               key[KEY_DOWN] = true;
               break;

            case ALLEGRO_KEY_LEFT:
               key[KEY_LEFT] = true;
               break;

            case ALLEGRO_KEY_RIGHT:
               key[KEY_RIGHT] = true;
               break;
         }
      }
      else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
         switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
                aviao_principal=al_load_bitmap("imagens/aviaoprincipal.png");
               key[KEY_UP] = false;
               break;

            case ALLEGRO_KEY_DOWN:
                aviao_principal=al_load_bitmap("imagens/aviaoprincipal.png");
               key[KEY_DOWN] = false;
               break;

            case ALLEGRO_KEY_LEFT:
                aviao_principal=al_load_bitmap("imagens/aviaoprincipal.png");
               key[KEY_LEFT] = false;
               break;

            case ALLEGRO_KEY_RIGHT:
                aviao_principal=al_load_bitmap("imagens/aviaoprincipal.png");
               key[KEY_RIGHT] = 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(135,206,250));

         al_draw_bitmap(aviao_principal, bouncer_x, bouncer_y, 0);

         al_flip_display();
      }
   }


   al_destroy_bitmap(aviao_principal);
   al_destroy_timer(timer);
   al_destroy_display(display);
   al_destroy_event_queue(event_queue);
   al_destroy_sample(sample);

   return 0;
}

 

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Basta você desenhar uma imagem com as posições definidas por variáveis, feito isso, você adiciona um incremento/decremento na variável.

EX teórico:

Desenha(Projetil, Posicao_x, Posicao_y);

Posicao_x ++;

Isso dentro do loop principal do jogo vai desenhar sempre 1 pixel a frente, gerando o efeito de movimentação.
 

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!