|
|
|
#include <stdio.h>
|
|
|
|
#include <allegro5/allegro.h>
|
|
|
|
#include <allegro5/allegro_font.h>
|
|
|
|
#include <allegro5/allegro_ttf.h>
|
|
|
|
#include <allegro5/allegro_image.h>
|
|
|
|
#include <allegro5/allegro_primitives.h>
|
|
|
|
|
|
|
|
#define WIN_WIDTH 1000
|
|
|
|
#define WIN_HEIGHT 600
|
|
|
|
|
|
|
|
void alleg_init();
|
|
|
|
|
|
|
|
void alleg_init() {
|
|
|
|
al_init();
|
|
|
|
al_install_keyboard();
|
|
|
|
al_install_mouse();
|
|
|
|
al_init_image_addon();
|
|
|
|
al_init_font_addon();
|
|
|
|
al_init_ttf_addon();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// Initialization
|
|
|
|
|
|
|
|
alleg_init();
|
|
|
|
ALLEGRO_DISPLAY* disp = al_create_display(WIN_WIDTH, WIN_HEIGHT);
|
|
|
|
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
|
|
|
|
al_register_event_source(queue,al_get_keyboard_event_source());
|
|
|
|
ALLEGRO_COLOR black = al_map_rgba(0,0,0,255);
|
|
|
|
ALLEGRO_COLOR white = al_map_rgba(255,255,255,255);
|
|
|
|
ALLEGRO_EVENT event;
|
|
|
|
bool running = true;
|
|
|
|
|
|
|
|
float width = 25; // Width and height of paddles
|
|
|
|
float height = 150;
|
|
|
|
float start_x_1 = 10; // Top-left coordinates of first paddle
|
|
|
|
float start_y_1 = 10;
|
|
|
|
float start_x_2 = WIN_WIDTH - 10 - width; // Top-left coordinates of second paddle
|
|
|
|
float start_y_2 = 10;
|
|
|
|
float velocity_1 = 0; // Vertical velocity of first paddle
|
|
|
|
float velocity_2 = 0; // Vertical velocity of second paddle
|
|
|
|
|
|
|
|
float circ_x = WIN_WIDTH / 2; // Center coordinates of ball
|
|
|
|
float circ_y = WIN_HEIGHT / 2;
|
|
|
|
float circ_rad = 10; // Radius of ball
|
|
|
|
float circ_vel_x = 5; // Horizontal velocity of ball
|
|
|
|
float circ_vel_y = 0; // Vertical velocity of ball
|
|
|
|
|
|
|
|
al_clear_to_color(black);
|
|
|
|
al_draw_filled_rectangle(start_x_1, start_y_1, start_x_1 + width, start_y_1 + height, white);
|
|
|
|
al_draw_filled_rectangle(start_x_2, start_y_2, start_x_2 + width, start_y_2 + height, white);
|
|
|
|
al_draw_filled_circle(circ_x, circ_y, circ_rad, white);
|
|
|
|
al_flip_display();
|
|
|
|
|
|
|
|
while (running) {
|
|
|
|
// Code borrowed from: https://stackoverflow.com/a/30078011
|
|
|
|
|
|
|
|
while (!al_is_event_queue_empty(queue)) {
|
|
|
|
al_wait_for_event(queue, &event);
|
|
|
|
|
|
|
|
if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
|
|
|
|
switch (event.keyboard.keycode) {
|
|
|
|
case ALLEGRO_KEY_W:
|
|
|
|
velocity_1 -= 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_S:
|
|
|
|
velocity_1 += 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_UP:
|
|
|
|
velocity_2 -= 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_DOWN:
|
|
|
|
velocity_2 += 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALLEGRO_KEY_Q:
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (event.type == ALLEGRO_EVENT_KEY_UP) {
|
|
|
|
switch (event.keyboard.keycode) {
|
|
|
|
case ALLEGRO_KEY_W:
|
|
|
|
velocity_1 += 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_S:
|
|
|
|
velocity_1 -= 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_UP:
|
|
|
|
velocity_2 += 5;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_DOWN:
|
|
|
|
velocity_2 -= 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
al_clear_to_color(black);
|
|
|
|
|
|
|
|
// Update positions
|
|
|
|
((velocity_1 < 0 && start_y_1 > 0) || (velocity_1 > 0 && start_y_1 + height < WIN_HEIGHT)) && (start_y_1 += velocity_1);
|
|
|
|
((velocity_2 < 0 && start_y_2 > 0) || (velocity_2 > 0 && start_y_2 + height < WIN_HEIGHT)) && (start_y_2 += velocity_2);
|
|
|
|
circ_x += circ_vel_x;
|
|
|
|
circ_y += circ_vel_y;
|
|
|
|
|
|
|
|
//Redraw shapes
|
|
|
|
al_draw_filled_rectangle(start_x_1, start_y_1, start_x_1 + width, start_y_1 + height + velocity_1, white);
|
|
|
|
al_draw_filled_rectangle(start_x_2, start_y_2, start_x_2 + width, start_y_2 + height + velocity_2, white);
|
|
|
|
al_draw_filled_circle(circ_x, circ_y, circ_rad, white);
|
|
|
|
al_flip_display();
|
|
|
|
|
|
|
|
if (circ_x + circ_rad >= WIN_WIDTH - 10 - width) { // FOR PADDLE 2: If the circle has reached the point where the paddle would be...
|
|
|
|
if (circ_y > start_y_2 && circ_y < start_y_2 + height) { // If the circle is touching the paddle...
|
|
|
|
circ_vel_x = -circ_vel_x;
|
|
|
|
}
|
|
|
|
} else if (circ_x - circ_rad <= 10 + width) { // FOR PADDLE 1
|
|
|
|
if (circ_y > start_y_1 && circ_y < start_y_1 + height) {
|
|
|
|
circ_vel_x =- circ_vel_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
al_destroy_display(disp);
|
|
|
|
al_destroy_event_queue(queue);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|