Example #1
0
class DuoLobby(Lobby):
    def __init__(self, player_ids):
        super().__init__()
        self.player_1_id = player_ids[0]
        self.player_2_id = player_ids[1]
        self.match = Match()
        self.match.add_player(self.player_1_id, [450, 500], 0.25, 30, 1)
        self.match.add_player(self.player_2_id, [950, 500], 0.25, 30, 1)
        self.num_players = 2

    def relay_input(self, user_id, input_x, input_y):
        self.match.set_player_input(user_id, input_x, input_y)

    def play(self):
        self.status = "STARTED"
        self.match.run_game_loop()
        # Wait for all players to leave
        while (self.num_players > 0):
            pass
        self.status = "FINISHED"

    def get_game_state(self, user_id):
        game_state = {
            "playerNames":
            [self.player_1_id,
             self.player_2_id],  # TODO change these to reference usernames
            "matchData": self.match.get_match_data(user_id),
            "playerData": self.match.get_player_data()
        }
        return game_state

    def remove_player(self, user_id):
        self.num_players -= 1
        self.match.num_players -= 1
Example #2
0
def run():
    match = Match()
    player_count = int(input('How many players (1-2)? '))
    if player_count != 1 and player_count != 2:
        raise PlayerCountException('This is for 1 or 2 players only')
    for i in range(player_count):
        player_name = input(f'Player {i+1} name: ')
        match.add_player(Player(player_name))
    match.start()
    while True:
        for player in match.players:
            print(
                f'Player {player.initial}: Games: {player.games_won} - Score: {player.score}'
            )
        received_input = input(
            f'Player {match.game.active_player.initial} score: ')
        if received_input == 'exit':
            break
        try:
            match.enter_score(int(received_input))
        except ValueError:
            print('Please enter a number, or type "exit" to quit')