Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cstdlib>
- #include <ctime>
- #include <fstream>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- using namespace std;
- const float FPS = 60;
- const int SCREEN_W = 640;
- const int SCREEN_H = 480;
- const int BOUNCER_SIZE = 32;
- const int PADDLE_SIZE_X = 15;
- const int PADDLE_SIZE_Y = 160;
- enum MYKEYS{KEY_W, KEY_S};
- int main(int argc, char **argv)
- {
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_BITMAP *bouncer = NULL;
- ALLEGRO_BITMAP *p1Paddle = NULL;
- ALLEGRO_BITMAP *p2Paddle = NULL;
- int randNum;
- float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
- float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
- float p1Paddle_x = 10;
- float p1Paddle_y = SCREEN_H / 2.0 - PADDLE_SIZE_Y / 2.0;
- float p2Paddle_x = SCREEN_W - 10 - PADDLE_SIZE_X;
- float p2Paddle_y = SCREEN_H / 2.0 - PADDLE_SIZE_Y / 2.0;
- float bouncer_dx = -4.0, bouncer_dy = 4.0;
- bool key[2] = {false, false};
- bool doexit = false;
- bool redraw = true;
- ///////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////
- // REMEMBER TO REMOVE THIS ONCE DONE!!!!!!!!!!!
- ofstream outfile;
- outfile.open("pongDebug.txt");
- outfile << setw(8) << right << " Ball X Paddle X Ball Y Paddle Y Paddle Y + Height " << endl;
- if (!al_init())
- {
- fprintf(stderr, "failed to initialize allegro!\n");
- return -1;
- }
- if(!al_install_keyboard())
- {
- fprintf(stderr, "failed to initialize the keyboard!\n");
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if (!timer)
- {
- fprintf(stderr, "failed to create timer!\n");
- return -1;
- }
- display = al_create_display(SCREEN_W, SCREEN_H);
- if (!display)
- {
- fprintf(stderr, "failed to create display!\n");
- return -1;
- }
- bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
- if (!bouncer)
- {
- fprintf(stderr, "failed to create bouncer bitmap!\n");
- al_destroy_display(display);
- al_destroy_timer(timer);
- return -1;
- }
- p1Paddle = al_create_bitmap(PADDLE_SIZE_X, PADDLE_SIZE_Y);
- if (!p1Paddle)
- {
- fprintf(stderr, "failed to create player one paddle bitmap!\n");
- al_destroy_display(display);
- al_destroy_timer(timer);
- return -1;
- }
- p2Paddle = al_create_bitmap(PADDLE_SIZE_X, PADDLE_SIZE_Y);
- if (!p2Paddle)
- {
- fprintf(stderr, "failed to create player two paddle bitmap!\n");
- return -1;
- }
- al_set_target_bitmap(p1Paddle);
- al_clear_to_color(al_map_rgb(0, 191, 255));
- al_set_target_bitmap(p2Paddle);
- al_clear_to_color(al_map_rgb(0, 255, 191));
- al_set_target_bitmap(bouncer);
- al_clear_to_color(al_map_rgb(255, 0, 255));
- al_set_target_bitmap(al_get_backbuffer(display));
- al_start_timer(timer);
- event_queue = al_create_event_queue();
- if (!event_queue)
- {
- fprintf(stderr, "failed to create event_queue!\n");
- al_destroy_bitmap(bouncer);
- al_destroy_display(display);
- al_destroy_timer(timer);
- 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)
- {
- if (bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE)
- {
- bouncer_dx = -bouncer_dx;
- }
- if (bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE)
- {
- bouncer_dy = -bouncer_dy;
- }
- bouncer_x += bouncer_dx;
- bouncer_y += bouncer_dy;
- //if bouncer_y+paddle_h/2 >= screen_h then paddle_y=screen_h-bouncer_h/2 else if bouncer_y-paddle_h/2 < 0 then paddle_y = paddle_h/2 else paddle_h = bouncer_y;
- if (bouncer_y + PADDLE_SIZE_Y / 2 >= SCREEN_H)
- {
- p2Paddle_y = SCREEN_H - PADDLE_SIZE_Y / 2;
- }
- else if(bouncer_y - PADDLE_SIZE_Y / 2 < 0)
- {
- p2Paddle_y = PADDLE_SIZE_Y / 2;
- }
- else
- {
- p2Paddle_y = bouncer_y;
- }
- if (bouncer_x <= p1Paddle_x + PADDLE_SIZE_X && bouncer_y >= p1Paddle_y && bouncer_y <= p1Paddle_y + PADDLE_SIZE_Y)
- {
- bouncer_x++;
- std::cout << "Contact P1 Paddle" << std::endl;
- outfile << setw(8) << right << bouncer_x << setw(13) << p1Paddle_x << setw(11) << bouncer_y << setw(10) << p1Paddle_y << setw(18) << p1Paddle_y + PADDLE_SIZE_Y << endl;
- }
- if(bouncer_x + BOUNCER_SIZE >= p2Paddle_x && bouncer_y >= p2Paddle_y && bouncer_y <= p2Paddle_y + PADDLE_SIZE_Y)
- {
- bouncer_x--;
- std::cout << "Contact P2 Paddle" << std::endl;
- outfile << setw(8) << right << bouncer_x << setw(13) << p2Paddle_x << setw(11) << bouncer_y << setw(10) << p2Paddle_y << setw(18) << p2Paddle_y + PADDLE_SIZE_Y << endl;
- }
- if(key[KEY_W] && p1Paddle_y > 0 )
- {
- p1Paddle_y -= 4;
- }
- if(key[KEY_S] && p1Paddle_y < SCREEN_H - PADDLE_SIZE_Y)
- {
- p1Paddle_y += 4;
- }
- 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;
- }
- }
- 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_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, p1Paddle_x, p1Paddle_y, 0);
- al_draw_bitmap(p2Paddle, p2Paddle_x, p2Paddle_y - PADDLE_SIZE_Y / 2, 0);
- al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
- al_flip_display();
- }
- }
- al_destroy_bitmap(bouncer);
- al_destroy_bitmap(p1Paddle);
- al_destroy_bitmap(p2Paddle);
- al_destroy_timer(timer);
- al_destroy_display(display);
- al_destroy_event_queue(event_queue);
- outfile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement