Browse Source

cancel game when second player joins

balancep0ng-improve-player-joining
Fr3deric 5 years ago
parent
commit
7f754c032b
  1. 39
      games/balancep0ng.py

39
games/balancep0ng.py

@ -130,6 +130,15 @@ class B4lancePong(object): @@ -130,6 +130,15 @@ class B4lancePong(object):
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
@ -141,13 +150,13 @@ class B4lancePong(object): @@ -141,13 +150,13 @@ class B4lancePong(object):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self.__balanceserver)
print('connected!')
bplayers = [
self.__bplayers = [
blup.balance_util.BalanceUtil(sock, 0),
blup.balance_util.BalanceUtil(sock, 1)
]
while not (bplayers[0].get_player_ready() or
bplayers[1].get_player_ready()):
while not (self.__bplayers[0].get_player_ready() or
self.__bplayers[1].get_player_ready()):
time.sleep(0.1)
for i in range(5):
@ -155,8 +164,8 @@ class B4lancePong(object): @@ -155,8 +164,8 @@ class B4lancePong(object):
self.__out.sendFrame(frame)
print('waiting for second player...')
if (bplayers[0].get_player_ready() and
bplayers[1].get_player_ready()):
if (self.__bplayers[0].get_player_ready() and
self.__bplayers[1].get_player_ready()):
break
time.sleep(1)
@ -164,9 +173,9 @@ class B4lancePong(object): @@ -164,9 +173,9 @@ class B4lancePong(object):
for i in [0, 1]:
paddle = self.__playground.leftPaddle if i == 0 else \
self.__playground.rightPaddle
if bplayers[i].get_player_ready():
if self.__bplayers[i].get_player_ready():
player = BalanceBoardPlayer(self.__playground, paddle,
bplayers[i])
self.__bplayers[i])
else:
player = pong.PongBot(self.__playground, paddle, 3)
self.__players.append(player)
@ -184,7 +193,10 @@ class B4lancePong(object): @@ -184,7 +193,10 @@ class B4lancePong(object):
self.__out.sendFrame(frame)
time.sleep(1)
while max(scoreLeft, scoreRight) < __MAX_SCORE__:
self.__playground.addGameTickCallback(self.gametickcb)
while (max(scoreLeft, scoreRight) < __MAX_SCORE__ and
not self.__playground.cancelled):
winner = self.__playground.play()
if winner is self.__playground.leftPaddle:
scoreLeft += 1
@ -192,13 +204,14 @@ class B4lancePong(object): @@ -192,13 +204,14 @@ class B4lancePong(object):
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 = []

Loading…
Cancel
Save