Added welcome text; added basic player scoring mechanism; fixed bug where ball would start from different positions

master
Aadhavan Srinivasan 10 months ago
parent 8f0d90360a
commit 964bb9452d

@ -23,7 +23,6 @@ void alleg_init() {
return;
}
int main(int argc, char** argv) {
// Initialization
@ -34,6 +33,8 @@ int main(int argc, char** argv) {
ALLEGRO_COLOR black = al_map_rgba(0,0,0,255);
ALLEGRO_COLOR white = al_map_rgba(255,255,255,255);
ALLEGRO_EVENT 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
@ -46,19 +47,25 @@ 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 = 25; // Center coordinates 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
printf("%f\n",circ_vel_x);
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
printf("%f\n",circ_vel_y);
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 {
@ -67,11 +74,17 @@ int main(int argc, char** argv) {
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);
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);
al_get_next_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (event.keyboard.keycode) {
@ -134,7 +147,21 @@ int main(int argc, char** argv) {
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);
@ -142,6 +169,7 @@ int main(int argc, char** argv) {
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

Loading…
Cancel
Save