Implement y-velocity, and added other minor changes

master
Aadhavan Srinivasan 1 year ago
parent ab3a56dc13
commit 8f0d90360a

@ -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
@ -32,7 +34,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;
bool running = true;
bool running = false;
srand(time(NULL)); // Seed for RNG
float width = 25; // Width and height of paddles
float height = 150;
@ -43,11 +46,14 @@ 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 = 25; // 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);
al_clear_to_color(black);
al_draw_filled_rectangle(start_x_1, start_y_1, start_x_1 + width, start_y_1 + height, white);
@ -55,6 +61,12 @@ int main(int argc, char** argv) {
al_draw_filled_circle(circ_x, circ_y, circ_rad, white);
al_flip_display();
do {
al_wait_for_event(queue, &event);
} while (!(event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_SPACE));
running = true;
while (running) {
// Code borrowed from: https://stackoverflow.com/a/30078011
@ -108,12 +120,6 @@ int main(int argc, char** argv) {
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;
@ -124,6 +130,18 @@ int main(int argc, char** argv) {
}
}
if ((circ_y + circ_rad >= WIN_HEIGHT) || (circ_y - circ_rad <= 0)) {
circ_vel_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();
}
// Clean up

Loading…
Cancel
Save