BHXSpecter

Hmmm...

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