From a5ffbe07d7c9e4a4871007358c6dfc08e44134b1 Mon Sep 17 00:00:00 2001 From: CHIBOUB Chakib Date: Wed, 11 Sep 2024 15:08:40 +0200 Subject: [PATCH] fixed bot to work as subject tells --- pong/game/game.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pong/game/game.py b/pong/game/game.py index a5c9320..8764a34 100644 --- a/pong/game/game.py +++ b/pong/game/game.py @@ -49,6 +49,7 @@ class Game: self.bt1 = 0 self.bt2 = 0 self.start_time = datetime.now() + self.future_ball_position = {'x': 390, 'y': 190} async def start_game(self): print(f"- Game #{self.game_id} STARTED ({self.game_state['player1_name']} vs {self.game_state['player2_name']}) --- ({self})") @@ -57,41 +58,40 @@ class Game: async def game_loop(self): print(" In the game loop..") - x = 0 + x = 59 while not self.ended: if self.botgame: x += 1 if x == 60: - await self.update_bot_position() + self.future_ball_position = await self.predict_ball_trajectory() x = 0 + await self.update_bot_position() await self.handle_pad_movement() await self.update_game_state() await self.send_game_state() await asyncio.sleep(1/60) # Around 60 FPS async def update_bot_position(self): - future_ball_position = self.predict_ball_trajectory() + #future_ball_position = self.predict_ball_trajectory() - target_y = future_ball_position['y'] + target_y = self.future_ball_position['y'] player2_position = self.game_state['player2_position'] # Adjusts bot position based on expected ball position if player2_position < target_y < player2_position + 80: pass #bot already placed elif player2_position < target_y: - #self.p2_mov = 1 - self.game_state['player2_position'] = min(player2_position + (50 * self.speed), 300) + self.p2_mov = 1 + #self.game_state['player2_position'] = min(player2_position + (50 * self.speed), 300) elif player2_position + 80 > target_y: - #self.p2_mov = -1 - self.game_state['player2_position'] = max(player2_position - (50 * self.speed), 0) + self.p2_mov = -1 + #self.game_state['player2_position'] = max(player2_position - (50 * self.speed), 0) - def predict_ball_trajectory(self, steps=60): - + async def predict_ball_trajectory(self, steps=60): future_x = self.game_state['ball_position']['x'] future_y = self.game_state['ball_position']['y'] velocity_x = self.game_state['ball_velocity']['x'] velocity_y = self.game_state['ball_velocity']['y'] - for _ in range(steps): future_x += velocity_x if future_x <= 10: @@ -101,11 +101,9 @@ class Game: future_x = 790 else: future_y += velocity_y - # Dealing with bounces off walls if future_y <= 10 or future_y >= 390: velocity_y = -velocity_y # Reverse the direction of vertical movement - return {'x': future_x, 'y': future_y} async def update_game_state(self):