Advertisement
eliasYFGM

Juego de plataformas en C++/Allegro (Juego.cpp)

Jul 21st, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. // Juego.cpp
  2. // Base para juego de plataformas en C++ y Allegro
  3.  
  4. // Allegro
  5. #include <allegro5/allegro.h>
  6.  
  7. // Funciones para dibujar figuras
  8. #include <allegro5/allegro_primitives.h>
  9.  
  10. // Fnuciones para mostrar mensajes de texto
  11. #include <allegro5/allegro_native_dialog.h>
  12.  
  13.  
  14. #include "Juego.h"
  15. #include "Nivel.h"
  16.  
  17.  
  18.  
  19. namespace {
  20.  
  21. // Para representar el nivel se usa una cadena de caracteres (string)
  22. const char *nivel_prueba =
  23.    "XXXXXXXXXXXXXXXXXXXXXXXXX"
  24.    "X                       X"
  25.    "X                       X"
  26.    "X                       X"
  27.    "X                       X"
  28.    "X                XXXXXXXX"
  29.    "X                       X"
  30.    "X               XXXXXXX X"
  31.    "X                       X"
  32.    "X XXXXXXXX     XXXXXXXXXX"
  33.    "X                       X"
  34.    "X          X X          X"
  35.    "X J XXXXXX X XX         X"
  36.    "X   X      X XXX        X"
  37.    "XXXXXXXXXXXX XXXXXXXXXXXX";
  38.  
  39. } // namespace
  40.  
  41.  
  42. // Inicializar la librería y el juego
  43. bool Juego::Inicializar()
  44. {
  45.    al_init();
  46.  
  47.    if (!al_init_primitives_addon())
  48.    {  al_show_native_message_box(NULL, "Allegro", "Error"
  49.                                  , "No se pudo inicializar el primitives_addon"
  50.                                  , NULL, ALLEGRO_MESSAGEBOX_ERROR);
  51.       return false;
  52.    }
  53.  
  54.    if (!al_install_keyboard())
  55.    {  al_show_native_message_box(NULL, "Allegro", "Error"
  56.                                  , "No se pudo inicializar el teclado", NULL
  57.                                  , ALLEGRO_MESSAGEBOX_ERROR);
  58.       return false;
  59.    }
  60.  
  61.    // Creación de la ventana
  62.    ventana = al_create_display(800, 480);
  63.  
  64.    if (!ventana)
  65.    {  al_show_native_message_box(NULL, "Allegro", "Error"
  66.                                  , "No se pudo crear una ventana para el juego"
  67.                                  , NULL, ALLEGRO_MESSAGEBOX_ERROR);
  68.       return false;
  69.    }
  70.  
  71.    al_set_window_title(ventana, "Juego de plataformas");
  72.  
  73.  
  74.  
  75.    // Crear el "timer" que controla la velocidad del juego
  76.    timer = al_create_timer(1.0 / 60);
  77.  
  78.    // Lista de eventos
  79.    lista_eventos = al_create_event_queue();
  80.  
  81.    // Array que usaremos para checar los botones del teclado
  82.    key = new int[ALLEGRO_KEY_MAX]();
  83.  
  84.  
  85.  
  86.    // Generar los eventos de la ventana (display)
  87.    al_register_event_source(lista_eventos, al_get_display_event_source(ventana));
  88.  
  89.    // ...del timer
  90.    al_register_event_source(lista_eventos, al_get_timer_event_source(timer));
  91.  
  92.    // ...y del teclado
  93.    al_register_event_source(lista_eventos, al_get_keyboard_event_source());
  94.  
  95.  
  96.    // Crear el nivel desde la cadena "nivel_prueba" y actualizar la posición
  97.    // de inicio del jugador.
  98.    Crear_Nivel(nivel_prueba, jugador_x, jugador_y);
  99.  
  100.    jugador_x_inicio = jugador_x;
  101.    jugador_y_inicio = jugador_y;
  102.  
  103.  
  104.    return true;
  105. }
  106.  
  107.  
  108.  
  109. void Juego::Actualizar()
  110. {
  111.    static float velocidad_y = 0;
  112.    static bool saltar = false;
  113.  
  114.    // Mover el jugador a la izquierda...
  115.    if (key[ALLEGRO_KEY_LEFT])
  116.    {  if (!Checar_Plataforma(jugador_x - 4.0, jugador_y))
  117.          jugador_x -= 4.0;
  118.    }
  119.  
  120.    // Mover el jugador a la derecha...
  121.    if (key[ALLEGRO_KEY_RIGHT])
  122.    {  if (!Checar_Plataforma(jugador_x + 4.0, jugador_y))
  123.          jugador_x += 4.0;
  124.    }
  125.  
  126.    if (key[ALLEGRO_KEY_UP])
  127.       saltar = true;
  128.  
  129.  
  130.    // Por defecto, la "gravedad" del jugador será de 0.5
  131.    velocidad_y += 0.5;
  132.  
  133.    // Actualizar la posición "y" con la velocidad
  134.    jugador_y += velocidad_y;
  135.  
  136.  
  137.    // Verificar si hay una plataforma al caer o al saltar
  138.    Plataforma *p = Checar_Plataforma(jugador_x, jugador_y);
  139.  
  140.    if (p)
  141.    {  // Reposicionar el jugador arriba o abajo de la plataforma.
  142.       if (velocidad_y < 0)
  143.          jugador_y = p->y + 32;
  144.       else
  145.          jugador_y = p->y - 32;
  146.  
  147.       velocidad_y = 0;
  148.    }
  149.  
  150.  
  151.    // Saltar si hay una plataforma debajo del jugador
  152.    if (saltar)
  153.    {  if (Checar_Plataforma(jugador_x, jugador_y + 1))
  154.          velocidad_y = -12;
  155.  
  156.       saltar = false;
  157.    }
  158.  
  159.  
  160.    if (jugador_y > 480)
  161.    {  jugador_x = jugador_x_inicio;
  162.       jugador_y = jugador_y_inicio;
  163.       velocidad_y = 0;
  164.    }
  165. }
  166.  
  167.  
  168.  
  169. void Juego::Dibujar()
  170. {
  171.    Dibujar_Nivel();
  172.  
  173.    // Dibujar el jugador
  174.    al_draw_filled_rectangle(jugador_x, jugador_y, jugador_x + TAMANO_JUGADOR_X
  175.                             , jugador_y + TAMANO_JUGADOR_Y
  176.                             , al_map_rgb(0, 255, 0));
  177. }
  178.  
  179.  
  180.  
  181. Juego::Juego()
  182. {
  183.    iniciado = Inicializar();
  184. }
  185.  
  186.  
  187.  
  188. void Juego::Correr()
  189. {
  190.    if (!iniciado)
  191.       return;
  192.  
  193.    // Empezar a correr el timer
  194.    al_start_timer(timer);
  195.  
  196.  
  197.    bool redibujar = false;
  198.  
  199.  
  200.    // Ciclo principal
  201.    while (1)
  202.    {  ALLEGRO_EVENT event;
  203.       al_wait_for_event(lista_eventos, &event);
  204.  
  205.       if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  206.       {  break;
  207.       }
  208.       else if (event.type == ALLEGRO_EVENT_KEY_DOWN)
  209.       {  key[event.keyboard.keycode] = 1;
  210.  
  211.          // Cerrar el juego al pulsar <Esc>
  212.          if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
  213.             break;
  214.       }
  215.       else if (event.type == ALLEGRO_EVENT_KEY_UP)
  216.       {  key[event.keyboard.keycode] = 0;
  217.       }
  218.       else if (event.type == ALLEGRO_EVENT_TIMER)
  219.       {  Actualizar();
  220.          redibujar = true;
  221.       }
  222.  
  223.       if (redibujar && al_is_event_queue_empty(lista_eventos))
  224.       {  redibujar = false;
  225.  
  226.          al_clear_to_color(C_NEGRO);
  227.  
  228.          Dibujar();
  229.  
  230.          al_flip_display();
  231.       }
  232.    }
  233.  
  234.    // Liberar la memoria de todos los recursos
  235.    al_destroy_display(ventana);
  236.    al_destroy_timer(timer);
  237.    al_destroy_event_queue(lista_eventos);
  238.  
  239.    delete key;
  240.  
  241.    Destruir_Nivel();
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement