Sync ok (finally), bot improved ( still primitive)

master
kaqu 2 years ago
parent 3032c89af5
commit 1630adf81b
  1. 4
      README.md
  2. 39
      game_objects/pong_bot.py
  3. 5
      game_objects/pong_player.py
  4. 12
      game_objects/pong_viewer.py

@ -118,7 +118,7 @@ It is possible to have bots as players! Use
./pp_player1_bot.sh <remote_host_ip_or_name>
for (left) player #1 or
for (left) player #1 and/or
./pp_player2_bot.sh <remote_host_ip_or_name>
@ -171,8 +171,6 @@ After some 2-5s this effect will disappear. Currently, there is no clean initial
## 4. Outlook ##
* Streaming synchronization still needs improvement.
* Maybe HTTP streaming shall be moved to raw TCP streaming ...
* Game mechanics: Maybe introduce an incubation time?

@ -28,28 +28,18 @@ def bot_game_logic(ball, player):
return 0, 0, 0
if back_to_neutral_y: # Reset/unpress y-axis movement
print("Reset y-axis to neutral")
#print("Reset y-axis to neutral")
back_to_neutral_y = False
return 3, 127, 1 # Y-Axis Neutral
resenddelay = 10
# Controlling vertical miss first:
if (ball.y + ball.h/2) > (player.y + 2*player.h/3): # Ball is below player?
print("ball below, going down")
back_to_neutral_y = True
return 3, 255, 1
elif (ball.y + ball.h/2) < (player.y + player.h/3): # Ball is above player?
print("ball above, going up")
back_to_neutral_y = True
return 3, 0, 1
# If Y axis is good, check on x axis
if abs(ball.x - player.x) < 100: # Ball within range?
# Infection control has higher priority
if abs(ball.x - player.x) < (pgc.GAMEAREA_MAX_X / 4): # Ball within range?
if lastcolor == pgc.COL_WHITE: # Code selection permissible?
if ball.color == pgc.COL_WHITE: # Ball not already infected?
color = randrange(288,291) # Color button codes
print("---------------- Infecting with {} ".format(color))
#print("---------------- Infecting with {} ".format(color))
lastcolor = color - 287
if color == 288:
lastcolor = pgc.COL_BLUE
@ -68,13 +58,32 @@ def bot_game_logic(ball, player):
color = 290
else: # Green
color = 291
print("---------------- Syncing color {}".format(color))
#print("---------------- Syncing color {}".format(color))
lastcolor = ball.color
resenddelay = 4
return 1, 1, color
elif abs(ball.x - player.x) > (pgc.GAMEAREA_MAX_X / 2): # Ball on other side?
lastcolor = False # Permit new color selection
# Controlling vertical miss:
if ((ball.delta_x > 0) and (player.player_index == 1)) or ((ball.delta_x < 0) and (player.player_index == 2)):
# Ball going away, return to vertical center
if (player.y + 3 * player.h / 4) < (pgc.GAMEAREA_MAX_Y / 2): # player above center
back_to_neutral_y = True
return 3, 255, 1 # Going down
elif (player.y + 1 * player.h / 4) > (pgc.GAMEAREA_MAX_Y / 2): # player below center
back_to_neutral_y = True
return 3, 0, 1 # Going up
else: # Ball approaching!
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
return 3, 255, 1
elif (ball.y + ball.h/2) < (player.y + player.h/4): # Ball is above player?
#print("ball 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):

@ -223,8 +223,11 @@ class PongPlayer:
return bChanged
def clear_UDP_queue(self):
"""Server side clearing function"""
try: # Clear UDP queue of remainders ...
data, addr = serverUDP.recvfrom(UDP_PACKET_SIZE)
while True: # Breaks by queue empty (or broken) ...
data, addr = serverUDP.recvfrom(UDP_PACKET_SIZE)
except BlockingIOError: # Until no more packets ...
pass

@ -99,12 +99,14 @@ class pongWindow(QMainWindow):
playsound(pgc.sounds[pgc.WALLCONTACTSOUND])
playsound(pgc.sounds[pgc.GAMEEXITSOUND])
time.sleep(0.5) # Play sounds ...
playsound(pgc.sounds[pgc.GAMESPLASHSOUND])
playsound(pgc.sounds[pgc.GAMESPLASHSOUND])
self.timer = QTimer() # Start processing 'loop'
self.timer.setInterval(25) # 25ms
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.on_timer)
self.timer.timeout.connect(self.on_timer)
while self.viewserver.receive_data() == 1: # Client side: Empty receive queue (to sync. faster!)
pass
self.timer.start()
def paintEvent(self, event):
@ -141,9 +143,7 @@ class pongWindow(QMainWindow):
painter.drawText(int((pgc.GAMEAREA_MAX_X / 2 + 100) * scale_x), int(pgc.GAMEAREA_MAX_Y / 5 * scale_y), "Press [color] button")
painter.drawText(int((pgc.GAMEAREA_MAX_X / 2 + 100) * scale_x), int(pgc.GAMEAREA_MAX_Y / 5 * 2 * scale_y), "to infect player,")
painter.drawText(int((pgc.GAMEAREA_MAX_X / 2 + 100) * scale_x), int(pgc.GAMEAREA_MAX_Y / 5 * 3 * scale_y), "view will pickup virus!")
painter.drawText(int((pgc.GAMEAREA_MAX_X / 2 + 100) * scale_x), int(pgc.GAMEAREA_MAX_Y / 5 * 4 * scale_y), "Match color to deflect!")
while self.viewserver.receive_data() == 1: # Empty receive queue (to sync. faster!)
pass
painter.drawText(int((pgc.GAMEAREA_MAX_X / 2 + 100) * scale_x), int(pgc.GAMEAREA_MAX_Y / 5 * 4 * scale_y), "Match color to deflect!")
elif self.game.state == pgc.STATE_PLAY: # Actual play 'til score reaches 10 points
# Ball gives NO indication ...
painter.drawRect(int(self.ball.x * scale_x), int(self.ball.y * scale_y), int(self.ball.w * scale_x), int(self.ball.h * scale_y)) # Ball

Loading…
Cancel
Save