Guest User

Pong Clone (with bugs)

a guest
Apr 27th, 2012
145
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 <allegro5/allegro.h>
  3. #include <allegro5/allegro_image.h>
  4.  
  5. const float FPS = 60;
  6. const int SCREEN_W = 640;
  7. const int SCREEN_H = 480;
  8. const int TILE_SIZE = 32;
  9. const int PADDLE_H = 120;
  10. const int PADDLE_W = 16;
  11. enum MYKEYS{KEY_W, KEY_S, KEY_UP, KEY_DOWN};
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     ALLEGRO_DISPLAY *display = NULL;
  16.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  17.     ALLEGRO_TIMER *timer = NULL;
  18.     ALLEGRO_BITMAP *sprite = NULL, *ballAnim[4] = {'\0'}, *p1Paddle = NULL, *p2Paddle = NULL;
  19.    
  20.     bool keys[4] = {false, false, false, false};
  21.     bool redraw = true;
  22.     bool doexit = false;
  23.     int ticks = 0;
  24.     int anim_x = SCREEN_W / 2, anim_y = SCREEN_H / 2;
  25.     int p1PadX = 12, p2PadX = SCREEN_W - 12 - PADDLE_W;
  26.     int p1PadY = SCREEN_H / 2, p2PadY = SCREEN_H / 2;
  27.     int ball_dx = -4.0, ball_dy = 4.0;
  28.    
  29.     if(!al_init())
  30.     {
  31.         fprintf(stderr, "failed to initialize allegro\n");
  32.         return -1;
  33.     }
  34.    
  35.     if(!al_install_keyboard())
  36.     {
  37.         fprintf(stderr, "failed to initialize keyboard\n");
  38.         return -1;
  39.     }
  40.    
  41.     timer = al_create_timer(1.0 / FPS);
  42.    
  43.     if(!timer)
  44.     {
  45.         fprintf(stderr, "failed to create timer\n");
  46.         return -1;
  47.     }
  48.    
  49.     if(!al_init_image_addon())
  50.     {
  51.         fprintf(stderr, "failed to load image library\n");
  52.         return -1;
  53.     }
  54.    
  55.     display = al_create_display(SCREEN_W, SCREEN_H);
  56.     if(!display)
  57.     {
  58.         fprintf(stderr, "failed to create display\n");
  59.         return -1;
  60.     }
  61.    
  62.     sprite = al_load_bitmap("ball.png");
  63.    
  64.     if(!sprite)
  65.     {
  66.         fprintf(stderr, "failed to load image to sprite\n");
  67.         return -1;
  68.     }
  69.    
  70.     int tile_offset = 0;
  71.    
  72.     for (int i = 0; i < 4; i++)
  73.     {
  74.         ballAnim[i] = al_create_sub_bitmap(sprite, tile_offset, 0, TILE_SIZE, TILE_SIZE);
  75.         tile_offset = tile_offset + TILE_SIZE;
  76.     }
  77.    
  78.     p1Paddle = al_load_bitmap("paddle.png");
  79.     p2Paddle = p1Paddle;
  80.     if(!p1Paddle)
  81.     {
  82.         fprintf(stderr, "failed to load image to paddle\n");
  83.         return -1;
  84.     }
  85.    
  86.     event_queue = al_create_event_queue();
  87.     if(!event_queue)
  88.     {
  89.         fprintf(stderr, "failed to initialize event queue\n");
  90.         return -1;
  91.     }
  92.    
  93.     al_register_event_source(event_queue, al_get_display_event_source(display));
  94.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  95.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  96.    
  97.     al_clear_to_color(al_map_rgb(0, 0, 0));
  98.     al_flip_display();
  99.     al_start_timer(timer);
  100.    
  101.     while(!doexit)
  102.     {
  103.         ALLEGRO_EVENT ev;
  104.         al_wait_for_event(event_queue, &ev);
  105.        
  106.         if(ev.type == ALLEGRO_EVENT_TIMER)
  107.         {
  108.             ticks++;
  109.            
  110.             if(keys[KEY_W]  && p1PadY > 0)
  111.             {
  112.                 p1PadY -= 4;
  113.             }
  114.            
  115.             if(keys[KEY_S] && p1PadY < SCREEN_H - PADDLE_H)
  116.             {
  117.                 p1PadY += 4;
  118.             }
  119.            
  120.             if (keys[KEY_UP] && p2PadY > 0)
  121.             {
  122.                 p2PadY -= 4;
  123.             }
  124.                        
  125.             if(keys[KEY_DOWN] && p2PadY < SCREEN_H - PADDLE_H)
  126.             {
  127.                 p2PadY += 4;
  128.             }
  129.            
  130.             if (anim_x <= p1PadX + PADDLE_W && (anim_y >= p1PadY && anim_y + TILE_SIZE <= p1PadY + PADDLE_H))
  131.             {
  132.                 ball_dx = -ball_dx;
  133.             }
  134.            
  135.             if (anim_x + TILE_SIZE >= p2PadX &&  (anim_y >= p2PadY && anim_y + TILE_SIZE <= p2PadY + PADDLE_H))
  136.             {
  137.                 ball_dx = -ball_dx;
  138.             }
  139.            
  140.             if (anim_x < 0 || anim_x > SCREEN_W - TILE_SIZE)
  141.             {
  142.                 anim_x = SCREEN_W / 2;
  143.                 anim_y = SCREEN_W / 2;
  144.             }
  145.            
  146.             if(anim_y < 0 || anim_y > SCREEN_H - TILE_SIZE)
  147.             {
  148.                 ball_dy = -ball_dy;
  149.             }
  150.            
  151.             anim_x += ball_dx;
  152.             anim_y += ball_dy;
  153.            
  154.             redraw = true;
  155.         }
  156.         else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  157.         {
  158.             break;
  159.         }
  160.         else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
  161.         {
  162.             switch(ev.keyboard.keycode)
  163.             {
  164.                 case ALLEGRO_KEY_UP:
  165.                     keys[KEY_UP] = true;
  166.                 break;
  167.                 case ALLEGRO_KEY_DOWN:
  168.                     keys[KEY_DOWN] = true;
  169.                 break;
  170.                 case ALLEGRO_KEY_W:
  171.                     keys[KEY_W] = true;
  172.                 break;
  173.                 case ALLEGRO_KEY_S:
  174.                     keys[KEY_S] = true;
  175.                 break;
  176.             }
  177.         }
  178.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  179.         {
  180.             switch(ev.keyboard.keycode)
  181.             {
  182.                 case ALLEGRO_KEY_UP:
  183.                     keys[KEY_UP] = false;
  184.                 break;
  185.                 case ALLEGRO_KEY_DOWN:
  186.                     keys[KEY_DOWN] = false;
  187.                 break;
  188.                 case ALLEGRO_KEY_W:
  189.                     keys[KEY_W] = false;
  190.                 break;
  191.                 case ALLEGRO_KEY_S:
  192.                     keys[KEY_S] = false;
  193.                 break;
  194.                 case ALLEGRO_KEY_ESCAPE:
  195.                     doexit = true;
  196.                 break;
  197.             }
  198.         }
  199.        
  200.         if(redraw && al_is_event_queue_empty(event_queue))
  201.         {
  202.             redraw = false;
  203.             al_clear_to_color(al_map_rgb(0, 0, 0));
  204.             al_draw_bitmap(p1Paddle, p1PadX, p1PadY, 0);
  205.             al_draw_bitmap(p2Paddle, p2PadX, p2PadY, 0);
  206.             al_draw_bitmap(ballAnim[ticks / 5 % 4], anim_x, anim_y, 0);
  207.             al_flip_display();
  208.         }
  209.     }
  210.    
  211.     al_clear_to_color(al_map_rgb(0, 0, 0));
  212.     al_flip_display();
  213.     al_destroy_display(display);
  214.                
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment