I recently started to learn pygame and python is my first ever programming language. I am trying to make classic ping pong as my first ever project. I think this doubt might sound stupid to good programmers but I would like to know how I can find the angle at which the ball hits the wall/paddle and how I could bounce it off at the realistic angle.
if self.paddle1_x + self.paddle_width >= self.ball_x >= self.paddle1_x and self.paddle1_y <= self.ball_y <= self.paddle1_y + self.paddle_height:
self.b_dx = -self.b_dx
if self.paddle2_x <= self.ball_x + self.ball_radius <= self.paddle2_x + self.paddle_width and self.paddle2_y <= self.ball_y <= self.paddle2_y + self.paddle_height:
self.b_dx = -self.b_dx
This is my current code, dir_x either being 1 or -1. The problem is that this makes the ball's movement repetitive and it only takes certain paths around the screen.
I was wondering whether there is an easy way to randomize maybe the angles at which the ball gets sent at the start and whether there was an easy way to make the ball bounce off the paddle depending on which way the paddle was going or how fast the paddle was going (for now the paddle has a certain speed but i also wanted to add a speed-up power up).
I recently started to learn pygame and python is my first ever programming language. I am trying to make classic ping pong as my first ever project. I think this doubt might sound stupid to good programmers but I would like to know how I can find the angle at which the ball hits the wall/paddle and how I could bounce it off at the realistic angle.
if self.paddle1_x + self.paddle_width >= self.ball_x >= self.paddle1_x and self.paddle1_y <= self.ball_y <= self.paddle1_y + self.paddle_height:
self.b_dx = -self.b_dx
if self.paddle2_x <= self.ball_x + self.ball_radius <= self.paddle2_x + self.paddle_width and self.paddle2_y <= self.ball_y <= self.paddle2_y + self.paddle_height:
self.b_dx = -self.b_dx
This is my current code, dir_x either being 1 or -1. The problem is that this makes the ball's movement repetitive and it only takes certain paths around the screen.
I was wondering whether there is an easy way to randomize maybe the angles at which the ball gets sent at the start and whether there was an easy way to make the ball bounce off the paddle depending on which way the paddle was going or how fast the paddle was going (for now the paddle has a certain speed but i also wanted to add a speed-up power up).
This is not at all the only way to do it, but an easy way to add some better angles to your game would be to calculate the up/down velocity of the ball based on which part of the paddle it hit from.
Something like, if the ball hit the paddle at the very top, it will reflect with a 60-ish degree angle upward, and 60-ish degree angle downward at the very bottom. If you hit the ball at the very center of the paddle it will go perfectly horizontal.
To do that, you could do the following:
Calculate which point of the paddle touched the ball:
paddle_contact_ratio = (self.ball_y - self.paddle_y) / self.paddle_height
This gives you a number between 0
and 1
, 0
being top edge, and 1
being bottom edge.
Calculate your ball_dy
scaled between -2 * ball_dx
and 2 * ball_dx
self.ball_dx = -self.ball_dx
self.ball_dy = (-2 + 4*paddle_contact_ratio) * self.ball_dx