BHXSpecter

Untitled

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