Advertisement
BHXSpecter

Pong With Primitives and Collision Response Issues

Apr 4th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.99 KB | None | 0 0
  1. // Pong Code
  2. #include <iostream>
  3. #include <allegro5/allegro.h>
  4. #include <allegro5/allegro_image.h>
  5. #include <allegro5/allegro_primitives.h>
  6. #include <allegro5/allegro_font.h>
  7.  
  8. using namespace std;
  9.  
  10. const int SCREEN_W = 640;
  11. const int SCREEN_H = 480;
  12. const float FPS = 60.0;
  13. const int START_X = SCREEN_W / 2;
  14. const int START_Y = SCREEN_H / 2;
  15. enum MYKEYS {
  16.     KEY_W, KEY_S, KEY_UP, KEY_DOWN, KEY_M
  17. };
  18.  
  19. int main()
  20. {
  21.     ALLEGRO_DISPLAY *display = NULL;
  22.     ALLEGRO_TIMER *timer = NULL;
  23.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  24.     ALLEGRO_FONT *font = NULL;
  25.     ALLEGRO_BITMAP *paddle1, *paddle2, *ball, *scrnshot;
  26.  
  27.     bool key[5] = { false, false, false, false, false };
  28.     bool redraw = true;
  29.     bool doexit = false;
  30.  
  31.     const int BSIZE = 32;
  32.     int padW = 10, padH = 120;
  33.     int pad1X = 10, pad1Y = SCREEN_H / 2 ;
  34.     int pad2X = SCREEN_W -(10 + padW), pad2Y = SCREEN_H / 2;
  35.     int ballX = START_X;
  36.     int ballY = START_Y;
  37.     int xVel = 4, yVel = 4;
  38.     int p1Score = 0, p2Score = 0;
  39.  
  40.     int fps = 0, fps_accum = 0;
  41.     double fps_time = 0;
  42.  
  43.     if(!al_init())
  44.     {
  45.         cout << "Failed to initialize allegro library.\n";
  46.         return -1;
  47.     }
  48.  
  49.     if(!al_install_keyboard())
  50.     {
  51.         cout << "Failed to install keyboard.\n";
  52.         return -1;
  53.     }
  54.  
  55.     if(!al_init_primitives_addon())
  56.     {
  57.         cout << "Failed to initialize primitives addon.\n";
  58.         return -1;
  59.     }
  60.  
  61.     al_init_image_addon();
  62.     al_init_font_addon();
  63.  
  64.     timer = al_create_timer(1.0 / FPS);
  65.     if(!timer)
  66.     {
  67.         cout << "Failed to create timer.\n";
  68.         return -1;
  69.     }
  70.  
  71.     display = al_create_display(SCREEN_W, SCREEN_H);
  72.     if(!display)
  73.     {
  74.         cout << "Failed to create display.\n";
  75.         return -1;
  76.     }
  77.  
  78.     font = al_load_font("data/fixed_font.tga", 0, 0);
  79.     if(!font)
  80.     {
  81.         cout << "Failed to load font.\n";
  82.         return -1;
  83.     }
  84.  
  85.     event_queue = al_create_event_queue();
  86.     if(!event_queue)
  87.     {
  88.         cout << "Failed to create event queue.\n";
  89.         return -1;
  90.     }
  91.  
  92.     paddle1 = al_create_bitmap(padW, padH);
  93.     al_set_target_bitmap(paddle1);
  94.     al_clear_to_color(al_map_rgb(0, 191, 255));
  95.     al_set_target_bitmap(al_get_backbuffer(display));
  96.  
  97.     paddle2 = al_create_bitmap(padW, padH);
  98.     al_set_target_bitmap(paddle2);
  99.     al_clear_to_color(al_map_rgb(0, 191, 255));
  100.     al_set_target_bitmap(al_get_backbuffer(display));
  101.  
  102.     ball = al_create_bitmap(BSIZE, BSIZE);
  103.     al_set_target_bitmap(ball);
  104.     al_clear_to_color(al_map_rgb(255, 0, 255));
  105.     al_set_target_bitmap(al_get_backbuffer(display));
  106.  
  107.     al_register_event_source(event_queue, al_get_display_event_source(display));
  108.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  109.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  110.  
  111.     al_clear_to_color(al_map_rgb(0, 0, 0));
  112.  
  113.     al_flip_display();
  114.  
  115.     al_start_timer(timer);
  116.  
  117.     while(!doexit)
  118.     {
  119.         ALLEGRO_EVENT ev;
  120.         al_wait_for_event(event_queue, &ev);
  121.  
  122.         if(ev.type == ALLEGRO_EVENT_TIMER)
  123.         {
  124.             // logic goes here
  125.             if(ballX < 0 || ballX > SCREEN_W - BSIZE)
  126.             {
  127.                 if(ballX < 0)p2Score++;
  128.                 if(ballX > SCREEN_W - BSIZE)p1Score++;
  129.                 ballX = START_X;
  130.                 ballY = START_Y;
  131.                 xVel = -xVel;
  132.                 yVel = -yVel;
  133.             }
  134.  
  135.             if(ballY < 0 || ballY > SCREEN_H - BSIZE)
  136.                 yVel = -yVel;
  137.  
  138.             ballX += xVel;
  139.             ballY += yVel;
  140.  
  141.             // collision detection
  142.             if(pad1X + padW > ballX && pad1Y < ballY + BSIZE && pad1Y + padH > ballY)
  143.             {
  144.                 xVel = -xVel;
  145.             }
  146.  
  147.             if(pad2X < ballX + BSIZE && pad2Y < ballY + BSIZE && pad2Y + padH > ballY)
  148.             {
  149.                 xVel = -xVel;
  150.             }
  151.  
  152.             if(key[KEY_W] && pad1Y >= 0)
  153.                 pad1Y -= 5;
  154.             if(key[KEY_S] && pad1Y <= SCREEN_H - padH)
  155.                 pad1Y += 5;
  156.  
  157.             if(key[KEY_UP] && pad2Y >= 0)
  158.                 pad2Y -= 5;
  159.             if(key[KEY_DOWN] && pad2Y <= SCREEN_H - padH)
  160.                 pad2Y += 5;
  161.             if(key[KEY_M])
  162.             {
  163.                 scrnshot = al_create_sub_bitmap(al_get_backbuffer(display), 0, 0, SCREEN_W, SCREEN_H);
  164.                 al_save_bitmap("data/screens/scrnshot.png", scrnshot);
  165.             }
  166.             redraw = true;
  167.         }
  168.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  169.         {
  170.             break;
  171.         }
  172.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  173.         {
  174.             switch(ev.keyboard.keycode)
  175.             {
  176.                 case ALLEGRO_KEY_W:
  177.                     key[KEY_W] = true;
  178.                     break;
  179.                 case ALLEGRO_KEY_S:
  180.                     key[KEY_S] = true;
  181.                     break;
  182.                 case ALLEGRO_KEY_UP:
  183.                     key[KEY_UP] = true;
  184.                     break;
  185.                 case ALLEGRO_KEY_DOWN:
  186.                     key[KEY_DOWN] = true;
  187.                     break;
  188.                 case ALLEGRO_KEY_M:
  189.                     key[KEY_M] = true;
  190.                     break;
  191.             }
  192.         }
  193.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  194.         {
  195.             switch(ev.keyboard.keycode)
  196.             {
  197.                 case ALLEGRO_KEY_W:
  198.                     key[KEY_W] = false;
  199.                     break;
  200.                 case ALLEGRO_KEY_S:
  201.                     key[KEY_S] = false;
  202.                     break;
  203.                 case ALLEGRO_KEY_UP:
  204.                     key[KEY_UP] = false;
  205.                     break;
  206.                 case ALLEGRO_KEY_DOWN:
  207.                     key[KEY_DOWN] = false;
  208.                     break;
  209.                 case ALLEGRO_KEY_M:
  210.                     key[KEY_M] = false;
  211.                     break;
  212.                 case ALLEGRO_KEY_ESCAPE:
  213.                     doexit = true;
  214.                     break;
  215.             }
  216.         }
  217.  
  218.         if(redraw && al_is_event_queue_empty(event_queue))
  219.         {
  220.             // draw code goes here
  221.             redraw = false;
  222.             al_clear_to_color(al_map_rgb(0, 0, 0));
  223.             double t = al_get_time();
  224.  
  225.             al_draw_bitmap(ball, ballX, ballY, 0);
  226.             al_draw_bitmap(paddle1, pad1X, pad1Y, 0);
  227.             al_draw_bitmap(paddle2, pad2X, pad2Y, 0);
  228.             al_draw_filled_rounded_rectangle(4, 4, 100, 30, 8, 8, al_map_rgba(0, 0, 0, 200));
  229.             al_draw_textf(font, al_map_rgb(255, 255, 255), 8, 8, 0, "FPS: %d", fps);
  230.             al_draw_textf(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, 10, ALLEGRO_ALIGN_CENTRE, "%d           SCORE            %d", p1Score, p2Score);
  231.  
  232.             al_flip_display();
  233.  
  234.             fps_accum++;
  235.             if(t - fps_time >= 1)
  236.             {
  237.                 fps = fps_accum;
  238.                 fps_accum = 0;
  239.                 fps_time = t;
  240.             }
  241.         }
  242.     }
  243.  
  244.     return 0;
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement