Compare commits

...

5 Commits

2 changed files with 70 additions and 21 deletions

View File

@@ -1,2 +1,4 @@
build:
gcc pong.c -o pong -lallegro_color -lallegro_ttf -lallegro_font -lallegro_image -lallegro_primitives -lallegro
LDLIBS = -lm -lallegro_color -lallegro_ttf -lallegro_font -lallegro_image -lallegro_primitives -lallegro
CFLAGS += -g -Wall -Wextra
pong:

85
pong.c
View File

@@ -1,9 +1,11 @@
#include <stdio.h>
#include <stdlib.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>
#include <math.h>
#define WIN_WIDTH 1000
#define WIN_HEIGHT 600
@@ -21,7 +23,6 @@ void alleg_init() {
return;
}
int main(int argc, char** argv) {
// Initialization
@@ -31,8 +32,11 @@ int main(int argc, char** argv) {
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;
ALLEGRO_EVENT temp_event;
ALLEGRO_FONT* main_font = al_load_ttf_font("/usr/share/fonts/cantarell/Cantarell-VF.otf", 30, 0);
bool running = false;
srand(time(NULL)); // Seed for RNG
float width = 25; // Width and height of paddles
float height = 150;
@@ -43,11 +47,32 @@ int main(int argc, char** argv) {
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
float circ_x = 60; // Center coordinates of ball
float circ_y = 25;
float circ_rad = 15; // Radius of ball
float ball_speed = sqrt(72); // Pythagorean distance travelled by ball in one main-loop iteration
float circ_vel_x = (float)(rand())/(float)(RAND_MAX / 3) + 4.0; // Horizontal velocity of ball (Randomly generated, 4 to 7) - borrowed from https://stackoverflow.com/a/13409133
float circ_vel_y = sqrt(pow(ball_speed,2) - pow(circ_vel_x,2)); // Vertical velocity of ball - Subtract X velocity from hypotenuse / ball_speed
int score_1 = 0; // Player 1 score
int score_2 = 0; // Player 2 score
// I reset the ball position here, so that the ball starts from the same place after I press SPACE
circ_x = 60;
circ_y = 25;
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_draw_text(main_font, white, (WIN_WIDTH / 2), (WIN_HEIGHT / 2), ALLEGRO_ALIGN_CENTRE, "Welcome to Pong!\0");
al_flip_display();
do {
al_wait_for_event(queue, &temp_event);
} while (!(temp_event.type == ALLEGRO_EVENT_KEY_DOWN && temp_event.keyboard.keycode == ALLEGRO_KEY_SPACE));
running = true;
al_clear_to_color(black);
al_draw_filled_rectangle(start_x_1, start_y_1, start_x_1 + width, start_y_1 + height, white);
@@ -56,10 +81,11 @@ int main(int argc, char** argv) {
al_flip_display();
while (running) {
ALLEGRO_EVENT event;
// Code borrowed from: https://stackoverflow.com/a/30078011
while (!al_is_event_queue_empty(queue)) {
al_wait_for_event(queue, &event);
al_get_next_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (event.keyboard.keycode) {
@@ -68,7 +94,7 @@ int main(int argc, char** argv) {
break;
case ALLEGRO_KEY_S:
velocity_1 += 5;
break;
break;
case ALLEGRO_KEY_UP:
velocity_2 -= 5;
break;
@@ -107,12 +133,6 @@ int main(int argc, char** argv) {
((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...
@@ -120,15 +140,42 @@ int main(int argc, char** argv) {
}
} 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;
circ_vel_x = -circ_vel_x;
}
}
if ((circ_y + circ_rad >= WIN_HEIGHT) || (circ_y - circ_rad <= 0)) {
circ_vel_y = -circ_vel_y;
}
if (circ_x - circ_rad <= 0 || circ_x + circ_rad >= WIN_WIDTH) {
if (circ_x - circ_rad <= 0) {
score_2++;
}
if (circ_x + circ_rad >= WIN_WIDTH) {
score_1++;
}
printf("Player 1: %d\n",score_1);
printf("Player 2: %d\n\n", score_2);
circ_x = 60;
circ_y = 25;
circ_vel_x = (float)(rand())/(float)(RAND_MAX / 3) + 4.0;
circ_vel_y = sqrt(pow(ball_speed,2) - pow(circ_vel_x,2));
sleep(1);
}
//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();
printf("Ball x: %.0f y: %.0f\n", circ_x, circ_y);
}
// Clean up
al_destroy_display(disp);
al_destroy_event_queue(queue);
al_destroy_event_queue(queue);
return 0;
}