Exemple #1
0
class Lineups():
    def __init__(self, home_team, away_team):
        self.display = Display()
        self.home_team = home_team
        self.away_team = away_team

    def display_players(self):
        # lineup boxs image
        lineup_box = pygame.image.load(
            "assets/sprites/Backgrounds/lineup.png").convert()

        # Box for home team
        self.display.draw_image(lineup_box, (20, 20))
        # Box for away team
        self.display.draw_image(lineup_box, (600, 20))

        home_team = self.home_team
        away_team = self.away_team

        # vars to postion text
        # home and away team
        hy = 24
        ay = 24

        # Space between names
        SPACE = 40

        # Displaying home team name
        self.display.display_text(home_team[0], self.display.font_small,
                                  (22, 22))
        # Display names of players on home team
        for i in range(1, 12):
            hy += SPACE
            self.display.display_text(home_team[i], self.display.font_small,
                                      (22, hy))

        # Displaying away team name
        self.display.display_text(away_team[0], self.display.font_small,
                                  (602, 22))
        # Display names of players on away team
        for i in range(1, 12):
            ay += SPACE
            self.display.display_text(away_team[i], self.display.font_small,
                                      (602, ay))

    def display_lineups(self):
        # menu audio
        pygame.mixer.music.stop()  # stopping main menu audio before playing
        pygame.mixer.music.load("assets/audio/match/lineup.wav")
        pygame.mixer.music.play(-1)

        #loading background
        field_bkg = pygame.image.load(
            "assets/sprites/Backgrounds/play-screen.png").convert()
        run = True
        while run:
            self.display.display_background(field_bkg)
            self.display_players()
            self.display.display_text("Press Spacebar to continue",
                                      self.display.font_small, (400, -5))
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

                # if users presses spacebar start simulator
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        # run sims
                        from Match.game import Game
                        Game().run_sim()
                        self.display.scoresheet()
                        run = False
            pygame.display.update()
Exemple #2
0
class Chances(Players):
    """
        Deals with a chance on goalnand setpieces
        for e.g
        a shot
        a pass to teammate
        a cross
    """
    def __init__(self):
        super().__init__()
        self.match_vars = MatchVariables()
        self.display = Display()

    # SHOTS
    # play goal audio, crowd should be louder after goal and show image
    def goal(self, team, player):
        self.match_vars.increase_score(team, player)
        goal_scored = True

        pygame.mixer.Sound("assets/audio/match/goal.wav").play()

        self.display.draw_image(goal_img, (410, 280))
        pygame.display.update()
        time.sleep(4)

    # play audio when miss
    def miss(self):
        pygame.mixer.Sound("assets/audio/match/miss.wav").play()

    def shot_on_target(self, player):
        goal = random.randint(1, 2)
        corner = random.randint(1, 2)
        if goal == 1:
            self.goal(self.attacking_team, player)
        else:
            self.display.game_com(f"{self.gk} saves!", self.display.font_small)
            if corner == 1:
                self.display.game_com(
                    f"{self.gk} did not save the ball cleanly and it goes behind for a corner.",
                    self.display.font_small)
                self.corner_kick()

    # if miss play audio
    def shot_off_target(self):
        r = random.randint(1, 10)
        msg = random.choice([
            "The ball goes slightly over the bar! Great effort!",
            "That was nowhere near the goal!", "He hit the crossbar! Unlucky!"
        ])
        self.miss()
        self.display.game_com(msg, self.display.font_small)

    def shoot(self, player):
        r = random.randint(1, 2)
        if r == 1:
            self.shot_on_target(player)
        else:
            self.shot_off_target()

    # PASS/CROSS
    def pass_ball(self):
        # through ball, low cross, high cross
        type_of_pass = random.randint(1, 10)
        volley_or_header = random.randint(1, 2)
        field = self.attacking_team[1:8]

        if self.player in field:
            field.remove(self.player)
        teammate = random.choice(field)

        if type_of_pass >= 1 and type_of_pass < 4:
            self.display.game_com(
                f"Great vision from {self.player} to find {teammate} with a pin-point through ball",
                self.display.font_small)
            self.display.game_com(
                f"{teammate} steadies himself and shoots!...",
                self.display.font_small)
            self.shoot(teammate)
        elif type_of_pass > 3 and type_of_pass < 7:
            self.display.game_com(
                f"{self.player} finds {teammate} with a low cross",
                self.display.font_small)
            self.display.game_com(f"{teammate} shoots!...",
                                  self.display.font_small)
            self.shoot(teammate)
        else:
            self.display.game_com(
                f"{self.player} finds {teammate} with a high cross",
                self.display.font_small)
            if volley_or_header == 1:
                self.display.game_com(f"{teammate} goes for the volley!",
                                      self.display.font_small)
                self.shoot(teammate)
            else:
                self.display.game_com(f"{teammate} heads it!",
                                      self.display.font_small)
                self.shoot(teammate)

    # DRIBBLE
    def dribble(self):
        event = random.randint(1, 2)
        is_pass = random.randint(1, 2)

        self.display.game_com(f"{self.player} dribbles with the ball...",
                              self.display.font_small)

        if event == 1:
            self.display.game_com("Then, he attempts to pass to his teammate.",
                                  self.display.font_small)
            if is_pass:
                self.pass_ball()
            else:
                self.display.game_com(f"{self.defender} intercepts the ball",
                                      self.display.font_small)
        else:
            #solo-goal
            self.display.game_com(
                f"{self.player} takes it pass the defender. He's in the box!",
                self.display.font_small)
            self.shoot(self.player)

    # SETPIECES
    def freekick(self, team, set_piece_taker):
        """
            This is done so python doesn't call Players
            init method again when it cannot find the variables
            thus creating a new random value
        """
        self.attacking_team = team
        self.fk_taker = set_piece_taker

        pass_or_shoot = random.randint(1, 2)
        self.display.game_com(
            f"{self.attacking_team[0]} gets a freekick. {self.fk_taker} stands over it.",
            self.display.font_small)

        # pass
        if pass_or_shoot == 1:
            head = random.randint(1, 2)
            headed_goal = random.randint(1, 2)
            # any player except the goalkeeper is in the box
            box = self.attacking_team[1:10]

            # freekick taker is in box remove him
            if self.fk_taker in box:
                box.remove(self.fk_taker)

            teammate = random.choice(box)
            self.display.game_com(
                f"{self.fk_taker} tries to find the head of his teammate in the box",
                self.display.font_small)

            # ball lands on head
            if head == 1:
                self.display.game_com(f"{teammate} heads it!",
                                      self.display.font_small)
                # scores
                if headed_goal == 1:
                    self.goal(self.attacking_team, teammate)
                else:
                    self.display.game_com("He heads it over the bar.",
                                          self.display.font_small)

            # ball does not find teammmate
            else:
                self.display.game_com(
                    "The ball flies over the heads of everyone in the box and goes out of play.",
                    self.display.font_small)

        # Player shoots from freekick
        else:
            shoot = random.randint(1, 3)
            self.display.game_com(f"{self.fk_taker} goes for goal!...",
                                  self.display.font_small)

            # 33.33% chance he scores directly from freekick
            if shoot == 1:
                self.goal(self.attacking_team, self.fk_taker)
            else:
                self.miss()
                self.display.game_com("It goes over the bar! Great Effort.",
                                      self.display.font_small)

    def penalty(self, team, set_piece_taker, gk):
        self.attacking_team = team
        self.pk_taker = set_piece_taker
        self.gk = gk
        score = random.randint(1, 2)
        miss = random.choice(
            ["{} misses!".format(self.pk_taker), "{} saves!".format(self.gk)])

        self.display.game_com("The referee points to the spot!",
                              self.display.font_small)
        self.display.game_com(
            f"{self.pk_taker} places the ball and steps back...",
            self.display.font_small)
        self.display.game_com(f"{self.gk} gets ready to defend his goal",
                              self.display.font_small)
        self.display.game_com(f"{self.pk_taker} runs up!...",
                              self.display.font_small)
        self.display.game_com("He shoots!", self.display.font_small)

        if score == 1:
            self.goal(self.attacking_team, self.pk_taker)
        else:
            self.miss()
            self.display.game_com(miss, self.display.font_small)

    def corner_kick(self):
        success = random.randint(1, 2)
        event = random.randint(1, 10)
        goal = random.randint(1, 2)

        box = self.attacking_team[1:11]  # excludes gk

        # remove corner kick taker if he is in the box
        if self.ck_taker in box:
            box.remove(self.ck_taker)

        teammate = random.choice(box)

        self.display.game_com(
            f"{self.attacking_team[0]} receives a corner. {self.ck_taker} is going to take it.",
            self.display.font_small)
        self.display.game_com(f"{self.ck_taker} whips it into the box!...",
                              self.display.font_small)

        # successful corner attempt
        if success == 1:
            if event >= 1 and event <= 6:
                self.display.game_com(f"{teammate} jumps and head the ball!",
                                      self.display.font_small)
                if goal == 1:
                    self.goal(self.attacking_team, teammate)
                else:
                    self.miss()
                    self.display.game_com("It's over the bar!",
                                          self.display.font_small)
            elif event > 6 and event <= 9:
                self.display.game_com(f"{teammate} tries a bicycle kick!...",
                                      self.display.font_small)
                if goal == 1:
                    self.goal(self.attacking_team, teammate)
                else:
                    self.miss()
                    self.display.game_com(
                        "Great effort but it goes slightly over the crossbar! ",
                        self.display.font_small)
            else:
                if goal == 1:
                    self.display.game_com(
                        f"{self.ck_taker} goes for goal from the corner flag.",
                        self.display.font_small)
                    self.goal(self.attacking_team, self.ck_taker)
                else:
                    self.display.game_com(
                        "The ball fly over the heads of everyone in the box.",
                        self.display.font_small)
        else:
            self.display.game_com(
                "The defender heads it clear. Good defending.",
                self.display.font_small)

    # when the two teams are struggling for possession/no chance for goal
    def idle(self):
        msg = random.choice([
            "Both teams are struggling for possession...",
            f"{self.home_team[0]} and {self.away_team[0]} are battling it out together...",
            "We wait for a chance on goal as both teams are currently at a stalemate..."
        ])
        self.display.game_com(msg, self.display.font_small)

    def play(self):
        # 50% chance player pass 30% chance player shoots 20% he dribbles
        dist = random.randint(20, 30)
        r = random.randint(1, 10)
        self.display.game_com(f"{self.attacking_team[0]} attacks...",
                              self.display.font_small)

        if r >= 1 and r < 6:
            self.pass_ball()
        elif r >= 6 and r < 9:
            self.display.game_com(
                f"{self.player} goes for goal from {dist} yards out!",
                self.display.font_small)
            self.shoot(self.player)
        else:
            self.dribble()