|
|
|
@ -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,28 +151,36 @@ class B4lancePong(object):
@@ -143,28 +151,36 @@ 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(): |
|
|
|
|
time.sleep(0.1) |
|
|
|
|
self.__bplayers = [ |
|
|
|
|
blup.balance_util.BalanceUtil(sock, 0), |
|
|
|
|
blup.balance_util.BalanceUtil(sock, 1) |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
self.__players = [] |
|
|
|
|
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, balance_util_p1)) |
|
|
|
|
while not (self.__bplayers[0].get_player_ready() or |
|
|
|
|
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): |
|
|
|
|
frame = mk_logo_frame(self.__dimension, onePlayer) |
|
|
|
|
self.__out.sendFrame(frame) |
|
|
|
|
|
|
|
|
|
print('waiting for second player...') |
|
|
|
|
if balance_util_p2.get_player_ready(): |
|
|
|
|
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, |
|
|
|
|
balance_util_p2)) |
|
|
|
|
if (self.__bplayers[0].get_player_ready() and |
|
|
|
|
self.__bplayers[1].get_player_ready()): |
|
|
|
|
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) |
|
|
|
|
time.sleep(2) |
|
|
|
@ -178,18 +194,18 @@ class B4lancePong(object):
@@ -178,18 +194,18 @@ 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) |
|
|
|
|
|
|
|
|
|
if not self.__playground.cancelled: |
|
|
|
|
for i in range(3): |
|
|
|
|
frame = blup.frame.Frame(self.__dimension) |
|
|
|
|
self.__out.sendFrame(frame) |
|
|
|
@ -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() |
|
|
|
|
|
|
|
|
|