Ejemplo n.º 1
0
class GamePlay:
    def __init__(self):
        self.__screen = pygame.display.set_mode((1000, 700))
        self.__vaccines_background = VaccineBackground()
        self.__player = Player()
        self.__heart = Heart()
        self.__vaccine = Vaccine()
        self.__handle_crocodile_human = HandleCrocodileToHuman()
        self.__virus_manager = VirusManager()
        self.__collision = Collisions()
        self.__sound = Sound()

    def playing(self, start):
        gameover = start
        self.__pontuation = Pontuation()
        self.__independent_heart = IndependentHeart()
        self.__independent_vaccine = IndependentVaccine()

        while gameover:
            current_time = time.time()
            self.__draw_background()

            hit_virus = self.__collision.with_virus.did_virus_collide_with_player(
                self.__player, self.__virus_manager.virus)
            hit_crocodile = self.__collision.with_crocodile.did_player_collide_with_crocodile(
                self.__player, self.__handle_crocodile_human.crocodile)
            player_is_invencible = self.__player.invencible
            gameover = self.__action_after_hit_crocodile(
                hit_crocodile, player_is_invencible)
            gameover = gameover and self.__action_after_hit_virus_and_player_not_invencible(
                hit_virus, player_is_invencible)

            self.__draw_managers()
            gameover = gameover and self.__player.move()
            self.__player.draw(self.__screen)
            self.__draw_collectables()

            hit_heart = self.__collision.with_heart.did_heart_collide_with_player(
                self.__player, self.__independent_heart)
            self.__action_after_hit_heart(hit_heart)

            hit_vaccine = self.__collision.with_vaccine.did_vaccine_collide_with_player(
                self.__player, self.__independent_vaccine)
            self.__action_after_hit_vacine(hit_vaccine)

            self.__handle_crocodile_human.draw(self.__screen)
            self.__pontuation.draw(self.__screen)

            pygame.display.update()

            if (gameover == False):
                return self.__pontuation.get_pontuation(current_time)

    def __action_after_hit_crocodile(self, hit_crocodile,
                                     player_is_invencible):
        if not self.__handle_crocodile_human.is_human:
            if hit_crocodile and self.__vaccine.zero_vaccine_left():
                if not player_is_invencible:
                    self.__heart.lost_life()
                    if self.__heart.zero_lives_left():
                        return False
                    else:
                        self.__player.is_invencible()
                        self.__sound.lost_life_play()
            elif hit_crocodile:
                self.__handle_crocodile_human.hit_crocodile_with_vaccine()
                self.__vaccine.spend_vaccine()
                self.__pontuation.add_point()
        return True

    def __action_after_hit_virus_and_player_not_invencible(
            self, hit_virus, player_is_invencible):
        current_time = time.time()
        if hit_virus and not player_is_invencible:
            self.__heart.lost_life()
            if self.__heart.zero_lives_left():
                return False
            else:
                self.__sound.lost_life_play()
                self.__player.is_invencible()
        return True

    def __action_after_hit_vacine(self, hit_vaccine):
        if hit_vaccine:
            self.__independent_vaccine.colided()
            self.__vaccine.got_vaccine()
            self.__sound.vaccine_heart_play()

    def __action_after_hit_heart(self, hit_heart):
        if hit_heart:
            self.__independent_heart.colided()
            self.__heart.win_life()
            self.__sound.vaccine_heart_play()

    def __draw_collectables(self):
        self.__heart.draw(self.__screen)
        self.__vaccine.draw(self.__screen)
        self.__independent_heart.draw(self.__screen)
        self.__independent_vaccine.draw(self.__screen)

    def __draw_managers(self):
        self.__virus_manager.draw(self.__screen)

    def __draw_background(self):
        self.__vaccines_background.draw(self.__screen)
Ejemplo n.º 2
0
class Game():
    '''
    Class used for handling a game of blackjack between two players, one of which is a dealer.
    '''
    def __init__(self):
        '''
        Constructor for a Game.
        '''
        self.deck = Deck()
        self.deck.shuffle()

        player_hand = Hand(self.deck)
        dealer_hand = Hand(self.deck)
        dealer = Dealer(dealer_hand, player_hand)

        self.player = Player("Player", player_hand)
        self.dealer = Autoplayer("Dealer", dealer_hand, dealer)

        self.player.draw()
        self.dealer.draw()
        self.player.draw()

        self.dealer_turn = False

        self.player_wins = 0
        self.dealer_wins = 0
        self.added_to_scoreboard = False

    def reset(self):
        '''
        Reinitializes the game.
        '''
        if not self.is_end() and not self.added_to_scoreboard:
            self.dealer_wins += 1

        self.deck.shuffle()

        player_hand = Hand(self.deck)
        dealer_hand = Hand(self.deck)
        dealer = Dealer(dealer_hand, player_hand)

        self.player = Player("Player", player_hand)
        self.dealer = Autoplayer("Dealer", dealer_hand, dealer)

        self.player.draw()
        self.dealer.draw()
        self.player.draw()

        self.dealer_turn = False
        self.added_to_scoreboard = False

    def hit(self):
        '''
        Draws a card for the first player.
        :raises BlackjackException: if game came to a conclusion or if it's not player's turn
        '''
        if self.is_end():
            raise BlackjackException("Game ended.")

        if self.dealer_turn:
            raise BlackjackException("Not player\'s turn.")

        self.player.draw()

    def stand(self):
        '''
        Stands first player's turn and passes the turn to the dealer.
        :raises BlackjackException: if game came to a conclusion or if it's not player's turn, if player's already standing
        '''
        if self.is_end():
            raise BlackjackException("Game ended.")

        if self.dealer_turn:
            raise BlackjackException("You\'re already standing.")

        self.player.stand()
        self.dealer_turn = True

    def deal(self):
        '''
        Lets the dealer to take a decision to play.
        :raises BlackjackException: if game came to a conclusion or if it's not dealer's turn
        '''
        if self.is_end():
            raise BlackjackException("Game ended.")

        if not self.dealer_turn:
            raise BlackjackException("Not dealer\'s turn.")

        move = self.dealer.proceed()
        return move

    def is_end(self):
        '''
        Checks if the game came to a conclusion.
        :return: True - game reached a conclusion, False - otherwise
        '''
        if self.player.is_loser():
            if not self.added_to_scoreboard:
                self.dealer_wins += 1
            self.added_to_scoreboard = True
            return True

        if self.player.is_standing() and self.dealer.is_loser():
            if not self.added_to_scoreboard:
                self.player_wins += 1
            self.added_to_scoreboard = True
            return True

        if self.player.is_standing() and self.dealer.is_standing():
            if not self.added_to_scoreboard:
                if self.player.hand.sum() > self.dealer.hand.sum():
                    self.player_wins += 1
                else:
                    self.dealer_wins += 1
            self.added_to_scoreboard = True
            return True

        return False