// Pong Code #include #include #include #include #include using namespace std; const int SCREEN_W = 640; const int SCREEN_H = 480; const float FPS = 60.0; const int START_X = SCREEN_W / 2; const int START_Y = SCREEN_H / 2; enum MYKEYS { KEY_W, KEY_S, KEY_UP, KEY_DOWN, KEY_M }; int main() { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_FONT *font = NULL; ALLEGRO_BITMAP *paddle1, *paddle2, *ball, *scrnshot; bool key[5] = { false, false, false, false, false }; bool redraw = true; bool doexit = false; const int BSIZE = 32; int padW = 10, padH = 120; int pad1X = 10, pad1Y = SCREEN_H / 2 ; int pad2X = SCREEN_W -(10 + padW), pad2Y = SCREEN_H / 2; int ballX = START_X; int ballY = START_Y; int xVel = 4, yVel = 4; int p1Score = 0, p2Score = 0; int fps = 0, fps_accum = 0; double fps_time = 0; if(!al_init()) { cout << "Failed to initialize allegro library.\n"; return -1; } if(!al_install_keyboard()) { cout << "Failed to install keyboard.\n"; return -1; } if(!al_init_primitives_addon()) { cout << "Failed to initialize primitives addon.\n"; return -1; } al_init_image_addon(); al_init_font_addon(); timer = al_create_timer(1.0 / FPS); if(!timer) { cout << "Failed to create timer.\n"; return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { cout << "Failed to create display.\n"; return -1; } font = al_load_font("data/fixed_font.tga", 0, 0); if(!font) { cout << "Failed to load font.\n"; return -1; } event_queue = al_create_event_queue(); if(!event_queue) { cout << "Failed to create event queue.\n"; return -1; } paddle1 = al_create_bitmap(padW, padH); al_set_target_bitmap(paddle1); al_clear_to_color(al_map_rgb(0, 191, 255)); al_set_target_bitmap(al_get_backbuffer(display)); paddle2 = al_create_bitmap(padW, padH); al_set_target_bitmap(paddle2); al_clear_to_color(al_map_rgb(0, 191, 255)); al_set_target_bitmap(al_get_backbuffer(display)); ball = al_create_bitmap(BSIZE, BSIZE); al_set_target_bitmap(ball); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); 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) { // logic goes here if(ballX < 0 || ballX > SCREEN_W - BSIZE) { if(ballX < 0)p2Score++; if(ballX > SCREEN_W - BSIZE)p1Score++; ballX = START_X; ballY = START_Y; xVel = -xVel; yVel = -yVel; } if(ballY < 0 || ballY > SCREEN_H - BSIZE) yVel = -yVel; ballX += xVel; ballY += yVel; // collision detection if(pad1X + padW > ballX && pad1Y < ballY + BSIZE && pad1Y + padH > ballY) { xVel = -xVel; } if(pad2X < ballX + BSIZE && pad2Y < ballY + BSIZE && pad2Y + padH > ballY) { xVel = -xVel; } if(key[KEY_W] && pad1Y >= 0) pad1Y -= 5; if(key[KEY_S] && pad1Y <= SCREEN_H - padH) pad1Y += 5; if(key[KEY_UP] && pad2Y >= 0) pad2Y -= 5; if(key[KEY_DOWN] && pad2Y <= SCREEN_H - padH) pad2Y += 5; if(key[KEY_M]) { scrnshot = al_create_sub_bitmap(al_get_backbuffer(display), 0, 0, SCREEN_W, SCREEN_H); al_save_bitmap("data/screens/scrnshot.png", scrnshot); } 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_W: key[KEY_W] = true; break; case ALLEGRO_KEY_S: key[KEY_S] = true; break; case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_M: key[KEY_M] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_W: key[KEY_W] = false; break; case ALLEGRO_KEY_S: key[KEY_S] = false; break; case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_M: key[KEY_M] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { // draw code goes here redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); double t = al_get_time(); al_draw_bitmap(ball, ballX, ballY, 0); al_draw_bitmap(paddle1, pad1X, pad1Y, 0); al_draw_bitmap(paddle2, pad2X, pad2Y, 0); al_draw_filled_rounded_rectangle(4, 4, 100, 30, 8, 8, al_map_rgba(0, 0, 0, 200)); al_draw_textf(font, al_map_rgb(255, 255, 255), 8, 8, 0, "FPS: %d", fps); al_draw_textf(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, 10, ALLEGRO_ALIGN_CENTRE, "%d SCORE %d", p1Score, p2Score); al_flip_display(); fps_accum++; if(t - fps_time >= 1) { fps = fps_accum; fps_accum = 0; fps_time = t; } } } return 0; }