Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- const float FPS = 60;
- const int SCREEN_W = 640;
- const int SCREEN_H = 480;
- const int TILE_SIZE = 32;
- const int PADDLE_H = 120;
- const int PADDLE_W = 16;
- enum MYKEYS{KEY_W, KEY_S, KEY_UP, KEY_DOWN};
- int main(int argc, char **argv)
- {
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_BITMAP *sprite = NULL, *ballAnim[4] = {'\0'}, *p1Paddle = NULL, *p2Paddle = NULL;
- bool keys[4] = {false, false, false, false};
- bool redraw = true;
- bool doexit = false;
- int ticks = 0;
- int anim_x = SCREEN_W / 2, anim_y = SCREEN_H / 2;
- int p1PadX = 12, p2PadX = SCREEN_W - 12 - PADDLE_W;
- int p1PadY = SCREEN_H / 2, p2PadY = SCREEN_H / 2;
- int ball_dx = -4.0, ball_dy = 4.0;
- if(!al_init())
- {
- fprintf(stderr, "failed to initialize allegro\n");
- return -1;
- }
- if(!al_install_keyboard())
- {
- fprintf(stderr, "failed to initialize keyboard\n");
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if(!timer)
- {
- fprintf(stderr, "failed to create timer\n");
- return -1;
- }
- if(!al_init_image_addon())
- {
- fprintf(stderr, "failed to load image library\n");
- return -1;
- }
- display = al_create_display(SCREEN_W, SCREEN_H);
- if(!display)
- {
- fprintf(stderr, "failed to create display\n");
- return -1;
- }
- sprite = al_load_bitmap("ball.png");
- if(!sprite)
- {
- fprintf(stderr, "failed to load image to sprite\n");
- return -1;
- }
- int tile_offset = 0;
- for (int i = 0; i < 4; i++)
- {
- ballAnim[i] = al_create_sub_bitmap(sprite, tile_offset, 0, TILE_SIZE, TILE_SIZE);
- tile_offset = tile_offset + TILE_SIZE;
- }
- p1Paddle = al_load_bitmap("paddle.png");
- p2Paddle = p1Paddle;
- if(!p1Paddle)
- {
- fprintf(stderr, "failed to load image to paddle\n");
- return -1;
- }
- event_queue = al_create_event_queue();
- if(!event_queue)
- {
- fprintf(stderr, "failed to initialize event queue\n");
- return -1;
- }
- 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)
- {
- ticks++;
- if(keys[KEY_W] && p1PadY > 0)
- {
- p1PadY -= 4;
- }
- if(keys[KEY_S] && p1PadY < SCREEN_H - PADDLE_H)
- {
- p1PadY += 4;
- }
- if (keys[KEY_UP] && p2PadY > 0)
- {
- p2PadY -= 4;
- }
- if(keys[KEY_DOWN] && p2PadY < SCREEN_H - PADDLE_H)
- {
- p2PadY += 4;
- }
- if (anim_x <= p1PadX + PADDLE_W && (anim_y >= p1PadY && anim_y + TILE_SIZE <= p1PadY + PADDLE_H))
- {
- ball_dx = -ball_dx;
- }
- if (anim_x + TILE_SIZE >= p2PadX && (anim_y >= p2PadY && anim_y + TILE_SIZE <= p2PadY + PADDLE_H))
- {
- ball_dx = -ball_dx;
- }
- if (anim_x < 0 || anim_x > SCREEN_W - TILE_SIZE)
- {
- anim_x = SCREEN_W / 2;
- anim_y = SCREEN_W / 2;
- }
- if(anim_y < 0 || anim_y > SCREEN_H - TILE_SIZE)
- {
- ball_dy = -ball_dy;
- }
- anim_x += ball_dx;
- anim_y += ball_dy;
- redraw = true;
- }
- else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- break;
- }
- else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- keys[KEY_UP] = true;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[KEY_DOWN] = true;
- break;
- case ALLEGRO_KEY_W:
- keys[KEY_W] = true;
- break;
- case ALLEGRO_KEY_S:
- keys[KEY_S] = true;
- break;
- }
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- keys[KEY_UP] = false;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[KEY_DOWN] = false;
- break;
- case ALLEGRO_KEY_W:
- keys[KEY_W] = false;
- break;
- case ALLEGRO_KEY_S:
- keys[KEY_S] = false;
- break;
- case ALLEGRO_KEY_ESCAPE:
- doexit = true;
- break;
- }
- }
- if(redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_draw_bitmap(p1Paddle, p1PadX, p1PadY, 0);
- al_draw_bitmap(p2Paddle, p2PadX, p2PadY, 0);
- al_draw_bitmap(ballAnim[ticks / 5 % 4], anim_x, anim_y, 0);
- al_flip_display();
- }
- }
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_flip_display();
- al_destroy_display(display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment