Advertisement
BHXSpecter

Allegro 5 Pong Attempt

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