Bot somewhat smarter now ...

master
kaqu 2 years ago
parent 1630adf81b
commit ce393ba9de
  1. 53
      game_objects/pong_bot.py
  2. 4
      game_objects/pong_game.py
  3. BIN
      pictures/pong_background (copy).png
  4. BIN
      pictures/pong_background.png

@ -19,6 +19,29 @@ resenddelay = 0 # Don't send too many datagrams ...
back_to_neutral_y = False
lastcolor = 0 # No color assumed
def calculate_target(msg, x, y, delta_x, delta_y):
#print("Ball({0}): x={1:.0f} y={2:.0f} (delta_x={3:.0f} delta_y={4:.0f})".format(msg, x, y, delta_x, delta_y))
if delta_y < 0: # /\
ny = abs(y / delta_y)
elif delta_y > 0: # \/
ny = abs((pgc.GAMEAREA_MAX_Y - y) / delta_y)
if delta_x < 0: # <- (Bot #1)
nx = abs(x / delta_x)
else: # -> (Bot #2)
nx = abs((pgc.GAMEAREA_MAX_X - x) / delta_x)
if ny < nx: # Wall touch
#msg2 = "Touch!"
xt = x + delta_x * ny
yt = y + delta_y * ny
else: # Direct approach
#msg2 = "Direct"
xt = x + delta_x * nx
yt = y + delta_y * nx
#print("calculate_target({0}): xt={1:.0f} yt={2:.0f} (nx={3:.0f} ny={4:.0f}) {5}".format(msg, xt, yt, nx, ny, msg2))
return xt, yt
def bot_game_logic(ball, player):
"""Actual PLAY state player bot logic"""
global resenddelay, back_to_neutral_y, lastcolor
@ -32,7 +55,7 @@ def bot_game_logic(ball, player):
back_to_neutral_y = False
return 3, 127, 1 # Y-Axis Neutral
resenddelay = 10
resenddelay = 4 #10
# Infection control has higher priority
if abs(ball.x - player.x) < (pgc.GAMEAREA_MAX_X / 4): # Ball within range?
@ -75,6 +98,7 @@ def bot_game_logic(ball, player):
back_to_neutral_y = True
return 3, 0, 1 # Going up
else: # Ball approaching!
""" Old logic
if (ball.y + ball.h/2) > (player.y + 3*player.h/4): # Ball is below player?
#print("ball below, going down")
back_to_neutral_y = True
@ -83,7 +107,34 @@ def bot_game_logic(ball, player):
#print("ball above, going up")
back_to_neutral_y = True
return 3, 0, 1
"""
xt, yt = calculate_target("1", ball.x, ball.y, ball.delta_x, ball.delta_y) # Potential wall contact point calculation
if player.player_index == 1: # Bot 1
if xt <= pgc.GAMEAREA_MIN_X: # (d) Direct player approach
pass # Calculate crossing where x=0
else: # Wall contact (a) or (c)
if ball.delta_y != 0: # (c) > 0, (a) < 0
_, yt = calculate_target("2", xt, yt, ball.delta_x, -ball.delta_y) # Invert y movement
else: # Horizontal movement
yt = ball.y # Bot target height
else: # Bot #2
if xt >= pgc.GAMEAREA_MAX_X: # (b) Direct player approach
pass # Calculate crossing where x=xmax
else: # Wall contact (a) or (c)
if ball.delta_y != 0: # (c) > 0, (a) < 0
_, yt = calculate_target("2", xt, yt, ball.delta_x, -ball.delta_y) # Invert y movement
else: # Horizontal movement
yt = ball.y # Bot target height
if yt > (player.y + 3*player.h/4): # Target is below player?
#print("Target below, going down")
back_to_neutral_y = True
return 3, 255, 1
elif yt < (player.y + player.h/4): # Target is above player?
#print("Target above, going up")
back_to_neutral_y = True
return 3, 0, 1
return 0, 0, 0 # Nothing changed!
def run_bot(game_state, ball, player):

@ -207,11 +207,11 @@ class PongGame:
if rc != 0: # Miss left(-1) or right(1)
if rc == -1: # Player #2: +1
player2.score = player2.score + 1
ball.reinit(8 + (random() - 0.5) * 4, (random() - 0.5) * 10)
ball.reinit(10 + (random() - 0.5) * 4, (random() - 0.5) * 10)
self.playsound = pgc.PLAYERMISSSOUND
else: # Player #1: +1
player1.score = player1.score + 1
ball.reinit(-(8 + (random() - 0.5) * 4), (random() - 0.5) * 10)
ball.reinit(-(10 + (random() - 0.5) * 4), (random() - 0.5) * 10)
self.playsound = pgc.PLAYERMISSSOUND
player1.clear_UDP_queue() # Dry up queues ...

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Loading…
Cancel
Save