Browse Source

Merge branch 'balancep0ng-improve-player-joining'

master
Fr3deric 5 years ago
parent
commit
44dfe58d61
  1. 59
      games/balancep0ng.py
  2. 7
      games/pong.py

59
games/balancep0ng.py

@ -117,7 +117,7 @@ class BalanceBoardPlayer(object):
class B4lancePong(object): class B4lancePong(object):
def __init__(self, out, balanceserver=None, color=True, numplayers=None): def __init__(self, out, balanceserver=None, color=True):
self.__playground = None self.__playground = None
self.__out = out self.__out = out
self.__color = color self.__color = color
@ -125,13 +125,21 @@ class B4lancePong(object):
self.__balanceserver = ('localhost', 4711) self.__balanceserver = ('localhost', 4711)
else: else:
self.__balanceserver = balanceserver self.__balanceserver = balanceserver
self.__numplayers = numplayers
if color: if color:
self.__dimension = blup.frame.FrameDimension(22, 16, 256, 3) self.__dimension = blup.frame.FrameDimension(22, 16, 256, 3)
else: else:
self.__dimension = blup.frame.FrameDimension(18, 8, 2, 1) self.__dimension = blup.frame.FrameDimension(18, 8, 2, 1)
def gametickcb(self):
for i, p in enumerate(self.__players):
if isinstance(p, pong.PongBot):
print('bot in game')
if (isinstance(p, pong.PongBot) and \
self.__bplayers[i].get_player_ready()):
print('second player joined, cancelling current game')
self.__playground.cancel()
def runGame(self): def runGame(self):
print('starting a game...') print('starting a game...')
scoreLeft = 0 scoreLeft = 0
@ -143,28 +151,36 @@ class B4lancePong(object):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self.__balanceserver) sock.connect(self.__balanceserver)
print('connected!') print('connected!')
balance_util_p1 = blup.balance_util.BalanceUtil(sock, 0) self.__bplayers = [
balance_util_p2 = blup.balance_util.BalanceUtil(sock, 1) blup.balance_util.BalanceUtil(sock, 0),
while not balance_util_p1.get_player_ready(): blup.balance_util.BalanceUtil(sock, 1)
time.sleep(0.1) ]
self.__players = [] while not (self.__bplayers[0].get_player_ready() or
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, balance_util_p1)) self.__bplayers[1].get_player_ready()):
time.sleep(0.1)
if self.__numplayers == 2:
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, balance_util_p2))
elif self.__numplayers is None:
for i in range(5): for i in range(5):
frame = mk_logo_frame(self.__dimension, onePlayer) frame = mk_logo_frame(self.__dimension, onePlayer)
self.__out.sendFrame(frame) self.__out.sendFrame(frame)
print('waiting for second player...') print('waiting for second player...')
if balance_util_p2.get_player_ready(): if (self.__bplayers[0].get_player_ready() and
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, self.__bplayers[1].get_player_ready()):
balance_util_p2))
break break
time.sleep(1) time.sleep(1)
self.__players = []
for i in [0, 1]:
paddle = self.__playground.leftPaddle if i == 0 else \
self.__playground.rightPaddle
if self.__bplayers[i].get_player_ready():
player = BalanceBoardPlayer(self.__playground, paddle,
self.__bplayers[i])
else:
player = pong.PongBot(self.__playground, paddle, 3)
self.__players.append(player)
frame = mk_logo_frame(self.__dimension, logo) frame = mk_logo_frame(self.__dimension, logo)
self.__out.sendFrame(frame) self.__out.sendFrame(frame)
time.sleep(2) time.sleep(2)
@ -178,18 +194,18 @@ class B4lancePong(object):
self.__out.sendFrame(frame) self.__out.sendFrame(frame)
time.sleep(1) time.sleep(1)
if len(self.__players) == 1: self.__playground.addGameTickCallback(self.gametickcb)
bot = pong.PongBot(self.__playground, self.__playground.rightPaddle, 3)
self.__players.append(bot)
while max(scoreLeft, scoreRight) < __MAX_SCORE__: while (max(scoreLeft, scoreRight) < __MAX_SCORE__ and
not self.__playground.cancelled):
winner = self.__playground.play() winner = self.__playground.play()
if winner is self.__players[0].ownPaddle: if winner is self.__playground.leftPaddle:
scoreLeft += 1 scoreLeft += 1
else: else:
scoreRight += 1 scoreRight += 1
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 3000) pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 3000)
if not self.__playground.cancelled:
for i in range(3): for i in range(3):
frame = blup.frame.Frame(self.__dimension) frame = blup.frame.Frame(self.__dimension)
self.__out.sendFrame(frame) self.__out.sendFrame(frame)
@ -205,8 +221,6 @@ class B4lancePong(object):
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Blinkenbunt Pong!') parser = argparse.ArgumentParser(description='Blinkenbunt Pong!')
parser.add_argument('--players', dest='players', type=int,
help='number of players (autodetect if unspecified)')
parser.add_argument('--balance', dest='balance', type=str, parser.add_argument('--balance', dest='balance', type=str,
metavar='HOST:PORT', help='use balance input') metavar='HOST:PORT', help='use balance input')
parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT', parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT',
@ -216,7 +230,6 @@ if __name__ == '__main__':
out = blup.output.getOutput(args.out) out = blup.output.getOutput(args.out)
bhost, bport = args.balance.split(':') bhost, bport = args.balance.split(':')
p0ng = B4lancePong(out, balanceserver=(bhost, int(bport)), color=True, p0ng = B4lancePong(out, balanceserver=(bhost, int(bport)), color=True)
numplayers=args.players)
p0ng.runGame() p0ng.runGame()

7
games/pong.py

@ -183,6 +183,8 @@ class Playground(object):
paddleRight = Paddle(self, width - 1, (height - paddlesize)//2, paddlesize) paddleRight = Paddle(self, width - 1, (height - paddlesize)//2, paddlesize)
self.__paddles = [paddleLeft, paddleRight] self.__paddles = [paddleLeft, paddleRight]
self.cancelled = False
self.__ball = Ball(self, 0, 0, 1, 1) self.__ball = Ball(self, 0, 0, 1, 1)
self.__gameTickCallbacks = [] self.__gameTickCallbacks = []
self.__newRoundCallbacks = [] self.__newRoundCallbacks = []
@ -227,6 +229,9 @@ class Playground(object):
if paddle.containsPoint(x, y): if paddle.containsPoint(x, y):
return paddle return paddle
def cancel(self):
self.cancelled = True
def play(self, serve=None): def play(self, serve=None):
leftPaddle = self.__paddles[0] leftPaddle = self.__paddles[0]
rightPaddle = self.__paddles[1] rightPaddle = self.__paddles[1]
@ -250,7 +255,7 @@ class Playground(object):
callback() callback()
ticks = 0 ticks = 0
while True: while not self.cancelled:
ticks += 1 ticks += 1
time.sleep(0.08) time.sleep(0.08)
if ticks %2 == 0: if ticks %2 == 0:

Loading…
Cancel
Save