Minor streamlining, local gamepad fix

master
kaqu 2 years ago
parent 399151f29b
commit d554cdce9f
  1. 8
      game_objects/pong_player.py
  2. 32
      pandemic_pong.py

@ -30,7 +30,7 @@ def pickEventQueues():
try:
with open(l, "rb") as fd:
dev = libevdev.Device(fd)
print("Input device name: {}".format(dev.name)) # 'USB Gamepad'
#print("Input device name: '{}'".format(dev.name)) # 'USB Gamepad'
if dev.name == "USB Gamepad ":
sEventQueues[i] = l
iQueueIndex[i] = iQueue
@ -59,7 +59,7 @@ def print_event(e):
class PongPlayer:
"""The player consists of gamepad input & variables"""
def __init__(self, path, bThisIsTheServer, player_index, player_server, x, y, w, h):
def __init__(self, path, player_index, player_server, x, y, w, h):
# Gamepad init., either local, client or server
global UDP_PACKET_SIZE, serverUDP
@ -77,7 +77,7 @@ class PongPlayer:
self.bUseServer = True
else:
self.bUseServer = False
if (player_index == 1) and (bThisIsTheServer): # Is this THE server? (But init. only once!)
if (player_index == 1) and (pgv.bIsServer == True): # Is this THE server? (But init. only once!)
UDP_PACKET_SIZE = struct.calcsize(pgc.UDP_TRANSFER_FORMAT) # Size of receiver buffer
serverUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP Receiver
serverUDP.bind(("0.0.0.0", pgc.PANDEMIC_PONG_PORT)) # Whatever local address ...
@ -239,7 +239,7 @@ class PongPlayer:
self.color = color
self.delay = delay
else:
other.seqnum = seqnum
other_player.seqnum = seqnum
other_player.state = state
other_player.x = x
other_player.y = y

@ -66,13 +66,19 @@ if __name__ == '__main__':
debug_y = int(pgc.GAMEAREA_MAX_Y / 40)
else:
print(sMsg)
exit(1)
else:
print(sMsg)
print(sMsg)
exit(1)
else: # Local version
print("*** PANDEMIC PONG (local) ***")
sEventQueue1, sEventQueue2 = pickEventQueues() # Search 'GAMEPAD ' entries (1 or 2 connected?)
if (sEventQueue1 == None) or (sEventQueue2 == None):
if pgv.bIsLocal == True:
print("Local game requires 2 gamepads connected!")
exit(-1)
if pgc.DEVLOCAL == False:
print("I'm a slow starter, please be patient!") # Caching actually ...
try:
@ -82,23 +88,23 @@ if __name__ == '__main__':
ball = PongObject(pgc.GAMEAREA_MAX_X/2, pgc.GAMEAREA_MAX_Y/2, 20, 20, 8.0, 2.5)
if player_index == 0: # Local or viewer or server?
if pgv.bIsServer == True: # Server, no gamepads locally avail.
player1 = PongPlayer(None, True, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, True, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(None, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
elif pgv.bIsViewer == True: # Viewer, no gamepads locally avail.
player1 = PongPlayer(None, False, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, False, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(None, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
else: # Local version (both gamepads assumed locally connected)
player1 = PongPlayer(sEventQueue1, False, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(sEventQueue2, False, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(sEventQueue1, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(sEventQueue2, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
elif player_index == 1: # Client/server version, player #1, only one queue (the last one!) avail. ...
if pgc.DEVLOCAL == True:
player1 = PongPlayer(sEventQueue1, False, player_index, player_server, 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(sEventQueue1, player_index, player_server, 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
else:
player1 = PongPlayer(sEventQueue2, False, player_index, player_server, 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, False, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(sEventQueue2, player_index, player_server, 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(None, 2, "", pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
elif player_index == 2: # Client/server version, player #2, only one queue (the last one!) avail. anyway ...
player1 = PongPlayer(None, False, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(sEventQueue2, False, player_index, player_server, pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player1 = PongPlayer(None, 1, "", 10, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
player2 = PongPlayer(sEventQueue2, player_index, player_server, pgc.GAMEAREA_MAX_X-40, pgc.GAMEAREA_MAX_Y/2-50, 20, 160)
# TODO: Pretty ugly patch for handler data access ... (better: separate sender class)
vs.init_send_structures(game, player1, player2, ball)

Loading…
Cancel
Save