From b3ce5cb09afee527e84dcf12dede6ec829023088 Mon Sep 17 00:00:00 2001 From: Chris Proctor Date: Thu, 18 Dec 2025 16:43:57 -0500 Subject: [PATCH] Implemented ball-paddle collision. I decided to have the ball check for collision with the paddle instead of having the paddle check for collision with the ball. Either way could work, but it seemed easier to have the ball do the checking since the ball will need to respond with a change in velocity anyway. --- ball.py | 15 ++++++++++++++- paddle.py | 5 ----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ball.py b/ball.py index 41b1f85..7e824af 100644 --- a/ball.py +++ b/ball.py @@ -1,6 +1,7 @@ # ball.py class Ball: character = "O" + name = "ball" def __init__(self, position): self.position = position @@ -16,5 +17,17 @@ class Ball: self.velocity = (-vx, vy) elif new_y < 0 or board_y <= new_y: self.velocity = (vx, -vy) + elif self.check_for_collision_with_paddle((new_x, new_y), game): + self.velocity = (vx, -vy) else: - self.position = (new_x, new_y) \ No newline at end of file + self.position = (new_x, new_y) + + def check_for_collision_with_paddle(self, position, game): + """Checks whether the given position collides with a piece of the paddle. + We pass a position rather than using self.position so that we can check + potential future positions. + """ + for agent in game.get_agents_by_position()[position]: + if not isinstance(agent, Ball): + return True + diff --git a/paddle.py b/paddle.py index 75d2e8b..3afc6de 100644 --- a/paddle.py +++ b/paddle.py @@ -15,11 +15,6 @@ class Paddle: def play_turn(self, game): if not self.pieces: self.create_pieces(game) - check_collision() - - def check_collision: - x, y = self.position - print (retro.game.Game.get_agent_by_name(ball)) def handle_keystroke(self, keystroke, game): x, y = self.position