def __OnEventStartOfTurn(self, message) : """{"game": {"currentTimeSeconds":7.2}, "ball": {"position": {"x":52.239,"y":19.314}, "vector": {"x":-0.915, "y":-0.403}, "speed":14, "controllingPlayerNumber" :-1}, "team1":{"team":{"name":"","score":0,"direction":"RIGHT"}, "players":[{"staticState":{"playerNumber":0,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":25,"y":10},"hasBall":false,"energy":100,"direction":270}},{"staticState":{"playerNumber":1,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":25,"y":25},"hasBall":false,"energy":100,"direction":270}},{"staticState":{"playerNumber":2,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":25,"y":40},"hasBall":false,"energy":100,"direction":270}},{"staticState":{"playerNumber":3,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":73.068,"y":30.268},"hasBall":false,"energy":100,"direction":298.001}},{"staticState":{"playerNumber":4,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":72.957,"y":31.666},"hasBall":false,"energy":100,"direction":301.252}},{"staticState":{"playerNumber":5,"playerType":"G","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":4.977,"y":24.526},"hasBall":false,"energy":100,"direction":354.952}}]}, "team2":{"team":{"name":"","score":0,"direction":"LEFT"},"players":[{"staticState":{"playerNumber":6,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":81.198,"y":33.952},"hasBall":false,"energy":100,"direction":296.958}},{"staticState":{"playerNumber":7,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":78.653,"y":32.417},"hasBall":false,"energy":100,"direction":296.521}},{"staticState":{"playerNumber":8,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":76.46,"y":29.984},"hasBall":false,"energy":100,"direction":293.775}},{"staticState":{"playerNumber":9,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":25,"y":35},"hasBall":false,"energy":100,"direction":270}},{"staticState":{"playerNumber":10,"playerType":"P","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":25,"y":15},"hasBall":false,"energy":100,"direction":270}},{"staticState":{"playerNumber":11,"playerType":"G","kickingAbility":66.667,"runningAbility":66.667,"ballControlAbility":66.667,"tacklingAbility":66.667},"dynamicState":{"position":{"x":95.03,"y":24.454},"hasBall":false,"energy":100,"direction":5.975}}]}}""" self.__ball = Class.Ball(message['ball']) self.__iHaveBall = False x = 0 if self.DirectionRight else Class.Pitch.Width if Util.Distance( (x, Class.Pitch.GoalCentre), self.__ball.Position) <= Class.Pitch.GoalAreaRadius : self.__ballInMyGoalAreaTurns += 1 else : self.__ballInMyGoalAreaTurns = 0 if self.__ball.PlayerNbr is not None : self.__inControl = (self.__ball.PlayerNbr in self.MyPlayerNumbers) self.__iHaveBall = self.__inControl for team in ('team1', 'team2') : players = message[team]['players'] for p in players : static = p['staticState'] nbr = static['playerNumber'] pos = (p['dynamicState']['position']['x'], p['dynamicState']['position']['y'], p['dynamicState']['direction']) self.__positions[nbr] = pos if team != self.TeamTag : if nbr not in self.__opponentPlayers : self.__opponentPlayers[nbr] = Player(nbr, static['ballControlAbility'], static['kickingAbility'], static['runningAbility'], static['tacklingAbility']) self.__opponentPlayers[nbr].SetPosition(pos) players = message[self.TeamTag]['players'] for p in players : pos = (p['dynamicState']['position']['x'], p['dynamicState']['position']['y'], p['dynamicState']['direction']) pl = self.GetPlayer(p['staticState']['playerNumber']) pl.OnNewTurn(position = pos, hasBall = self.Ball.PlayerNbr == pl.Number) if self.__iHaveBall : pl = self.GetPlayer(self.Ball.PlayerNbr) self.__ball.SetPlayer(pl) self.__closestMyPlayerNbrToBall = self.ClosestPlayerNbrToBall(friendly = True) self.__closestOpponenetPlayerNbrToBall = self.ClosestPlayerNbrToBall(friendly = False) self.Log('Ball: %s' % str(self.__ball)) self.Log('In Control: %s' % str(self.__inControl))
import Class as C import pygame pygame.init() ball = C.Ball() player1 = C.Player() player2 = C.Player() player1.x = int(C.screenwidth - player1.width) player2.x = 0 C.win while C.run: pygame.time.delay(10) for event in pygame.event.get(): if event.type == pygame.QUIT: C.run = False keys = pygame.key.get_pressed() font = pygame.font.Font(None, 36) # player 1 (Right) if keys[pygame.K_UP] and player1.y > 0: player1.y -= player1.vel if keys[pygame.K_DOWN] and player1.y < (C.screenheight - player1.height): player1.y += player1.vel # player 2 (Left)