Advertisement
BHXSpecter

Lightcycle Modified (Unfinished)

Aug 13th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 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 <iostream>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. struct Cycle
  12. {
  13.     int x, y;
  14.     int r, g, b;
  15.     int score;
  16.     char direct;
  17. };
  18.  
  19. const int SCREEN_W = 640;
  20. const int SCREEN_H = 480;
  21. const double FPS = 60.0;
  22. const int BXSTART = (SCREEN_W / 2) - 10;
  23. const int RXSTART = (SCREEN_W / 2) + 10;
  24. const int CYSTART = SCREEN_H / 2;
  25. const int DVEL = 1;
  26.  
  27. enum MYKEYS{ KEY_A, KEY_S, KEY_W, KEY_D, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT };
  28.  
  29. int main()
  30. {
  31.     int fps = 0, fps_accum = 0;
  32.     double fps_time = 0.0;    
  33.  
  34.     Cycle blueCycle;
  35.     Cycle redCycle;
  36.  
  37.     blueCycle.x = BXSTART;
  38.     redCycle.x = RXSTART;
  39.  
  40.     blueCycle.y = CYSTART;
  41.     redCycle.y = CYSTART;
  42.  
  43.     blueCycle.r = 0;
  44.     blueCycle.g = 0;
  45.     blueCycle.b = 255;
  46.  
  47.     redCycle.r = 255;
  48.     redCycle.g = 0;
  49.     redCycle.b = 0;
  50.  
  51.     blueCycle.score = 0;
  52.     redCycle.score = 0;
  53.  
  54.     int redWall[SCREEN_W][SCREEN_H] = {0};
  55.     int blueWall[SCREEN_W][SCREEN_H] = {0};
  56.  
  57.     ALLEGRO_DISPLAY *display = NULL;
  58.     ALLEGRO_TIMER *timer = NULL;
  59.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  60.     ALLEGRO_FONT *frames, *scores;
  61.  
  62.     bool key[8] = { false, false, false, false, false, false, false, false };
  63.     bool redraw = true;
  64.     bool doexit = false;
  65.     // TODO: add proper error checking to the initialization code
  66.     al_init();
  67.     al_install_keyboard();
  68.     al_init_primitives_addon();
  69.     al_init_font_addon(); // can't be error checked
  70.     al_init_ttf_addon(); // can't be error checked
  71.     timer = al_create_timer(1.0 / FPS);
  72.     display = al_create_display(SCREEN_W, SCREEN_H);
  73.     event_queue = al_create_event_queue();
  74.  
  75.     scores = al_load_ttf_font("pirulen.ttf", 12, 0);
  76.     frames = al_load_ttf_font("pirulen.ttf", 12, 0);
  77.  
  78.     al_register_event_source(event_queue, al_get_display_event_source(display));
  79.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  80.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  81.  
  82.     al_clear_to_color(al_map_rgb(0, 0, 0));
  83.     al_flip_display();
  84.     al_start_timer(timer);
  85.  
  86.     while(!doexit)
  87.     {
  88.         ALLEGRO_EVENT ev;
  89.         al_wait_for_event(event_queue, &ev);
  90.  
  91.         if(ev.type == ALLEGRO_EVENT_TIMER)
  92.         {
  93.             redraw = true;
  94.  
  95.             // logic goes here
  96.  
  97.             // tell the blue cycle which direction to automatically run til next key press
  98.             switch(blueCycle.direct)
  99.             {
  100.                 case 'l':
  101.                     blueCycle.x -= DVEL;
  102.                     break;
  103.                 case 'r':
  104.                     blueCycle.x += DVEL;
  105.                     break;
  106.                 case 'u':
  107.                     blueCycle.y -= DVEL;
  108.                     break;
  109.                 case 'd':
  110.                     blueCycle.y += DVEL;
  111.                     break;
  112.             }
  113.             // red cycle will run automatically in picked direction til next key press
  114.             switch(redCycle.direct)
  115.             {
  116.                 case 'L':
  117.                     redCycle.x -= DVEL;
  118.                     break;
  119.                 case 'R':
  120.                     redCycle.x += DVEL;
  121.                     break;
  122.                 case 'U':
  123.                     redCycle.y -= DVEL;
  124.                     break;
  125.                 case 'D':
  126.                     redCycle.y += DVEL;
  127.                     break;
  128.             }
  129.  
  130.             // set the walls collision
  131.             redWall[redCycle.x][redCycle.y] = 1;
  132.             blueWall[blueCycle.x][blueCycle.y] = 1;
  133.  
  134.             // collision detection for if it hits the screen edges or other cycles wall
  135.             if(redWall[blueCycle.x][blueCycle.y] == 1 || blueCycle.x < 0 || blueCycle.x > SCREEN_W || blueCycle.y < 0 || blueCycle.y > SCREEN_H ||
  136.                blueWall[redCycle.x][redCycle.y] == 1 || redCycle.x < 0 || redCycle.x > SCREEN_W || redCycle.y < 0 || redCycle.y > SCREEN_H)
  137.             {
  138.                 al_clear_to_color(al_map_rgb(0, 0, 0));
  139.                 if(redWall[blueCycle.x][blueCycle.y] == 1){ redCycle.score++; }
  140.                 if(blueWall[redCycle.x][redCycle.y] == 1) { blueCycle.score++; }
  141.                 for(int i = 0; i <= SCREEN_W; i++)
  142.                 {
  143.                     for(int j = 0; j <= SCREEN_H; j++)
  144.                     {
  145.                         blueWall[i][j] = 0;
  146.                         redWall[i][j] = 0;
  147.                     }
  148.                 }
  149.                 blueCycle.direct = 'n';
  150.                 redCycle.direct = 'n';
  151.                 blueCycle.x = BXSTART;
  152.                 blueCycle.y = CYSTART;
  153.                 redCycle.x = RXSTART;
  154.                 redCycle.y = CYSTART;
  155.             }
  156.  
  157.         }
  158.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  159.         {
  160.             break;
  161.         }
  162.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  163.         {
  164.             switch(ev.keyboard.keycode)
  165.             {
  166.                 case ALLEGRO_KEY_A:
  167.                     key[KEY_A] = true;
  168.                     blueCycle.direct = 'l';
  169.                     break;
  170.                 case ALLEGRO_KEY_S:
  171.                     key[KEY_S] = true;
  172.                     blueCycle.direct = 'd';
  173.                     break;
  174.                 case ALLEGRO_KEY_W:
  175.                     key[KEY_W] = true;
  176.                     blueCycle.direct = 'u';
  177.                     break;
  178.                 case ALLEGRO_KEY_D:
  179.                     key[KEY_D] = true;
  180.                     blueCycle.direct = 'r';
  181.                     break;
  182.                 case ALLEGRO_KEY_UP:
  183.                     key[KEY_UP] = true;
  184.                     redCycle.direct = 'U';
  185.                     break;
  186.                 case ALLEGRO_KEY_DOWN:
  187.                     key[KEY_DOWN] = true;
  188.                     redCycle.direct = 'D';
  189.                     break;
  190.                 case ALLEGRO_KEY_LEFT:
  191.                     key[KEY_LEFT] = true;
  192.                     redCycle.direct = 'L';
  193.                     break;
  194.                 case ALLEGRO_KEY_RIGHT:
  195.                     key[KEY_RIGHT] = true;
  196.                     redCycle.direct = 'R';
  197.                     break;
  198.             }
  199.         }
  200.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  201.         {
  202.             switch(ev.keyboard.keycode)
  203.             {
  204.                 case ALLEGRO_KEY_A:
  205.                     key[KEY_A] = false;
  206.                     break;
  207.                 case ALLEGRO_KEY_S:
  208.                     key[KEY_S] = false;
  209.                     break;
  210.                 case ALLEGRO_KEY_W:
  211.                     key[KEY_W] = false;
  212.                     break;
  213.                 case ALLEGRO_KEY_D:
  214.                     key[KEY_D] = false;
  215.                     break;
  216.                 case ALLEGRO_KEY_UP:
  217.                     key[KEY_UP] = false;
  218.                     break;
  219.                 case ALLEGRO_KEY_DOWN:
  220.                     key[KEY_DOWN] = false;
  221.                     break;
  222.                 case ALLEGRO_KEY_LEFT:
  223.                     key[KEY_LEFT] = false;
  224.                     break;
  225.                 case ALLEGRO_KEY_RIGHT:
  226.                     key[KEY_RIGHT] = false;
  227.                     break;
  228.                 case ALLEGRO_KEY_ESCAPE:
  229.                     doexit = true;
  230.                     break;
  231.             }
  232.         }
  233.  
  234.         if(redraw && al_is_event_queue_empty(event_queue))
  235.         {
  236.             redraw = false;
  237.             double t = al_get_time();
  238.             //al_clear_to_color(al_map_rgb(0, 0, 0));
  239.             // draw code goes here
  240.             al_draw_pixel(blueCycle.x, blueCycle.y, al_map_rgb(blueCycle.r, blueCycle.g, blueCycle.b));
  241.             al_draw_pixel(redCycle.x, redCycle.y, al_map_rgb(redCycle.r, redCycle.g, redCycle.b));
  242.             al_draw_textf(scores, al_map_rgb(88, 86, 86), 12, 12, 0, "%d", blueCycle.score);
  243.             al_draw_textf(scores, al_map_rgb(88, 86, 86), SCREEN_W - 24, 12, 0, "%d", redCycle.score);
  244.  
  245.             al_flip_display();
  246.  
  247.             fps_accum++;
  248.             if(t - fps_time >= 1)
  249.             {
  250.                 fps = fps_accum;
  251.                 fps_accum = 0;
  252.                 fps_time = t;
  253.             }
  254.         }
  255.     }
  256.  
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement