Browse Source

Merge branch 'balancep0ng-improve-player-joining'

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

79
games/balancep0ng.py

@ -117,7 +117,7 @@ class BalanceBoardPlayer(object): @@ -117,7 +117,7 @@ class BalanceBoardPlayer(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.__out = out
self.__color = color
@ -125,13 +125,21 @@ class B4lancePong(object): @@ -125,13 +125,21 @@ class B4lancePong(object):
self.__balanceserver = ('localhost', 4711)
else:
self.__balanceserver = balanceserver
self.__numplayers = numplayers
if color:
self.__dimension = blup.frame.FrameDimension(22, 16, 256, 3)
else:
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):
print('starting a game...')
scoreLeft = 0
@ -143,27 +151,35 @@ class B4lancePong(object): @@ -143,27 +151,35 @@ class B4lancePong(object):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self.__balanceserver)
print('connected!')
balance_util_p1 = blup.balance_util.BalanceUtil(sock, 0)
balance_util_p2 = blup.balance_util.BalanceUtil(sock, 1)
while not balance_util_p1.get_player_ready():
self.__bplayers = [
blup.balance_util.BalanceUtil(sock, 0),
blup.balance_util.BalanceUtil(sock, 1)
]
while not (self.__bplayers[0].get_player_ready() or
self.__bplayers[1].get_player_ready()):
time.sleep(0.1)
self.__players = []
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, balance_util_p1))
for i in range(5):
frame = mk_logo_frame(self.__dimension, onePlayer)
self.__out.sendFrame(frame)
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):
frame = mk_logo_frame(self.__dimension, onePlayer)
self.__out.sendFrame(frame)
print('waiting for second player...')
if (self.__bplayers[0].get_player_ready() and
self.__bplayers[1].get_player_ready()):
break
time.sleep(1)
print('waiting for second player...')
if balance_util_p2.get_player_ready():
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle,
balance_util_p2))
break
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)
self.__out.sendFrame(frame)
@ -178,25 +194,25 @@ class B4lancePong(object): @@ -178,25 +194,25 @@ class B4lancePong(object):
self.__out.sendFrame(frame)
time.sleep(1)
if len(self.__players) == 1:
bot = pong.PongBot(self.__playground, self.__playground.rightPaddle, 3)
self.__players.append(bot)
self.__playground.addGameTickCallback(self.gametickcb)
while max(scoreLeft, scoreRight) < __MAX_SCORE__:
while (max(scoreLeft, scoreRight) < __MAX_SCORE__ and
not self.__playground.cancelled):
winner = self.__playground.play()
if winner is self.__players[0].ownPaddle:
if winner is self.__playground.leftPaddle:
scoreLeft += 1
else:
scoreRight += 1
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 3000)
for i in range(3):
if not self.__playground.cancelled:
for i in range(3):
frame = blup.frame.Frame(self.__dimension)
self.__out.sendFrame(frame)
time.sleep(0.5)
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 500)
frame = blup.frame.Frame(self.__dimension)
self.__out.sendFrame(frame)
time.sleep(0.5)
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 500)
frame = blup.frame.Frame(self.__dimension)
self.__out.sendFrame(frame)
self.__playground = None
self.__players = []
@ -205,8 +221,6 @@ class B4lancePong(object): @@ -205,8 +221,6 @@ class B4lancePong(object):
if __name__ == '__main__':
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,
metavar='HOST:PORT', help='use balance input')
parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT',
@ -216,7 +230,6 @@ if __name__ == '__main__': @@ -216,7 +230,6 @@ if __name__ == '__main__':
out = blup.output.getOutput(args.out)
bhost, bport = args.balance.split(':')
p0ng = B4lancePong(out, balanceserver=(bhost, int(bport)), color=True,
numplayers=args.players)
p0ng = B4lancePong(out, balanceserver=(bhost, int(bport)), color=True)
p0ng.runGame()

7
games/pong.py

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

Loading…
Cancel
Save