Advertisement
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 <iostream>
- #include <vector>
- using namespace std;
- struct Cycle
- {
- int x, y;
- int r, g, b;
- int score;
- char direct;
- };
- const int SCREEN_W = 640;
- const int SCREEN_H = 480;
- const double FPS = 60.0;
- const int BXSTART = (SCREEN_W / 2) - 10;
- const int RXSTART = (SCREEN_W / 2) + 10;
- const int CYSTART = SCREEN_H / 2;
- const int DVEL = 1;
- enum MYKEYS{ KEY_A, KEY_S, KEY_W, KEY_D, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT };
- int main()
- {
- int fps = 0, fps_accum = 0;
- double fps_time = 0.0;
- Cycle blueCycle;
- Cycle redCycle;
- blueCycle.x = BXSTART;
- redCycle.x = RXSTART;
- blueCycle.y = CYSTART;
- redCycle.y = CYSTART;
- blueCycle.r = 0;
- blueCycle.g = 0;
- blueCycle.b = 255;
- redCycle.r = 255;
- redCycle.g = 0;
- redCycle.b = 0;
- blueCycle.score = 0;
- redCycle.score = 0;
- int redWall[SCREEN_W][SCREEN_H] = {0};
- int blueWall[SCREEN_W][SCREEN_H] = {0};
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_FONT *frames, *scores;
- bool key[8] = { false, false, false, false, false, false, false, false };
- bool redraw = true;
- bool doexit = false;
- // TODO: add proper error checking to the initialization code
- al_init();
- al_install_keyboard();
- al_init_primitives_addon();
- al_init_font_addon(); // can't be error checked
- al_init_ttf_addon(); // can't be error checked
- timer = al_create_timer(1.0 / FPS);
- display = al_create_display(SCREEN_W, SCREEN_H);
- event_queue = al_create_event_queue();
- scores = al_load_ttf_font("pirulen.ttf", 12, 0);
- frames = al_load_ttf_font("pirulen.ttf", 12, 0);
- 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_register_event_source(event_queue, al_get_keyboard_event_source());
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_flip_display();
- al_start_timer(timer);
- while(!doexit)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if(ev.type == ALLEGRO_EVENT_TIMER)
- {
- redraw = true;
- // logic goes here
- // tell the blue cycle which direction to automatically run til next key press
- switch(blueCycle.direct)
- {
- case 'l':
- blueCycle.x -= DVEL;
- break;
- case 'r':
- blueCycle.x += DVEL;
- break;
- case 'u':
- blueCycle.y -= DVEL;
- break;
- case 'd':
- blueCycle.y += DVEL;
- break;
- }
- // red cycle will run automatically in picked direction til next key press
- switch(redCycle.direct)
- {
- case 'L':
- redCycle.x -= DVEL;
- break;
- case 'R':
- redCycle.x += DVEL;
- break;
- case 'U':
- redCycle.y -= DVEL;
- break;
- case 'D':
- redCycle.y += DVEL;
- break;
- }
- // set the walls collision
- redWall[redCycle.x][redCycle.y] = 1;
- blueWall[blueCycle.x][blueCycle.y] = 1;
- // collision detection for if it hits the screen edges or other cycles wall
- if(redWall[blueCycle.x][blueCycle.y] == 1 || blueCycle.x < 0 || blueCycle.x > SCREEN_W || blueCycle.y < 0 || blueCycle.y > SCREEN_H ||
- blueWall[redCycle.x][redCycle.y] == 1 || redCycle.x < 0 || redCycle.x > SCREEN_W || redCycle.y < 0 || redCycle.y > SCREEN_H)
- {
- al_clear_to_color(al_map_rgb(0, 0, 0));
- if(redWall[blueCycle.x][blueCycle.y] == 1){ redCycle.score++; }
- if(blueWall[redCycle.x][redCycle.y] == 1) { blueCycle.score++; }
- for(int i = 0; i <= SCREEN_W; i++)
- {
- for(int j = 0; j <= SCREEN_H; j++)
- {
- blueWall[i][j] = 0;
- redWall[i][j] = 0;
- }
- }
- blueCycle.direct = 'n';
- redCycle.direct = 'n';
- blueCycle.x = BXSTART;
- blueCycle.y = CYSTART;
- redCycle.x = RXSTART;
- redCycle.y = CYSTART;
- }
- }
- else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- break;
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_A:
- key[KEY_A] = true;
- blueCycle.direct = 'l';
- break;
- case ALLEGRO_KEY_S:
- key[KEY_S] = true;
- blueCycle.direct = 'd';
- break;
- case ALLEGRO_KEY_W:
- key[KEY_W] = true;
- blueCycle.direct = 'u';
- break;
- case ALLEGRO_KEY_D:
- key[KEY_D] = true;
- blueCycle.direct = 'r';
- break;
- case ALLEGRO_KEY_UP:
- key[KEY_UP] = true;
- redCycle.direct = 'U';
- break;
- case ALLEGRO_KEY_DOWN:
- key[KEY_DOWN] = true;
- redCycle.direct = 'D';
- break;
- case ALLEGRO_KEY_LEFT:
- key[KEY_LEFT] = true;
- redCycle.direct = 'L';
- break;
- case ALLEGRO_KEY_RIGHT:
- key[KEY_RIGHT] = true;
- redCycle.direct = 'R';
- break;
- }
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_A:
- key[KEY_A] = false;
- break;
- case ALLEGRO_KEY_S:
- key[KEY_S] = false;
- break;
- case ALLEGRO_KEY_W:
- key[KEY_W] = false;
- break;
- case ALLEGRO_KEY_D:
- key[KEY_D] = false;
- break;
- case ALLEGRO_KEY_UP:
- key[KEY_UP] = false;
- break;
- case ALLEGRO_KEY_DOWN:
- key[KEY_DOWN] = false;
- break;
- case ALLEGRO_KEY_LEFT:
- key[KEY_LEFT] = false;
- break;
- case ALLEGRO_KEY_RIGHT:
- key[KEY_RIGHT] = false;
- break;
- case ALLEGRO_KEY_ESCAPE:
- doexit = true;
- break;
- }
- }
- if(redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- double t = al_get_time();
- //al_clear_to_color(al_map_rgb(0, 0, 0));
- // draw code goes here
- al_draw_pixel(blueCycle.x, blueCycle.y, al_map_rgb(blueCycle.r, blueCycle.g, blueCycle.b));
- al_draw_pixel(redCycle.x, redCycle.y, al_map_rgb(redCycle.r, redCycle.g, redCycle.b));
- al_draw_textf(scores, al_map_rgb(88, 86, 86), 12, 12, 0, "%d", blueCycle.score);
- al_draw_textf(scores, al_map_rgb(88, 86, 86), SCREEN_W - 24, 12, 0, "%d", redCycle.score);
- al_flip_display();
- fps_accum++;
- if(t - fps_time >= 1)
- {
- fps = fps_accum;
- fps_accum = 0;
- fps_time = t;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement