Advertisement
Adrian_Q

Problem Exit

Dec 22nd, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_native_dialog.h>
  3. #include <allegro5/allegro_ttf.h>
  4. #include <allegro5/allegro_font.h>
  5. #include <allegro5/allegro_primitives.h>
  6. #include <allegro5/allegro_image.h>
  7. #include <allegro5/allegro_audio.h>
  8. #include <allegro5/allegro_acodec.h>
  9. #include <iostream>
  10. #include <string>
  11. #include <stdio.h>
  12. #include <cmath>
  13. #include <stdlib.h>
  14. using namespace std;
  15. #define ScreenWidth 1366
  16. #define ScreenHeight 768
  17. unsigned int dist(int a, int b,int c,int d)
  18. {
  19.     return sqrt(pow(a - c, 2) + pow(b - d, 2));
  20. }
  21. int main() {
  22.     if (!al_init())
  23.     {
  24.         al_show_native_message_box(NULL, NULL, NULL, "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
  25.         return -1;
  26.     }
  27.     al_set_new_display_flags(ALLEGRO_FULLSCREEN);
  28.     char c[32] = "caca";
  29.     char c2[32];
  30.     bool redraw = true;
  31.     ALLEGRO_DISPLAY *display;
  32.     //song
  33.     display = al_create_display(ScreenWidth, ScreenHeight);
  34.     if (!display)
  35.     {
  36.         al_show_native_message_box(display, "Sample Title", "Display Settings", "Display problems", NULL, ALLEGRO_MESSAGEBOX_ERROR);
  37.         return -1;
  38.     }
  39.     al_init_font_addon();
  40.     al_init_ttf_addon();
  41.     ALLEGRO_FONT *font = al_load_font("orange juice.ttf", 72, NULL);
  42.     ALLEGRO_FONT *font2 = al_load_font("beer money.ttf", 72, NULL);
  43.     long sec = 0;
  44.     long score = 0;
  45.     long clicked = 0;
  46.     //keyboard
  47.     al_init_primitives_addon();
  48.     al_install_keyboard();
  49.     al_install_mouse();
  50.     al_install_audio();
  51.     al_init_image_addon();
  52.     al_init_acodec_addon();
  53.     //
  54.  
  55.     //image
  56.     ALLEGRO_BITMAP *player = al_load_bitmap("Aim.png");
  57.     ALLEGRO_BITMAP *player2 = al_load_bitmap("Like.png");
  58.     //timer
  59.     bool done = false, draw = true;
  60.     int x = 10, y = 10, moveSpeed = 5, x2=rand()%1200,y2=rand()%700;
  61.     ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
  62.     ALLEGRO_TIMER *timer = al_create_timer(1.0 / 30);
  63.     ALLEGRO_TIMER *timer2 = al_create_timer(1.0);
  64.     al_reserve_samples(1);
  65.     ALLEGRO_SAMPLE *song = al_load_sample("salam.ogg");
  66.     ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
  67.     al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
  68.     al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
  69.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  70.     al_register_event_source(event_queue, al_get_mouse_event_source());
  71.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  72.     al_register_event_source(event_queue, al_get_timer_event_source(timer2));
  73.     int dir = 0;
  74.     al_play_sample_instance(songInstance);
  75.     al_start_timer(timer);
  76.     al_start_timer(timer2);
  77.     al_hide_mouse_cursor(display);
  78.     while (!done) {
  79.         ALLEGRO_EVENT events;
  80.         al_wait_for_event(event_queue, &events);
  81.         switch (events.keyboard.keycode) {
  82.         case ALLEGRO_KEY_DOWN:
  83.             dir = 1; break;
  84.         case ALLEGRO_KEY_UP:
  85.             dir = 2; break;
  86.         case ALLEGRO_KEY_RIGHT:
  87.             dir = 3; break;
  88.         case ALLEGRO_KEY_LEFT:
  89.             dir = 4; break;
  90.         case ALLEGRO_KEY_ESCAPE:
  91.         {done = true; break; }
  92.         }
  93.  
  94.  
  95.         if (events.type == ALLEGRO_EVENT_MOUSE_AXES)
  96.         {
  97.             x = events.mouse.x;
  98.             y = events.mouse.y;
  99.         }
  100.         if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
  101.         {
  102.             if (events.mouse.button & 1)
  103.             {
  104.                 if (dist(x, y, x2, y2) < 20)
  105.                     score++;
  106.  
  107.             }
  108.             if (events.mouse.button & 2)
  109.             {
  110.                 if (dist(x, y, x2, y2) < 20)
  111.                     score += 2;
  112.             }
  113.         }
  114.         if (events.type == ALLEGRO_EVENT_TIMER)
  115.         {
  116.  
  117.             if (events.timer.source == timer)
  118.             {
  119.                 sprintf_s(c, "%d/%d", sec, dist(x, y, x2, y2));
  120.                 sprintf_s(c2, "Score: %d", score);
  121.                 al_draw_bitmap(player2, x2, y2, ALLEGRO_FLIP_HORIZONTAL);
  122.                 al_draw_bitmap(player, x, y, ALLEGRO_FLIP_HORIZONTAL);
  123.                 al_draw_text(font, al_map_rgb(44, 127, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTER, c);
  124.                 al_draw_text(font2, al_map_rgb(44, 127, 255), ScreenWidth / 2, (ScreenHeight / 2)-200, ALLEGRO_ALIGN_CENTER, c2);
  125.             }
  126.             if (events.timer.source == timer2)
  127.             {
  128.                 sec++;
  129.  
  130.                 if (sec >= 5)
  131.                 {
  132.                     x2 = rand() % 1200;
  133.                     y2 = rand() % 700;
  134.                     sec = 0;
  135.                     clicked = 0;
  136.                 }
  137.             }
  138.             redraw = true;
  139.         }
  140.         else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  141.             break;
  142.         }
  143.         if (redraw&&al_is_event_queue_empty(event_queue)) {
  144.             redraw = false;
  145.             al_flip_display();
  146.             al_clear_to_color(al_map_rgb(0, 0, 0));
  147.         }
  148.  
  149.         //
  150.     }
  151.     al_destroy_timer(timer);
  152.     al_destroy_bitmap(player);
  153.     al_destroy_bitmap(player2);
  154.     al_destroy_font(font);
  155.     al_destroy_font(font2);
  156.     al_destroy_sample(song);
  157.     al_destroy_sample_instance(songInstance);
  158.     al_destroy_display(display);
  159.     al_destroy_event_queue(event_queue);
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement