Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pablo Goulart

Como fazer colisão do personagem com o cenário ?

Recommended Posts

#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;



}


Compartilhar este post


Link para o post
Compartilhar em outros sites

  • Conteúdo Similar

    • Por Sharank
      Strcat Function In C++
       
      I'm new to C and C++ programming, can anyone give me a hint on what I'm doing wrong here. I'm trying to write to concat function that takes to pointers to chars and concatenates the second to the first. The code does do that, but the problem is that it adds a bunch of junk at the end.
       
      For instance, when passing the arguments - "green" and "blue", the output will be "greenblue" plus a bunch of random characters. I also wrote the strlen function that strcat uses, which I will provide below it for reference. I'm using the online compiler at InterviewBit The exact instructions and specification is this:
       
      int main(int argc, char** argv)
      {
      const int MAX = 100;
       
      char s1[MAX];
      char s2[MAX];
       
      cout << "Enter your first string up to 99 characters. ";
      cin.getline(s1, sizeof(s1));
      int size_s1 = strlen(s1);
      cout << "Length of first string is " << size_s1 << "\n";
       
      cout << "Enter your second string up to 99 characters. ";
      cin.getline(s2, sizeof(s2));
      int size_s2 = strlen(s2);
      cout << "Length of second string is " << size_s2 << "\n";
      cout << " Now the first string will be concatenated with the second
      string ";
      char* a = strcat(s1,s2);
       
      for(int i = 0; i<MAX; i++)
      cout <<a;
       
      // system("pause");
      return 0;
      }
       
      //strcat function to contatenate two strings
      char* strcat(char *__s1, const char *__s2)
      {
      int indexOfs1 = strlen(__s1);
      int s2L = strlen(__s2);
      cout <<s2L << "\n";
      int indexOfs2 = 0;
      do{
      __s1[indexOfs1] = __s2[indexOfs2];
      indexOfs1++;
      indexOfs2++;
      }while(indexOfs2 < s2L);
       
       
      return __s1;
      }
       
      //Returns length of char array
      size_t strlen(const char *__s)
      {
      int count = 0;
      int i;
      for (i = 0; __s != '\0'; i++)
      count++;
      return (count) / sizeof(__s[0]);
       
      }
    • Por TkCode
      Estou tentando desenvolver um código para calcular o valor final de custas de imoveis.
      Exemplo: Entro com um valor de R$50.000,00. Tem o ITBI que é 2% sob os R$50.000,00 + o valor de custas que é o valor de uma tabela (essa tabela tem valores que de R$0,01 até R$17.800,90 é uma valor, e assim sucessivamente)
       
      Então teria que calcular os 2% (do valor informado) + o valor da tabela, dando um resultado final com o valor total (2%+ValorTabela).
       
      Alguem teria como me dar uma dica de como resolver isso?
      Desde já agradeço!
    • Por roberson abalaid
      #include <stdio.h>
      #include <stdlib.h>
      int arr[3][5];
      int main(){
          
          printf("Favor inserir os dados...\n");
          
          for(int i = 0; i < 3; i++){
              for(int j = 0; j < 5; j++){
                  scanf("%d", &arr[j]);
              }
          }
          
            printf("os valores inseridos foram...\n");
          
          for(int i = 0; i < 3; i++){
              for(int j = 0; j < 5; j++){
                  printf("  %d  ", arr[j]);
              }
              printf("\n");
          }
          return 0;
      }
    • Por Roberto S. Santos
      Bom dia.
      Eu gostaria de postar uma foto do meu computador no facebok usando VB.NET com login automático.
      Teria como fazer em VB.net ou HTML ?
      Obrigado.
    • Por Quencyjones79
      Olá boa tarde, sou iniciante na linguagem PHP, embora tenha umas noções básicas do código e estou com algumas dificuldades dúvidas no código que está a cor de laranja, se alguém que perceba de código PHP se me puder ajudar, agradecia imenso a ajuda.
       
       
      <?php 
      include "..\ligacao.php";
      ?>
               
      <?php
      $idFunc=$_POST['idFunc'];
      $NomeAlterado=$_POST['NomeAlterado'];
      $idLoja=$_POST['idLoja'];
      $permissao=$_POST['permissao'];
      if($idLoja=="Selecione..."){
          $idLoja=$_POST['idLojaAtual'];
      }
      if($permissao==NULL){
          $qfunc="UPDATE funcionario SET nome_func='".$NomeAlterado."',id_loja='".$idLoja."' WHERE id_func='".$idFunc."'";
          $connfunc=mysqli_query($ligax,$qfunc); 
       }else{    
          $qfunc="UPDATE funcionario SET nome_func='".$NomeAlterado."',id_loja='".$idLoja."', ativo_func='".$permissao."' WHERE id_func='".$idFunc."'";
          $connfunc=mysqli_query($ligax,$qfunc);
       }
      if($connfunc==1){
           print"<script> alert('Funcionário alterado com sucesso!');
                       location.href='../inserirfuncionario.php';</script>";
          exit;
      }else{
      print"<script> alert('Não foi possível alterar o Funcionário!');
          location.href='../inserirfuncionario.php';</script>";
      exit;
      }    
      ?>
       
      Atentamente,
       
      José Moreira
       
×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.