Glaas2

main

Jun 13th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.57 KB | None | 0 0
  1. #include <allegro5\allegro.h>
  2. #include <allegro5\allegro_primitives.h>
  3. #include <allegro5\allegro_font.h>
  4. #include <allegro5\allegro_ttf.h>
  5. #include <allegro5\allegro_image.h>
  6. #include "objects.h"
  7.  
  8. enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE};
  9. bool keys[4] = {false, false, false, false};
  10.  
  11. int main(void)
  12. {
  13.     //primitive variable
  14.     bool done = false;
  15.     const int FPS = 60;
  16.     bool redraw = true;
  17.     bool isGameOver = false;
  18.  
  19.     //object variables
  20.     RoadBear oso;
  21.     Premio premios[NUM_FOOD];
  22.     Coche coches[NUM_CARS];
  23.     Coche2 coches2[NUM_CARS2];
  24.     Coche3 coches3[NUM_CARS3];
  25.     Coche4 coches4[NUM_CARS4];
  26.     Coche5 coches5[NUM_CARS5];
  27.     Coche6 coches6[NUM_CARS6];
  28.     Coche7 coches7[NUM_CARS7];
  29.     Coche8 coches8[NUM_CARS8];     
  30.    
  31.     //Allegro variables
  32.     ALLEGRO_DISPLAY *display = NULL;
  33.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  34.     ALLEGRO_TIMER *timer = NULL;
  35.     ALLEGRO_FONT *font18 = NULL;
  36.     ALLEGRO_BITMAP *image = NULL;
  37.     ALLEGRO_BITMAP *image2 = NULL;
  38.     ALLEGRO_BITMAP *image3 = NULL;
  39.     ALLEGRO_BITMAP *image4 = NULL;
  40.     ALLEGRO_BITMAP *image5 = NULL;
  41.     ALLEGRO_BITMAP *image6 = NULL;
  42.     ALLEGRO_BITMAP *image7 = NULL;
  43.     ALLEGRO_BITMAP *image8 = NULL;          
  44.     ALLEGRO_BITMAP *image9 = NULL;
  45.     ALLEGRO_BITMAP *image10 = NULL;
  46.     ALLEGRO_BITMAP *image11 = NULL;
  47.    
  48.     //Initialization Functions
  49.     if(!al_init())                                      //initialize Allegro
  50.         return -1;
  51.  
  52.     display = al_create_display(WIDTH, HEIGHT);         //create our display object
  53.  
  54.     if(!display)                                        //test display object
  55.         return -1;
  56.  
  57.     al_init_primitives_addon();
  58.     al_install_keyboard();
  59.     al_init_font_addon();
  60.     al_init_ttf_addon();
  61.     al_init_image_addon();
  62.    
  63.     image2 = al_load_bitmap("road.jpg");
  64.     image3 = al_load_bitmap("auto.png");
  65.     image4 = al_load_bitmap("camion.png");
  66.     image5 = al_load_bitmap("auto2.png");
  67.     image6 = al_load_bitmap("camion2.png");
  68.     image7 = al_load_bitmap("auto2.png");
  69.     image8 = al_load_bitmap("camion2.png");
  70.     image9 = al_load_bitmap("auto.png");
  71.     image10 = al_load_bitmap("camion.png");
  72.     image11 = al_load_bitmap("salmon.png");
  73.     image = al_load_bitmap("bear.png");
  74.  
  75.     event_queue = al_create_event_queue();
  76.     timer = al_create_timer(1.0 / FPS);
  77.     srand(time(NULL));
  78.    
  79.     font18 = al_load_font("arial.ttf", 18, 0);
  80.    
  81.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  82.     al_register_event_source(event_queue, al_get_display_event_source(display));
  83.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  84.    
  85.     al_start_timer(timer);
  86.  
  87.     while(!done)
  88.     {
  89.         ALLEGRO_EVENT ev;
  90.         al_wait_for_event(event_queue, &ev);
  91.  
  92.         if(ev.type == ALLEGRO_EVENT_TIMER)
  93.         {
  94.             redraw = true;
  95.             if(keys[UP])
  96.                 oso.mover_arriba();
  97.             if(keys[DOWN])
  98.                 oso.mover_abajo();
  99.             if(keys[LEFT])
  100.                 oso.mover_izquierda();
  101.             if(keys[RIGHT])
  102.                 oso.mover_derecha();
  103.            
  104.             if(!isGameOver)
  105.             {
  106.                for( int i = 0; i < NUM_CARS; i++){
  107.                      if(!coches[i].live)
  108.                     {
  109.                         if(rand() % 500 == 0)
  110.                         {
  111.                             coches[i].iniciar_coche();
  112.                             break;
  113.                         }
  114.                     }
  115.                     coches[i].actualizar_coche();
  116.                 }
  117.                 for( int i = 0; i < NUM_CARS; i++)
  118.                       coches[i].colision_coche(oso);
  119.                 {      
  120.                 if(oso.lives <= 0)
  121.                     isGameOver = true;
  122.                 }  
  123.                
  124.                 for( int i = 0; i < NUM_CARS2; i++){
  125.                      if(!coches2[i].live)
  126.                     {
  127.                         if(rand() % 500 == 0)
  128.                         {
  129.                             coches2[i].iniciar_coche2();
  130.                             break;
  131.                         }
  132.                     }
  133.                     coches2[i].actualizar_coche2();
  134.                 }
  135.                 for( int i = 0; i < NUM_CARS2; i++)
  136.                       coches2[i].colision_coche2(oso);
  137.                 {      
  138.                 if(oso.lives <= 0)
  139.                     isGameOver = true; 
  140.                 }
  141.                    
  142.                 for( int i = 0; i < NUM_CARS3; i++){
  143.                      if(!coches3[i].live)
  144.                     {
  145.                         if(rand() % 500 == 0)
  146.                         {
  147.                             coches3[i].iniciar_coche3();
  148.                             break;
  149.                         }
  150.                     }
  151.                     coches3[i].actualizar_coche3();
  152.                 }
  153.                 for( int i = 0; i < NUM_CARS3; i++)
  154.                       coches3[i].colision_coche3(oso);
  155.                 {      
  156.                 if(oso.lives <= 0)
  157.                     isGameOver = true;
  158.                 }
  159.                
  160.                 for( int i = 0; i < NUM_CARS4; i++){
  161.                      if(!coches4[i].live)
  162.                     {
  163.                         if(rand() % 500 == 0)
  164.                         {
  165.                             coches4[i].iniciar_coche4();
  166.                             break;
  167.                         }
  168.                     }
  169.                     coches4[i].actualizar_coche4();
  170.                 }
  171.                 for( int i = 0; i < NUM_CARS4; i++)
  172.                       coches4[i].colision_coche4(oso);
  173.                 {      
  174.                 if(oso.lives <= 0)
  175.                     isGameOver = true;                 
  176.                 }
  177.                
  178.                 for( int i = 0; i < NUM_CARS5; i++){
  179.                      if(!coches5[i].live)
  180.                     {
  181.                         if(rand() % 500 == 0)
  182.                         {
  183.                             coches5[i].iniciar_coche5();
  184.                             break;
  185.                         }
  186.                     }
  187.                     coches5[i].actualizar_coche5();
  188.                 }
  189.                 for( int i = 0; i < NUM_CARS5; i++)
  190.                       coches5[i].colision_coche5(oso);
  191.                 {      
  192.                 if(oso.lives <= 0)
  193.                     isGameOver = true;  
  194.                 }          
  195.                
  196.                 for( int i = 0; i < NUM_CARS6; i++){
  197.                      if(!coches6[i].live)
  198.                     {
  199.                         if(rand() % 500 == 0)
  200.                         {
  201.                             coches6[i].iniciar_coche6();
  202.                             break;
  203.                         }
  204.                     }
  205.                     coches6[i].actualizar_coche6();
  206.                 }
  207.                 for( int i = 0; i < NUM_CARS6; i++)
  208.                       coches6[i].colision_coche6(oso);
  209.                 {      
  210.                 if(oso.lives <= 0)
  211.                     isGameOver = true;
  212.                 }
  213.                
  214.                 for( int i = 0; i < NUM_CARS7; i++){
  215.                      if(!coches7[i].live)
  216.                     {
  217.                         if(rand() % 500 == 0)
  218.                         {
  219.                             coches7[i].iniciar_coche7();
  220.                             break;
  221.                         }
  222.                     }
  223.                     coches7[i].actualizar_coche7();
  224.                 }
  225.                 for( int i = 0; i < NUM_CARS7; i++)
  226.                       coches7[i].colision_coche7(oso);
  227.                 {      
  228.                 if(oso.lives <= 0)
  229.                     isGameOver = true;    
  230.                 }  
  231.                
  232.                 for( int i = 0; i < NUM_CARS8; i++){
  233.                      if(!coches8[i].live)
  234.                     {
  235.                         if(rand() % 500 == 0)
  236.                         {
  237.                             coches8[i].iniciar_coche8();
  238.                             break;
  239.                         }
  240.                     }
  241.                     coches8[i].actualizar_coche8();
  242.                 }
  243.                 for( int i = 0; i < NUM_CARS8; i++)
  244.                       coches8[i].colision_coche8(oso);
  245.                 {      
  246.                 if(oso.lives <= 0)
  247.                     isGameOver = true;         
  248.                 }
  249.                
  250.                 for( int i = 0; i < NUM_FOOD; i++){
  251.                      if(!premios[i].live)
  252.                     {
  253.                         if(rand() % 500 == 0)
  254.                         {
  255.                             premios[i].iniciar_premio();
  256.                             break;
  257.                         }
  258.                     }
  259.                     premios[i].actualizar_premio();
  260.                 }
  261.                 for( int i = 0; i < NUM_FOOD; i++)
  262.                       premios[i].colision_premio(oso);
  263.                 {      
  264.                 if(oso.lives <= 0)
  265.                     isGameOver = true;
  266.                 }  
  267.              }
  268.         }
  269.        
  270.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  271.         {
  272.             done = true;
  273.         }
  274.        
  275.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  276.         {
  277.             switch(ev.keyboard.keycode)
  278.             {
  279.                 case ALLEGRO_KEY_ESCAPE:
  280.                     done = true;
  281.                     break;
  282.                 case ALLEGRO_KEY_UP:
  283.                     keys[UP] = true;
  284.                     break;
  285.                 case ALLEGRO_KEY_DOWN:
  286.                     keys[DOWN] = true;
  287.                     break;
  288.                 case ALLEGRO_KEY_RIGHT:
  289.                     keys[RIGHT] = true;
  290.                     break;
  291.                 case ALLEGRO_KEY_LEFT:
  292.                     keys[LEFT] = true;
  293.                     break;
  294.  
  295.             }
  296.         }
  297.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  298.         {
  299.             switch(ev.keyboard.keycode)
  300.             {
  301.                 case ALLEGRO_KEY_UP:
  302.                     keys[UP] = false;
  303.                     break;
  304.                 case ALLEGRO_KEY_DOWN:
  305.                     keys[DOWN] = false;
  306.                     break;
  307.                 case ALLEGRO_KEY_RIGHT:
  308.                     keys[RIGHT] = false;
  309.                     break;
  310.                 case ALLEGRO_KEY_LEFT:
  311.                     keys[LEFT] = false;
  312.                     break;
  313.  
  314.             }
  315.         }
  316.        
  317.         if(redraw && al_is_event_queue_empty(event_queue))
  318.         {
  319.             redraw = false;
  320.            
  321.             if(!isGameOver)
  322.             {
  323.                 al_draw_bitmap(image2, WIDTH-799, HEIGHT-455, 0);
  324.                 oso.dibujar_oso(image);
  325.                
  326.                 for( int i = 0; i < NUM_CARS; i++)
  327.                      coches[i].dibujar_coche(image3);
  328.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  329.                
  330.                 for( int i = 0; i < NUM_CARS2; i++)
  331.                      coches2[i].dibujar_coche2(image4);
  332.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  333.                
  334.                 for( int i = 0; i < NUM_CARS3; i++)
  335.                      coches3[i].dibujar_coche3(image5);
  336.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  337.                
  338.                 for( int i = 0; i < NUM_CARS4; i++)
  339.                      coches4[i].dibujar_coche4(image6);
  340.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  341.                
  342.                 for( int i = 0; i < NUM_CARS5; i++)
  343.                      coches5[i].dibujar_coche5(image7);
  344.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  345.                
  346.                 for( int i = 0; i < NUM_CARS6; i++)
  347.                      coches6[i].dibujar_coche6(image8);
  348.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);    
  349.                
  350.                 for( int i = 0; i < NUM_CARS7; i++)
  351.                      coches7[i].dibujar_coche7(image9);
  352.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  353.                
  354.                 for( int i = 0; i < NUM_CARS8; i++)
  355.                      coches8[i].dibujar_coche8(image10);
  356.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
  357.                        
  358.                 for( int i = 0; i < NUM_FOOD; i++)
  359.                      premios[i].dibujar_premio(image11);
  360.                 al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);                      
  361.              }
  362.              else
  363.             {
  364.                 al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. */nPuntuacion Final: %i", oso.score);
  365.             }
  366.            
  367.             al_flip_display();
  368.             al_clear_to_color(al_map_rgb(0,0,0));
  369.         }
  370.     }
  371.  
  372.     al_destroy_event_queue(event_queue);
  373.     al_destroy_timer(timer);
  374.     al_destroy_font(font18);
  375.     al_destroy_display(display);                        //destroy our display object
  376.  
  377.     return 0;
  378. }
Advertisement
Add Comment
Please, Sign In to add comment