Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <allegro5/allegro.h>
  3.  
  4. const float FPS = 60;
  5. const int SCREEN_W = 640;
  6. const int SCREEN_H = 480;
  7. const int BOUNCER_SIZE = 12;
  8. enum MYKEYS {
  9.    KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
  10. };
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.    ALLEGRO_DISPLAY *display = NULL;
  15.    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  16.    ALLEGRO_TIMER *timer = NULL;
  17.    ALLEGRO_BITMAP *bouncer = NULL;
  18.    float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
  19.    float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
  20.    float vel_x = 0;
  21.    float vel_y = 0;
  22.    float acceleration = 0.3;
  23.    float atrito = .01;
  24.    bool key[4] = { false, false, false, false };
  25.    bool redraw = true;
  26.    bool doexit = false;
  27.  
  28.    if(!al_init()) {
  29.       fprintf(stderr, "failed to initialize allegro!\n");
  30.       return -1;
  31.    }
  32.  
  33.    if(!al_install_keyboard()) {
  34.       fprintf(stderr, "failed to initialize the keyboard!\n");
  35.       return -1;
  36.    }
  37.  
  38.    timer = al_create_timer(1.0 / FPS);
  39.    if(!timer) {
  40.       fprintf(stderr, "failed to create timer!\n");
  41.       return -1;
  42.    }
  43.  
  44.    display = al_create_display(SCREEN_W, SCREEN_H);
  45.    if(!display) {
  46.       fprintf(stderr, "failed to create display!\n");
  47.       al_destroy_timer(timer);
  48.       return -1;
  49.    }
  50.  
  51.    bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
  52.    if(!bouncer) {
  53.       fprintf(stderr, "failed to create bouncer bitmap!\n");
  54.       al_destroy_display(display);
  55.       al_destroy_timer(timer);
  56.       return -1;
  57.    }
  58.  
  59.    al_set_target_bitmap(bouncer);
  60.  
  61.    al_clear_to_color(al_map_rgb(255, 0, 255));
  62.  
  63.    al_set_target_bitmap(al_get_backbuffer(display));
  64.  
  65.    event_queue = al_create_event_queue();
  66.    if(!event_queue) {
  67.       fprintf(stderr, "failed to create event_queue!\n");
  68.       al_destroy_bitmap(bouncer);
  69.       al_destroy_display(display);
  70.       al_destroy_timer(timer);
  71.       return -1;
  72.    }
  73.  
  74.    al_register_event_source(event_queue, al_get_display_event_source(display));
  75.  
  76.    al_register_event_source(event_queue, al_get_timer_event_source(timer));
  77.  
  78.    al_register_event_source(event_queue, al_get_keyboard_event_source());
  79.  
  80.    al_clear_to_color(al_map_rgb(0,0,0));
  81.  
  82.    al_flip_display();
  83.  
  84.    al_start_timer(timer);
  85.  
  86.    while(!doexit)
  87.    {
  88.       ALLEGRO_EVENT ev;
  89.       al_wait_for_event(event_queue, &ev);
  90.  
  91.       if(ev.type == ALLEGRO_EVENT_TIMER) {
  92.          if(key[KEY_UP] &&  bouncer_y >= 4.0) {
  93.            // bouncer_y -= 4.
  94.         vel_y -= acceleration;
  95.          }
  96.  
  97.          if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
  98.             //bouncer_y += 4.0;
  99.        if(bouncer_y < 0){
  100.          bouncer_y == 0;
  101.        }
  102.        else
  103.         vel_y += acceleration;
  104.          }
  105.  
  106.          if(key[KEY_LEFT] && bouncer_x >= 4.0) {
  107.             //bouncer_x -= 4.0;
  108.         vel_x -= acceleration;
  109.          }
  110.  
  111.          if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
  112.             //bouncer_x += 4.0;
  113.         vel_x += acceleration;
  114.          }
  115.  
  116.  
  117.     // aplicar as velocidades atuais na posicao do quadrado
  118.     bouncer_x += vel_x;
  119.     bouncer_y += vel_y;
  120.     // aplica gravidade
  121.     //bouncer_y += 0.5;
  122.  
  123.     // atrito
  124.     bouncer_x *= (1 - atrito);
  125.     bouncer_y *=  (1 - atrito);
  126.  
  127.  
  128.          redraw = true;
  129.       }
  130.       else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  131.          break;
  132.       }
  133.       else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
  134.          switch(ev.keyboard.keycode) {
  135.             case ALLEGRO_KEY_UP:
  136.                key[KEY_UP] = true;
  137.                break;
  138.  
  139.             case ALLEGRO_KEY_DOWN:
  140.                key[KEY_DOWN] = true;
  141.                break;
  142.  
  143.             case ALLEGRO_KEY_LEFT:
  144.                key[KEY_LEFT] = true;
  145.                break;
  146.  
  147.             case ALLEGRO_KEY_RIGHT:
  148.                key[KEY_RIGHT] = true;
  149.                break;
  150.          }
  151.       }
  152.       else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
  153.          switch(ev.keyboard.keycode) {
  154.             case ALLEGRO_KEY_UP:
  155.                key[KEY_UP] = false;
  156.                break;
  157.  
  158.             case ALLEGRO_KEY_DOWN:
  159.                key[KEY_DOWN] = false;
  160.                break;
  161.  
  162.             case ALLEGRO_KEY_LEFT:
  163.                key[KEY_LEFT] = false;
  164.                break;
  165.  
  166.             case ALLEGRO_KEY_RIGHT:
  167.                key[KEY_RIGHT] = false;
  168.                break;
  169.  
  170.             case ALLEGRO_KEY_ESCAPE:
  171.                doexit = true;
  172.                break;
  173.          }
  174.       }
  175.  
  176.       if(redraw && al_is_event_queue_empty(event_queue)) {
  177.          redraw = false;
  178.  
  179.          al_clear_to_color(al_map_rgb(0,0,0));
  180.  
  181.          al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
  182.  
  183.          al_flip_display();
  184.       }
  185.    }
  186.  
  187.    al_destroy_bitmap(bouncer);
  188.    al_destroy_timer(timer);
  189.    al_destroy_display(display);
  190.    al_destroy_event_queue(event_queue);
  191.  
  192.    return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement