def __init__(self, number, title, command, server_log): PlayerInfo.__init__(self, number) self.steps = 0 self.total_step_time = 0 self.max_step_time = 0 self.title = title self.msg = '' self.server_log = server_log self.points = [] self.process = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) self.stderr_reader = StreamReader(self.process.stderr) self.stderr_reader.start()
def move(self, x0, y0, x1, y1): """Add logging to moving.""" PlayerInfo.move(self, x0, y0, x1, y1) if self.points: if self.points[-1] != (x1, y1): self.points.append((x1, y1)) else: if (x0, y0) == (x1, y1): self.points = [(x1, y1)] else: self.points = [(x0, y0), (x1, y1)] self.log('New coordinates: {} {} {} {}'.format(x0, y0, x1, y1))
def getTeamInfo(team_info, formatText=formatHTML): '''takes team information and generates a team object''' #Put player names in array player_names = team_info.find_elements_by_class_name( "champion-nameplate-name") player_names = list(map(formatText, player_names)) #Extract team information result_raw = team_info.find_element_by_class_name("game-conclusion") gold_raw = team_info.find_element_by_class_name("gold") kills_raw = team_info.find_element_by_class_name("kills") towers_raw = team_info.find_element_by_class_name("tower-kills") inhibs_raw = team_info.find_element_by_class_name("inhibitor-kills") barons_raw = team_info.find_element_by_class_name("baron-kills") dragons_raw = team_info.find_element_by_class_name("dragon-kills") rift_herald_raw = team_info.find_element_by_class_name("rift-herald-kills") result = formatText(result_raw) == "VICTORY" gold = float(formatText(gold_raw).strip("k")) * 1000 kills = float(formatText(kills_raw)) towers = int(formatText(towers_raw)) inhibs = int(formatText(inhibs_raw)) barons = int(formatText(barons_raw)) dragons = int(formatText(dragons_raw)) rift_herald = int(formatText(rift_herald_raw)) team_name = player_names[0].split(" ")[0] team = Team(team_name, result, gold, kills, towers, inhibs, barons, dragons, rift_herald) #Extract player names players = [] for player in player_names: #removes teamname from name name = " ".join(player.split(" ")[1:]) players.append(PlayerInfo(name)) team.players = players return team
def handle_input(self): self.players_count, self.my_number = self.read_numbers() for i in xrange(self.players_count): x0, y0, x1, y1 = self.read_numbers() if i not in self.players: self.players[i] = PlayerInfo(i) if x0 != -1: self.grid.put(x0, y0, self.grid.body_of(i)) else: # replace old head with body old_head = self.players[i].head self.grid.put(old_head[0], old_head[1], self.grid.body_of(i)) player = self.players[i] player.move(x0, y0, x1, y1) if player.is_alive: self.grid.put(x1, y1, self.grid.head_of(i)) else: self.remove_player(i)
def run_game_stage_play(): topScorer = True server_communicate = True client_player_info =[] hitLoadButton = HitLoadButton(playerLog[5], playerLog[6]) # Ready to connect to server net = Network() print("receiving: ", net.init_info) # if no connection to server if not net.init_info: draw_no_connection() server_communicate = False game_stage = "name" if server_communicate == True: # initial info from server: game ID, player_ID, pos_x, pos_y, icon_num init_player_info = split_init_info(net.init_info) game_ID = int(init_player_info[0]) pygame.display.set_caption("Connected to Room "+ str(game_ID)) client_player_ID = int(init_player_info[1]) pos_x = int(init_player_info[2]) pos_y = int(init_player_info[3]) icon_num = int(init_player_info[4]) icon_info = getIcon(icon_num) # create client Player object client_player = Player(client_player_ID, nameButton.name, pos_x, pos_y, icon_info[0], icon_info[1], icon_num,[], 0, int(playerLog[4]), playerLog[5], playerLog[6]) # create client PlayerInfo object client_player_info = PlayerInfo(client_player,[]) run = True # check keyboard & mouse actions hitType = "NONE" hitBool = 1 rapid_hit = False while run: obtained_potion_IDs=[] clock.tick(SCREEN_UPDATE_CLOCK_TICK_GAME) # getting the new screen dimensions update_display_dimensions(pygame.display) # sending the new player list from server while sending the updated player if client_player_info.health <= 0: game_stage = "game_over" run = False else: try: net.send(client_player_info) except: draw_no_connection() game_stage = "stat_info" run = False net.close() break # receiving the new player list info from server try: game = net.receive() except: draw_no_connection() game_stage = "game_over" net.close() run = False break playerInfoList = game.playerInfoList if not playerInfoList: draw_no_connection() game_stage = "game_over" run = False break # updating the current player for info in playerInfoList: if info.id == client_player_ID: client_player.copy(info) players = [] players.append(client_player) for info in playerInfoList: if info.id != client_player_ID: icon_info = getIcon(info.icon_num) other_player = Player(info.id, info.name, info.x, info.y, icon_info[0], icon_info[1], icon_num, info.hitList, info.score, info.health, PUNCH_WAIT, KICK_WAIT) players.append(other_player) events = pygame.event.get() for event in events: if event.type == pygame.QUIT: run = False game_stage = "closing" net.close() break # checking mouse clicks for updating sound buttons & sounds if event.type == pygame.MOUSEBUTTONDOWN: mx, my = pygame.mouse.get_pos() SoundButtons.mouseAction(mx, my) exitGameButton.mouseAction(mx, my) leaveRoomButton.mouseAction(mx,my) if exitGameButton.clicked: net.close() game_stage = "closing" run = False if leaveRoomButton.clicked: net.close() game_stage = "game_over" leaveRoomButton.clicked = False run = False # checking keyboard inputs for hitting if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: hitType = "PUNCH" if event.key == pygame.K_s: hitType = "KICK" if event.type == pygame.KEYUP: if event.key == pygame.K_a: hitBool = 1 if event.key == pygame.K_s: hitBool = 1 if event.key == pygame.K_w: topScorer = not topScorer if hitBool == 0: hitType = "NONE" # updating player location : keyboard input checks are done inside the function client_player.move(MOVE_TIME_WAIT, FRAMED_WIN_DIMENSIONS[0], FRAMED_WIN_DIMENSIONS[1], FRAME_WIDTH, PLAYER_ICON_WIDTH, ZERO_DIVIDE_THRESHOLD) # check if player gets any potion client_rect = client_player.getPlayerRect() for potion in game.potionList: potion_rect = health_potion_img.get_rect(topleft=potion[1]) getPotion = client_rect.colliderect(potion_rect) if getPotion: obtained_potion_IDs.append(potion[2]) if potion[0] == "health": client_player.health += 3 if potion[0] == "rapid": start_counter = time.perf_counter() rapid_hit = True if rapid_hit == True: if time.perf_counter()-start_counter > 10: rapid_hit = False client_player.minPunchWait = PUNCH_WAIT client_player.minKickWait = KICK_WAIT hitLoadButton.punchWait = PUNCH_WAIT hitLoadButton.kickWait = KICK_WAIT else: client_player.minPunchWait = PUNCH_WAIT/2 client_player.minKickWait = KICK_WAIT/2 hitLoadButton.punchWait = PUNCH_WAIT/2 hitLoadButton.kickWait = KICK_WAIT/2 # reset the hit list of client player & get the hit_info: (did_player_hit, the Rect object of hitting) client_player.resetHitList() hit_info = client_player.hit(hitType, game_sound_mixer, hit_try_music, punch_img, kick_img, PUNCH_ICON_WIDTH, KICK_ICON_WIDTH, PLAYER_ICON_WIDTH, ZERO_DIVIDE_THRESHOLD) if hit_info[0] == "YES": hit_rect = hit_info[1] #update hit load buttons hitLoadButton.lastHitTime = client_player.lastHitTime hitLoadButton.updateHitLoad(time.perf_counter()) # update player health if other players hit matches the client ID # update the hit list of the client (other player IDs & damage performed ) # based on collision check of client hit Rect & other player icon Rect object for player in players: check_id = player.getID() if check_id != client_player_ID: # check if the client player is hit by other players & update the health points for hits in player.getHitList(): if hits[0] == client_player_ID: game_sound_mixer.Channel(2).play(game_sound_mixer.Sound(hit_success_music)) client_player.health -= hits[1] # check if the client player manage to hit other players & update the client players hit list & sounds if hit_info[0] == "YES": check_rect = player.getPlayerRect() hit_success = hit_rect.colliderect(check_rect) if hit_success == 1: game_sound_mixer.Channel(2).play(game_sound_mixer.Sound(hit_success_music)) client_player.updateHitList(check_id, client_player.getDamage(hitType)) client_player.score += 1 draw_stage_play(players, client_player_ID, topScorer, hitLoadButton, game.potionList) hitType = "NONE" client_player_info = PlayerInfo(client_player, obtained_potion_IDs) # adjust the background and hit sounds if SoundButtons.bg_sound == False: game_sound_mixer.Channel(0).set_volume(0) elif SoundButtons.bg_sound == True: game_sound_mixer.Channel(0).set_volume(BG_SOUND_LEVEL) if SoundButtons.kick_sound == False: game_sound_mixer.Channel(1).set_volume(0) game_sound_mixer.Channel(2).set_volume(0) elif SoundButtons.kick_sound == True: game_sound_mixer.Channel(1).set_volume(HIT_SOUND_LEVEL) game_sound_mixer.Channel(2).set_volume(HIT_SOUND_LEVEL) return (game_stage, client_player_info)
def run_game_stage_play(): score = 0 gainCoin = 0 playerLogChange = False newBestScore = False newLevelGain = False game_stage = "game_over" topScorer = True player_condition = True server_communicate = True client_player_info = [] hitLoadButton = HitLoadButton(inits.playerLog["PunchWait"], inits.playerLog["KickWait"], inits.playerLog["Health"]) # Ready to connect to server net = Network() # if no connection to server if net.connection == False: drawings.draw_no_connection() server_communicate = False game_stage = "name" else: init_player_info = pickle.loads(net.init_info) print("Received Initial Player Info: ", init_player_info) if server_communicate == True: # initial info from server: game ID, player_ID, pos_x, pos_y, icon_num init_player_info = submodules.split_init_info(init_player_info) game_ID = int(init_player_info[0]) inits.gameDisp.set_caption("Connected to Room " + str(game_ID)) client_player_ID = int(init_player_info[1]) pos_x = int(init_player_info[2]) pos_y = int(init_player_info[3]) icon_num = int(init_player_info[4]) icon_info = submodules.getIcon(icon_num) # create client Player object posInfo = [pos_x, pos_y, 0, 0] hitInfo = [False, "right", (0, 0), [], 0, "NONE"] client_player = Player( client_player_ID, inits.nameButton.name, posInfo, icon_info[0], icon_info[1], icon_num, hitInfo, 0, inits.playerLog["Health"], inits.playerLog["PunchWait"], inits.playerLog["KickWait"], inits.playerLog["PunchDamage"], inits.playerLog["KickDamage"]) # create client PlayerInfo object client_player_info = PlayerInfo(client_player, []) run = True # check keyboard & mouse actions hitType = "NONE" hitBool = 1 rapid_hit = False rx_time = time.perf_counter() while run: obtained_potion_IDs = [] #inits.clock.tick(settings.SCREEN_UPDATE_CLOCK_TICK_GAME) # getting the new screen dimensions submodules.update_display_dimensions(inits.gameDisp) # sending the new player list from server while sending the updated player if client_player_info.health <= 0: game_stage = "game_over" run = False break else: """try: net.send(client_player_info) except: print("sending not successful") drawings.draw_no_connection() game_stage = "name" run = False net.close() break""" net.send(client_player_info) if net.connection == False: print("sending not successful") drawings.draw_no_connection() game_stage = "name" run = False net.close() break # receiving the new player list info from server data = net.receive() if net.connection == True: game_data = pickle.loads(data) else: print("receiving not successful") drawings.draw_no_connection() game_stage = "name" run = False net.close() break """"if data[0] == True: game_data = pickle.loads(data[1]) else: print("receiving not successful") drawings.draw_no_connection() game_stage = "name" run = False net.close() break""" """try: game_data = pickle.loads(data) except: print("unknown error")""" game_IDX = game_data.game_ID playerInfoList = game_data.playerInfoList potionList = game_data.potionList if game_IDX < 0: drawings.draw_no_connection() game_stage = "name" run = False playerLogChange = False net.close() break else: playerLogChange = True # updating the current player for info in playerInfoList: if info.id == client_player_ID: client_player.copy(info) players = [] players.append(client_player) for info in playerInfoList: if info.id != client_player_ID: icon_info = submodules.getIcon(info.icon_num) op_posInfo = [ info.x, info.y, info.vel_x, info.vel_y ] op_hitInfo = [ info.hitDisp, info.hit_img_orientation, info.hit_img_center, info.hitList, info.hitRotAngle, info.hitType ] other_player = Player(info.id, info.name, op_posInfo, icon_info[0], info.orientation, icon_num, op_hitInfo, info.score, info.health, 0, 0, 0, 0) players.append(other_player) events = inits.pygame.event.get() for event in events: if event.type == inits.pygame.QUIT: run = False game_stage = "closing" net.close() break # checking mouse clicks for updating sound buttons & sounds if event.type == inits.pygame.MOUSEBUTTONDOWN: mx, my = inits.pygame.mouse.get_pos() inits.SoundButtons.mouseAction(mx, my) inits.exitGameButton.mouseAction(mx, my) inits.leaveRoomButton.mouseAction(mx, my) if inits.exitGameButton.clicked: net.close() game_stage = "closing" run = False if inits.leaveRoomButton.clicked: net.close() game_stage = "game_over" inits.leaveRoomButton.clicked = False run = False # checking keyboard inputs for hitting if event.type == inits.pygame.KEYDOWN: if event.key == inits.pygame.K_a: hitType = "PUNCH" if event.key == inits.pygame.K_s: hitType = "KICK" if event.type == inits.pygame.KEYUP: if event.key == inits.pygame.K_a: hitBool = 1 if event.key == inits.pygame.K_s: hitBool = 1 if event.key == inits.pygame.K_w: topScorer = not topScorer if event.key == inits.pygame.K_q: player_condition = not player_condition if hitBool == 0: hitType = "NONE" # updating player location : keyboard input checks are done inside the function client_player.move(settings.MOVE_TIME_WAIT, settings.WIN_DIM, settings.ICON_DIM[0], settings.ZERO_THR) # check if player gets any potion client_rect = client_player.getPlayerRect() for potion in potionList: potion_rect = inits.health_potion_img.get_rect( topleft=potion[1]) getPotion = client_rect.colliderect(potion_rect) if getPotion: obtained_potion_IDs.append(potion[2]) if potion[0] == "health": client_player.health = min( client_player.health + 3, inits.playerLog["Health"]) hitLoadButton.health_rem = client_player.health if potion[0] == "rapid": start_counter = time.perf_counter() rapid_hit = True if rapid_hit == True: if time.perf_counter() - start_counter > 15: rapid_hit = False client_player.minPunchWait = inits.playerLog[ "PunchWait"] client_player.minKickWait = inits.playerLog[ "KickWait"] hitLoadButton.punchWait = inits.playerLog[ "PunchWait"] hitLoadButton.kickWait = inits.playerLog[ "KickWait"] else: client_player.minPunchWait = 1 client_player.minKickWait = 2 hitLoadButton.punchWait = 1 hitLoadButton.kickWait = 2 # reset the hit list of client player & get the hit_info: (did_player_hit, the Rect object of hitting) client_player.resetHitList() hit_info = client_player.hit( hitType, inits.game_sound_mixer, settings.hit_try_music, inits.punch_img, inits.kick_img, settings.ICON_DIM[2], settings.ICON_DIM[4], settings.ICON_DIM[0], settings.ZERO_THR) if hit_info[0] == "YES": hit_rect = hit_info[1] # update hit load buttons hitLoadButton.lastHitTime = client_player.lastHitTime hitLoadButton.updateHitLoad(time.perf_counter()) # update player health if other players hit matches the client ID # update the hit list of the client (other player IDs & damage performed ) # based on collision check of client hit Rect & other player icon Rect object for player in players: check_id = player.id if check_id != client_player_ID: # check if the client player is hit by other players & update the health points for hits in player.getHitList(): if hits[0] == client_player_ID: inits.game_sound_mixer.Channel(2).play( inits.game_sound_mixer.Sound( settings.hit_success_music)) if client_player.health - hits[1] <= 0: client_player.KO = True inits.game_sound_mixer.Channel(3).play( inits.game_sound_mixer.Sound( settings.ko_music)) client_player.health = max( 0, client_player.health - hits[1]) hitLoadButton.health_rem = client_player.health # check if the client player manage to hit other players & update the client players hit list & sounds if hit_info[0] == "YES": check_rect = player.getPlayerRect() hit_success = hit_rect.colliderect(check_rect) if hit_success == 1: inits.game_sound_mixer.Channel(2).play( inits.game_sound_mixer.Sound( settings.hit_success_music)) client_player.updateHitList( check_id, client_player.getDamage(hitType)) client_player.score += client_player.getDamage( hitType) if client_player.getDamage( hitType) > player.health: client_player.KO = True inits.game_sound_mixer.Channel(3).play( inits.game_sound_mixer.Sound( settings.ko_music)) drawings.draw_stage_play(players, client_player_ID, topScorer, hitLoadButton, potionList, player_condition) hitType = "NONE" client_player_info = PlayerInfo(client_player, obtained_potion_IDs) # adjust the background and hit sounds routines.sound_set_routine() if playerLogChange: score = round(client_player_info.score) gainCoin = submodules.calculateGold(score) inits.playerLog["Coins"] = gainCoin + inits.playerLog["Coins"] if score > inits.playerLog["BestScore"]: newBestScore = True inits.playerLog["BestScore"] = score if score > settings.LEVEL_PTS[inits.playerLog["Level"]]: inits.playerLog["Level"] += 1 newLevelGain = True output = [game_stage, gainCoin, newBestScore, newLevelGain, score] return output
def __init__(self, config: GameConfig, players: List[Player]): self.config = config self.state = State(config) self.players = [ PlayerInfo(i, player) for i, player in enumerate(players) ]
return maxPot - info.pot #======================================== # TESTS #======================================== if __name__ == '__main__': #UNIT TESTS print "Testing constructors." player1 = PlayerInfo(0, "John", True, True, 100, 10, [card.Card("H", "3"), card.Card("S", "2")], False, False) player2 = PlayerInfo(1, "Susy", True, True, 100, 5, [card.Card("?", "?"), card.Card("?", "?")], True, False) player3 = PlayerInfo(2, "Hamm", True, True, 100, 0, [card.Card("?", "?"), card.Card("?", "?")], False, False) player4 = PlayerInfo(3, "Gunn", True, True, 100, 0, [card.Card("?", "?"), card.Card("?", "?")], False, True) infos = [player1, player2, player3, player4] state = State(infos, []) assert state.isValid() #Here the ids aren't unique player1 = PlayerInfo(0, "John", True, True, 100, 10, [card.Card("H", "3"), card.Card("S", "2")], False, False) player2 = PlayerInfo(0, "Susy", True, True, 100, 5, [card.Card("?", "?"), card.Card("?", "?")], True, False) player3 = PlayerInfo(2, "Hamm", True, True, 100, 0, [card.Card("?", "?"), card.Card("?", "?")], False, False) player4 = PlayerInfo(3, "Gunn", True, True, 100, 0, [card.Card("?", "?"), card.Card("?", "?")], False, True)