@ -6,23 +6,25 @@
# include "math-helpers.hpp"
# include "math-helpers.hpp"
/* Global variables used to instantiate structs */
/* Global variables used to instantiate structs */
const int WIDTH = 1 0 00;
const int WIDTH = 1 5 00;
const int HEIGHT = 48 0;
const int HEIGHT = 60 0;
const int RECT_H = HEIGHT / 3 ;
const int RECT_H = HEIGHT / 3 ;
const int RECT_W = 30 ;
const int RECT_W = 30 ;
const int PADDLE_SPEED = 8 ;
const int PADDLE_SPEED = 8 ;
const int CIRC_RAD = 10 ;
const int CIRC_RAD = 10 ;
const float BASE_BOUNCE_DEG = 30 ;
const float BASE_BOUNCE_DEG = 45 ;
const float BASE_BOUNCE_RAD = ( BASE_BOUNCE_DEG / 180.0 ) * M_PI ;
const float BASE_BOUNCE_RAD = ( BASE_BOUNCE_DEG / 180.0 ) * M_PI ;
const float BASE_SPEED_COMPONENTS = 7 ;
const float BASE_SPEED_COMPONENTS = 15 ;
const float BASE_SPEED = sqrt ( powf ( BASE_SPEED_COMPONENTS , 2 ) * 2 ) ;
const float BASE_SPEED = sqrt ( powf ( BASE_SPEED_COMPONENTS , 2 ) * 2 ) ;
raylib : : Vector2 changeVelocityAfterCollision ( Paddle paddle , Ball ball ) {
raylib : : Vector2 changeVelocityAfterCollision ( Paddle paddle , Ball ball ) {
float paddle_mid_y = ( paddle . getRect ( ) . y + paddle . getRect ( ) . GetHeight ( ) ) / 2.0 ; /* Middle y value of rectangle */
float paddle_mid_y = ( paddle . getRect ( ) . y + paddle . getRect ( ) . GetHeight ( ) ) / 2.0 ; /* Middle y value of rectangle */
TraceLog ( LOG_INFO , " \n Mid y-value: %.2f " , paddle_mid_y ) ;
float ball_y = ball . pos . y ; /* Y co-ordinate of ball */
float ball_y = ball . pos . y ; /* Y co-ordinate of ball */
float offset = paddle_mid_y - ball_y ; /* Subtracting the ball coordinate will give us a value between -paddle_mid_y (represents bottom of paddle) and +paddle_mid_y (represents top of paddle) */
float offset = paddle_mid_y - ball_y ; /* Subtracting the ball coordinate will give us a value between -paddle_mid_y (represents bottom of paddle) and +paddle_mid_y (represents top of paddle) */
offset / = ( paddle . getRect ( ) . GetHeight ( ) / 2.0 ) ; /* Normalize the value, by dividing it by its magnitude. It is now a value between -1 and 1. */
offset / = ( paddle . getRect ( ) . GetHeight ( ) / 2.0 ) ; /* Normalize the value, by dividing it by its maximum magnitude. It is now a value between -1 and 1. */
TraceLog ( LOG_INFO , " Offset: %.2f \n " , offset ) ;
// offset *= -1; /* Reverse the sign of the offset, so that -1 represents the top, and 1 represents the bottom */
// offset *= -1; /* Reverse the sign of the offset, so that -1 represents the top, and 1 represents the bottom */
float bounce_angle = offset * BASE_BOUNCE_RAD ; /* Calculate the actual bounce angle from the base bounce angle. */
float bounce_angle = offset * BASE_BOUNCE_RAD ; /* Calculate the actual bounce angle from the base bounce angle. */
@ -36,7 +38,6 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
return raylib : : Vector2 ( new_x_vel , new_y_vel ) ;
return raylib : : Vector2 ( new_x_vel , new_y_vel ) ;
}
}
int main ( ) {
int main ( ) {
/* Initialize window */
/* Initialize window */
raylib : : Window window = raylib : : Window ( WIDTH , HEIGHT , " Pong " ) ;
raylib : : Window window = raylib : : Window ( WIDTH , HEIGHT , " Pong " ) ;
@ -44,68 +45,98 @@ int main() {
window . ClearBackground ( BLACK ) ;
window . ClearBackground ( BLACK ) ;
SetTargetFPS ( 60 ) ;
SetTargetFPS ( 60 ) ;
SetExitKey ( KEY_Q ) ;
SetExitKey ( KEY_Q ) ;
std : : string points_str = std : : string ( " 0 \t \t 0 " ) ;
bool game_started = false ;
/* Instantiate Paddle and Ball objects */
/* Instantiate Paddle and Ball objects */
Paddle pad1 = Paddle ( 10 , 10 , RECT_W , RECT_H ) ;
Paddle pad1 = Paddle ( 10 , ( HEIGHT / 2 ) - ( RECT_H / 2 ) , RECT_W , RECT_H ) ;
Paddle pad2 = Paddle ( window . GetWidth ( ) - RECT_W - 10 , 10 , RECT_W , RECT_H ) ;
Paddle pad2 = Paddle ( window . GetWidth ( ) - RECT_W - 10 , ( HEIGHT / 2 ) - ( RECT_H / 2 ) , RECT_W , RECT_H ) ;
Ball ball = Ball ( window . GetWidth ( ) / 2 , window . GetHeight ( ) / 2 , CIRC_RAD , 5 , 5 ) ;
Ball ball = Ball ( window . GetWidth ( ) / 2 , window . GetHeight ( ) / 2 , CIRC_RAD , BASE_SPEED , 0 ) ;
window . BeginDrawing ( ) ;
window . BeginDrawing ( ) ;
pad1 . draw ( ) ;
pad1 . draw ( ) ;
pad2 . draw ( ) ;
pad2 . draw ( ) ;
ball . draw ( ) ;
ball . draw ( ) ;
window . EndDrawing ( ) ;
/* Main loop */
/* Main loop */
while ( ! window . ShouldClose ( ) ) {
while ( ! window . ShouldClose ( ) ) {
/* Update paddle velocity */
if ( IsKeyPressed ( KEY_S ) ) {
pad1 . velocity . y = PADDLE_SPEED ; /* Set positive (downward) velocity, since (0,0) is top-left */
}
if ( IsKeyPressed ( KEY_W ) ) {
pad1 . velocity . y = ( - 1 ) * PADDLE_SPEED ; /* Set negative (upward) velocity */
}
if ( IsKeyReleased ( KEY_S ) | | IsKeyReleased ( KEY_W ) ) {
if ( ! game_started ) {
pad1 . velocity . y = 0 ;
if ( IsKeyDown ( KEY_SPACE ) ) {
}
game_started = true ;
}
if ( IsKeyPressed ( KEY_UP ) ) {
pad2 . velocity . y = ( - 1 ) * PADDLE_SPEED ;
}
if ( IsKeyPressed ( KEY_DOWN ) ) {
pad2 . velocity . y = PADDLE_SPEED ;
}
if ( IsKeyReleased ( KEY_UP ) | | IsKeyReleased ( KEY_DOWN ) ) {
pad2 . velocity . y = 0 ;
}
}
/* Update ball velocity based on collision detection */
if ( game_started ) {
if ( pad1 . getRect ( ) . CheckCollision ( ball . pos , ball . radius ) ) { /* Collision with paddle 1 */
/* Update paddle velocity */
ball . pos . x = pad1 . getRect ( ) . x + pad1 . getRect ( ) . GetWidth ( ) + ball . radius + 1 ; /* Ensuring that the ball doesn't get stuck inside the paddle */
if ( IsKeyPressed ( KEY_S ) ) {
ball . vel = changeVelocityAfterCollision ( pad1 , ball ) ;
pad1 . velocity . y = PADDLE_SPEED ; /* Set positive (downward) velocity, since (0,0) is top-left */
}
if ( IsKeyPressed ( KEY_W ) ) {
pad1 . velocity . y = ( - 1 ) * PADDLE_SPEED ; /* Set negative (upward) velocity */
}
if ( IsKeyReleased ( KEY_S ) | | IsKeyReleased ( KEY_W ) ) {
pad1 . velocity . y = 0 ;
}
if ( IsKeyPressed ( KEY_UP ) ) {
pad2 . velocity . y = ( - 1 ) * PADDLE_SPEED ;
}
if ( IsKeyPressed ( KEY_DOWN ) ) {
pad2 . velocity . y = PADDLE_SPEED ;
}
if ( IsKeyReleased ( KEY_UP ) | | IsKeyReleased ( KEY_DOWN ) ) {
pad2 . velocity . y = 0 ;
}
/* Update ball velocity based on collision detection */
if ( pad1 . getRect ( ) . CheckCollision ( ball . pos , ball . radius ) ) { /* Collision with paddle 1 */
ball . pos . x = pad1 . getRect ( ) . x + pad1 . getRect ( ) . GetWidth ( ) + ball . radius + 1 ; /* Ensuring that the ball doesn't get stuck inside the paddle */
ball . vel = changeVelocityAfterCollision ( pad1 , ball ) ;
}
if ( pad2 . getRect ( ) . CheckCollision ( ball . pos , ball . radius ) ) { /* Collision with paddle 2 */
ball . pos . x = pad2 . getRect ( ) . x - ball . radius - 1 ;
ball . vel = changeVelocityAfterCollision ( pad2 , ball ) ;
}
if ( ball . pos . x + ball . radius > = window . GetWidth ( ) ) { /* Collision with right wall */
pad1 . incrementPoints ( ) ;
game_started = false ;
ball . reset ( ) ;
pad1 . reset ( ) ;
pad2 . reset ( ) ;
}
if ( ball . pos . x - ball . radius < = 0 ) { /* Collision with left wall */
pad2 . incrementPoints ( ) ;
game_started = false ;
ball . reset ( ) ;
pad1 . reset ( ) ;
pad2 . reset ( ) ;
}
if ( ball . pos . y - ball . radius < = 0 ) { /* Collision with top wall */
ball . pos . y = ball . radius + 1 ;
ball . vel . y = ball . vel . y * - 1 ;
}
if ( ball . pos . y + ball . radius > = window . GetHeight ( ) ) { /* Collision with bottom wall */
ball . pos . y = HEIGHT - ball . radius - 1 ;
ball . vel . y = ball . vel . y * - 1 ;
}
/* Update positions based on velocities */
pad1 . updatePosition ( ) ;
pad2 . updatePosition ( ) ;
ball . updatePosition ( ) ;
}
}
if ( pad2 . getRect ( ) . CheckCollision ( ball . pos , ball . radius ) ) { /* Collision with paddle 2 */
ball . pos . x = pad2 . getRect ( ) . x - ball . radius - 1 ;
ball . vel = changeVelocityAfterCollision ( pad2 , ball ) ;
}
if ( ball . pos . x + ball . radius > = window . GetWidth ( ) ) { /* Collision with right wall */
pad1 . incrementPoints ( ) ;
ball . pos = raylib : : Vector2 ( window . GetWidth ( ) / 2 , window . GetHeight ( ) / 2 ) ;
}
if ( ball . pos . x - ball . radius < = 0 ) { /* Collision with left wall */
pad2 . incrementPoints ( ) ;
ball . pos = raylib : : Vector2 ( window . GetWidth ( ) / 2 , window . GetHeight ( ) / 2 ) ;
}
if ( ball . pos . y - ball . radius < = 0 | | ball . pos . y + ball . radius > = window . GetHeight ( ) ) { /* Collision with top and bottom wals */
ball . vel . y = ball . vel . y * - 1 ;
}
/* Draw objects */
/* Draw objects */
window . BeginDrawing ( ) ;
window . BeginDrawing ( ) ;
window . ClearBackground ( BLACK ) ;
window . ClearBackground ( BLACK ) ;
points_str = std : : to_string ( pad1 . getPoints ( ) ) + " \t \t " + std : : to_string ( pad2 . getPoints ( ) ) ;
raylib : : Text : : Draw ( points_str , ( WIDTH / 2 ) - 30 , HEIGHT / 10 , 30 , raylib : : Color : : White ( ) ) ;
pad1 . draw ( ) ;
pad1 . draw ( ) ;
pad2 . draw ( ) ;
pad2 . draw ( ) ;
ball . draw ( ) ;
ball . draw ( ) ;