mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
fixed bot to work as subject tells
This commit is contained in:
parent
f68c4a65b6
commit
a5ffbe07d7
@ -49,6 +49,7 @@ class Game:
|
|||||||
self.bt1 = 0
|
self.bt1 = 0
|
||||||
self.bt2 = 0
|
self.bt2 = 0
|
||||||
self.start_time = datetime.now()
|
self.start_time = datetime.now()
|
||||||
|
self.future_ball_position = {'x': 390, 'y': 190}
|
||||||
|
|
||||||
async def start_game(self):
|
async def start_game(self):
|
||||||
print(f"- Game #{self.game_id} STARTED ({self.game_state['player1_name']} vs {self.game_state['player2_name']}) --- ({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):
|
async def game_loop(self):
|
||||||
print(" In the game loop..")
|
print(" In the game loop..")
|
||||||
x = 0
|
x = 59
|
||||||
while not self.ended:
|
while not self.ended:
|
||||||
if self.botgame:
|
if self.botgame:
|
||||||
x += 1
|
x += 1
|
||||||
if x == 60:
|
if x == 60:
|
||||||
await self.update_bot_position()
|
self.future_ball_position = await self.predict_ball_trajectory()
|
||||||
x = 0
|
x = 0
|
||||||
|
await self.update_bot_position()
|
||||||
await self.handle_pad_movement()
|
await self.handle_pad_movement()
|
||||||
await self.update_game_state()
|
await self.update_game_state()
|
||||||
await self.send_game_state()
|
await self.send_game_state()
|
||||||
await asyncio.sleep(1/60) # Around 60 FPS
|
await asyncio.sleep(1/60) # Around 60 FPS
|
||||||
|
|
||||||
async def update_bot_position(self):
|
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']
|
player2_position = self.game_state['player2_position']
|
||||||
|
|
||||||
# Adjusts bot position based on expected ball position
|
# Adjusts bot position based on expected ball position
|
||||||
if player2_position < target_y < player2_position + 80:
|
if player2_position < target_y < player2_position + 80:
|
||||||
pass #bot already placed
|
pass #bot already placed
|
||||||
elif player2_position < target_y:
|
elif player2_position < target_y:
|
||||||
#self.p2_mov = 1
|
self.p2_mov = 1
|
||||||
self.game_state['player2_position'] = min(player2_position + (50 * self.speed), 300)
|
#self.game_state['player2_position'] = min(player2_position + (50 * self.speed), 300)
|
||||||
elif player2_position + 80 > target_y:
|
elif player2_position + 80 > target_y:
|
||||||
#self.p2_mov = -1
|
self.p2_mov = -1
|
||||||
self.game_state['player2_position'] = max(player2_position - (50 * self.speed), 0)
|
#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_x = self.game_state['ball_position']['x']
|
||||||
future_y = self.game_state['ball_position']['y']
|
future_y = self.game_state['ball_position']['y']
|
||||||
velocity_x = self.game_state['ball_velocity']['x']
|
velocity_x = self.game_state['ball_velocity']['x']
|
||||||
velocity_y = self.game_state['ball_velocity']['y']
|
velocity_y = self.game_state['ball_velocity']['y']
|
||||||
|
|
||||||
for _ in range(steps):
|
for _ in range(steps):
|
||||||
future_x += velocity_x
|
future_x += velocity_x
|
||||||
if future_x <= 10:
|
if future_x <= 10:
|
||||||
@ -101,11 +101,9 @@ class Game:
|
|||||||
future_x = 790
|
future_x = 790
|
||||||
else:
|
else:
|
||||||
future_y += velocity_y
|
future_y += velocity_y
|
||||||
|
|
||||||
# Dealing with bounces off walls
|
# Dealing with bounces off walls
|
||||||
if future_y <= 10 or future_y >= 390:
|
if future_y <= 10 or future_y >= 390:
|
||||||
velocity_y = -velocity_y # Reverse the direction of vertical movement
|
velocity_y = -velocity_y # Reverse the direction of vertical movement
|
||||||
|
|
||||||
return {'x': future_x, 'y': future_y}
|
return {'x': future_x, 'y': future_y}
|
||||||
|
|
||||||
async def update_game_state(self):
|
async def update_game_state(self):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user