Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5\allegro.h>
- #include <allegro5\allegro_primitives.h>
- #include <allegro5\allegro_font.h>
- #include <allegro5\allegro_ttf.h>
- #include <allegro5\allegro_image.h>
- #include "objects.h"
- enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE};
- bool keys[4] = {false, false, false, false};
- int main(void)
- {
- //primitive variable
- bool done = false;
- const int FPS = 60;
- bool redraw = true;
- bool isGameOver = false;
- //object variables
- RoadBear oso;
- Premio premios[NUM_FOOD];
- Coche coches[NUM_CARS];
- Coche2 coches2[NUM_CARS2];
- Coche3 coches3[NUM_CARS3];
- Coche4 coches4[NUM_CARS4];
- Coche5 coches5[NUM_CARS5];
- Coche6 coches6[NUM_CARS6];
- Coche7 coches7[NUM_CARS7];
- Coche8 coches8[NUM_CARS8];
- //Allegro variables
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_FONT *font18 = NULL;
- ALLEGRO_BITMAP *image = NULL;
- ALLEGRO_BITMAP *image2 = NULL;
- ALLEGRO_BITMAP *image3 = NULL;
- ALLEGRO_BITMAP *image4 = NULL;
- ALLEGRO_BITMAP *image5 = NULL;
- ALLEGRO_BITMAP *image6 = NULL;
- ALLEGRO_BITMAP *image7 = NULL;
- ALLEGRO_BITMAP *image8 = NULL;
- ALLEGRO_BITMAP *image9 = NULL;
- ALLEGRO_BITMAP *image10 = NULL;
- ALLEGRO_BITMAP *image11 = NULL;
- //Initialization Functions
- if(!al_init()) //initialize Allegro
- return -1;
- display = al_create_display(WIDTH, HEIGHT); //create our display object
- if(!display) //test display object
- return -1;
- al_init_primitives_addon();
- al_install_keyboard();
- al_init_font_addon();
- al_init_ttf_addon();
- al_init_image_addon();
- image2 = al_load_bitmap("road.jpg");
- image3 = al_load_bitmap("auto.png");
- image4 = al_load_bitmap("camion.png");
- image5 = al_load_bitmap("auto2.png");
- image6 = al_load_bitmap("camion2.png");
- image7 = al_load_bitmap("auto2.png");
- image8 = al_load_bitmap("camion2.png");
- image9 = al_load_bitmap("auto.png");
- image10 = al_load_bitmap("camion.png");
- image11 = al_load_bitmap("salmon.png");
- image = al_load_bitmap("bear.png");
- event_queue = al_create_event_queue();
- timer = al_create_timer(1.0 / FPS);
- srand(time(NULL));
- font18 = al_load_font("arial.ttf", 18, 0);
- al_register_event_source(event_queue, al_get_keyboard_event_source());
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_start_timer(timer);
- while(!done)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if(ev.type == ALLEGRO_EVENT_TIMER)
- {
- redraw = true;
- if(keys[UP])
- oso.mover_arriba();
- if(keys[DOWN])
- oso.mover_abajo();
- if(keys[LEFT])
- oso.mover_izquierda();
- if(keys[RIGHT])
- oso.mover_derecha();
- if(!isGameOver)
- {
- for( int i = 0; i < NUM_CARS; i++){
- if(!coches[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches[i].iniciar_coche();
- break;
- }
- }
- coches[i].actualizar_coche();
- }
- for( int i = 0; i < NUM_CARS; i++)
- coches[i].colision_coche(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS2; i++){
- if(!coches2[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches2[i].iniciar_coche2();
- break;
- }
- }
- coches2[i].actualizar_coche2();
- }
- for( int i = 0; i < NUM_CARS2; i++)
- coches2[i].colision_coche2(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS3; i++){
- if(!coches3[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches3[i].iniciar_coche3();
- break;
- }
- }
- coches3[i].actualizar_coche3();
- }
- for( int i = 0; i < NUM_CARS3; i++)
- coches3[i].colision_coche3(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS4; i++){
- if(!coches4[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches4[i].iniciar_coche4();
- break;
- }
- }
- coches4[i].actualizar_coche4();
- }
- for( int i = 0; i < NUM_CARS4; i++)
- coches4[i].colision_coche4(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS5; i++){
- if(!coches5[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches5[i].iniciar_coche5();
- break;
- }
- }
- coches5[i].actualizar_coche5();
- }
- for( int i = 0; i < NUM_CARS5; i++)
- coches5[i].colision_coche5(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS6; i++){
- if(!coches6[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches6[i].iniciar_coche6();
- break;
- }
- }
- coches6[i].actualizar_coche6();
- }
- for( int i = 0; i < NUM_CARS6; i++)
- coches6[i].colision_coche6(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS7; i++){
- if(!coches7[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches7[i].iniciar_coche7();
- break;
- }
- }
- coches7[i].actualizar_coche7();
- }
- for( int i = 0; i < NUM_CARS7; i++)
- coches7[i].colision_coche7(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_CARS8; i++){
- if(!coches8[i].live)
- {
- if(rand() % 500 == 0)
- {
- coches8[i].iniciar_coche8();
- break;
- }
- }
- coches8[i].actualizar_coche8();
- }
- for( int i = 0; i < NUM_CARS8; i++)
- coches8[i].colision_coche8(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- for( int i = 0; i < NUM_FOOD; i++){
- if(!premios[i].live)
- {
- if(rand() % 500 == 0)
- {
- premios[i].iniciar_premio();
- break;
- }
- }
- premios[i].actualizar_premio();
- }
- for( int i = 0; i < NUM_FOOD; i++)
- premios[i].colision_premio(oso);
- {
- if(oso.lives <= 0)
- isGameOver = true;
- }
- }
- }
- else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- done = true;
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_ESCAPE:
- done = true;
- break;
- case ALLEGRO_KEY_UP:
- keys[UP] = true;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[DOWN] = true;
- break;
- case ALLEGRO_KEY_RIGHT:
- keys[RIGHT] = true;
- break;
- case ALLEGRO_KEY_LEFT:
- keys[LEFT] = true;
- break;
- }
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- keys[UP] = false;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[DOWN] = false;
- break;
- case ALLEGRO_KEY_RIGHT:
- keys[RIGHT] = false;
- break;
- case ALLEGRO_KEY_LEFT:
- keys[LEFT] = false;
- break;
- }
- }
- if(redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- if(!isGameOver)
- {
- al_draw_bitmap(image2, WIDTH-799, HEIGHT-455, 0);
- oso.dibujar_oso(image);
- for( int i = 0; i < NUM_CARS; i++)
- coches[i].dibujar_coche(image3);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS2; i++)
- coches2[i].dibujar_coche2(image4);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS3; i++)
- coches3[i].dibujar_coche3(image5);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS4; i++)
- coches4[i].dibujar_coche4(image6);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS5; i++)
- coches5[i].dibujar_coche5(image7);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS6; i++)
- coches6[i].dibujar_coche6(image8);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS7; i++)
- coches7[i].dibujar_coche7(image9);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_CARS8; i++)
- coches8[i].dibujar_coche8(image10);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- for( int i = 0; i < NUM_FOOD; i++)
- premios[i].dibujar_premio(image11);
- al_draw_textf(font18, al_map_rgb(255, 255, 255), 5, 5, 0, "Vidas: %i. Score: %i", oso.lives, oso.score);
- }
- else
- {
- al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. */nPuntuacion Final: %i", oso.score);
- }
- al_flip_display();
- al_clear_to_color(al_map_rgb(0,0,0));
- }
- }
- al_destroy_event_queue(event_queue);
- al_destroy_timer(timer);
- al_destroy_font(font18);
- al_destroy_display(display); //destroy our display object
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment