Beispiel #1
0
class Game:
    '''game class'''
    # pylint: disable=too-many-instance-attributes

    bust = False
    bj_pays = 3/2
    bj_player = False
    bj_dealer = False
    total_p = 0
    total_d = 0

    def __init__(self):
        '''
        game init
        '''
        #print(Deck())
        print('BlackJack pays {}'.format(self.bj_pays))
        self.deck = Deck()
        self.player = Player()
        self.dealer = Dealer()
        self.deck.shuffle()

    def deal_two_foreach(self):
        '''
        deal 2 cards for each player
        '''
        #one for the player
        card = self.deck.deal()
        self.player.hit(card)
        #one for the dealer, hidden
        card = self.deck.deal(message=False)
        card.hide()# one card of the dealer is hidden
        self.dealer.hit(card)
        #one for the player
        card = self.deck.deal(message=False)
        self.player.hit(card)
        #one for the dealer
        card = self.deck.deal(message=False)
        self.dealer.hit(card)
        #show table
        self.deck.draw_table(self.player.cards, self.dealer.cards)

    def check_hand(self, cards):
        '''
        counts the number of aces
        calculates hand total
        checks if busted
        @param cards
        @return total
        '''
        total = 0
        has_ace = 0
        for card in cards:
            #count aces
            if card.value == 11:
                has_ace += 1
            total += card.value
        #if total exceds 21, set aces value to 1 until total gets to 21 or below
        while total > 21 and has_ace > 0:
            total = total - 10
            has_ace -= 1
        #busted
        if total > 21:
            self.bust = True
        return total

    def players_run(self):
        '''
        hit or stand actions for the player
        '''
        while self.bj_player is False:

            self.total_p = self.check_hand(self.player.cards)
            #check hand
            if self.total_p == 21:
                #player has 21
                break
            elif self.bust:
                #bust
                print('You lost.')
                self.play_again()
                return
            else:
                while True:
                    try:
                        action = int(input('Press 1 for Hit or 2 for Stand: '))
                        break
                    except ValueError:
                        pass
                if action == 1:
                    #one for the player
                    card = self.deck.deal()
                    self.player.hit(card)
                    #show table
                    self.deck.draw_table(self.player.cards, self.dealer.cards)
                else:
                    #stand
                    self.player.stand()
                    break

    def dealers_run(self):
        '''
        hit or stand actions for the dealer
        '''
        #show hidden card
        self.dealer.cards[0].show()
        self.deck.draw_table(self.player.cards, self.dealer.cards)
        while self.bj_dealer is False and self.bj_player is False:
            # if the player has blackjack and the dealer is not,
            # dealing more cards doesn't make sense
            self.total_d = self.check_hand(self.dealer.cards)
            #check hand
            if self.total_d == 21:
                #dealer has 21
                break
            elif self.bust is True:
                #bust
                print('You won {} chip(s).'.format(2*self.player.bet_value))
                self.player.chips += 2*self.player.bet_value
                self.play_again()
                return
            elif self.total_d <= self.dealer.draw_until:
                card = self.deck.deal()
                self.dealer.hit(card)
                self.deck.draw_table(self.player.cards, self.dealer.cards)
            elif self.total_d >= self.dealer.must_stand:
                self.dealer.stand()
                break

    def conclude(self):
        '''
        decide who the winner is
        '''
        if (self.bj_player == True and self.bj_dealer == True) or (self.total_p == self.total_d):
            # tie
            print('Tie game')
            self.player.chips += self.player.bet_value
        elif self.bj_player:
            print('You won {} chip(s)'.format((self.bj_pays+1)*self.player.bet_value))
            self.player.chips += (self.bj_pays+1)*self.player.bet_value
        elif self.total_p > self.total_d:
            print('You won {} chip(s)'.format(2*self.player.bet_value))
            self.player.chips += 2*self.player.bet_value
        else:
            print('You lost.')
        self.play_again()

    def play(self):
        '''
        game thread
        '''
        #check if player has chips
        if self.player.chips == 0:
            print('You don\'t have any chips')
            quit()
        #check if enough cards
        if len(self.deck.cards) < self.deck.min_in_shoe:
            print('Not enough cards')
            self.deck.reset()
        # place bet
        print('You have {} chips'.format(self.player.chips))
        self.player.bet()
        #deal cards
        self.deal_two_foreach()
        # check player for blackjack
        self.total_p = self.check_hand(self.player.cards)
        self.bj_player = (self.total_p == 21)
        # check dealer for blackjack
        self.total_d = self.check_hand(self.dealer.cards)
        self.bj_dealer = (self.total_d == 21)
        #player first
        self.players_run()
        #dealer second
        self.dealers_run()
        #decide who the winner is
        self.conclude()

    def play_again(self):
        '''
        new game
        '''
        while True:
            action = str(input('Play again? (y/n)'))
            if action.lower() == 'y':
                self.reset()
                self.play()
                break
            elif action.lower() == 'n':
                quit()

    def reset(self):
        '''
        reset game
        '''
        self.player.cards = []
        self.dealer.cards = []
        self.player.bet_value = 1
        self.bust = False
        self.bj_player = False
        self.bj_dealer = False
        self.total_p = 0
        self.total_d = 0
        
Beispiel #2
0
class Game(object):
    window = None
    game = None
    background = None
    game_is_running = True
    player = None
    stance_event = None
    sprites = None
    clock = None
    health_bar = None
    monster = None

    def init(self):
        """
        Initializes the game
        :return:
        """
        self.game = pygame
        self.game.init()

        # Game setup
        self.window = self.game.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.game.display.set_caption(WINDOW_TITLE)

        # Draw the image background
        self.background = self.game.image.load(DEFAULT_BACKGROUND)
        self.window.blit(self.background, (DEFAULT_BACKGROUND_X, DEFAULT_BACKGROUND_Y))
        self.game.display.update()

        # Initialize the player class
        self.player = Player()

        #Initialize the monster class
        self.monster = Monster()

        # Initialize user events
        self.stance_event = self.game.USEREVENT + 1
        self.attacking_event = self.game.USEREVENT + 2
        self.game.time.set_timer(self.stance_event, CHARACTER_STANCE_EVENT_INTERVAL)
        self.sprites = pygame.sprite.Group()
        self.sprites.add(self.player)
        self.sprites.draw(self.window)

        # Define a health bar

        # Define a clock
        self.clock = self.game.time.Clock()

        # Start game loop
        self.loop()

    def update(self):
        # Step 1, update the player
        self.sprites.update()
        self.window.blit(self.background, (DEFAULT_BACKGROUND_X, DEFAULT_BACKGROUND_Y))
        self.sprites.draw(self.window)
        pygame.draw.rect(self.window, (0, 0, 0), pygame.Rect(self.player.rect.x - 15, self.player.rect.y - 15, 100, 7))
        pygame.draw.rect(self.window, (255, 0, 0), pygame.Rect(self.player.rect.x - 15, self.player.rect.y - 15, self.player.health, 7))
        self.game.display.flip()
        self.clock.tick(30)

    def loop(self):
        while self.game_is_running:

        threading.Timer(6.7, Monster.random_movement)

        self.game.mixer.init()
        self.game.mixer.load('muscic.mp3')
        self.game.mixer.music.play(-1, 0.0)
            # Close event
            for event in self.game.event.get():
                if event.type == self.game.QUIT:
                    self.game_is_running = False

                if event.type == self.stance_event:
                    self.player.movement()

                if event.type == self.game.KEYDOWN:
                    self.player.hit(5)
                    # Check left right
                    if (event.key == self.game.K_LEFT):
                        self.player.move_left()
                    elif (event.key == self.game.K_RIGHT):
                        self.player.move_right()

            keys_pressed = self.game.key.get_pressed()

            if keys_pressed[self.game.K_LEFT]:
                self.player.move_left()

            if keys_pressed[self.game.K_RIGHT]:
                self.player.move_right()
            if keys_pressed[self.game.K_LCTRL]:
                self.player.attack()
            if keys_pressed[self.game.K_SPACE]:
                self.player.jump()

            self.update()
Beispiel #3
0
            suriken = Suriken()
            all_surikens.add(suriken)
            all_sprites.add(suriken)

    clock.tick(FPS)

    screen.blit(backgroundItem.background, backgroundItem.background_rect)
    # обработка ввода
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    hits = pygame.sprite.spritecollide(player, all_surikens, True,
                                       pygame.sprite.collide_circle)
    for hit in hits:
        player.hit(10)
        if player.health <= 0:
            game_over = True
        new_suriken = Suriken()
        all_surikens.add(new_suriken)
        all_sprites.add(new_suriken)

    # обновление данных
    all_surikens.update()
    player.update(screen, backgroundItem)

    # отрисовка
    all_sprites.draw(screen)  # вносим изменения спрайтов на экран
    draw_health(screen, 175, 5, player.health, BLUE)
    draw_text(screen, str(player.score), 12, 100, 100, font_name, RED)
    pygame.display.flip()  # обновление экран (отображение нового кадра)
Beispiel #4
0
def menu():
    print("Acciones")
    print("1. Usar espada")
    print("2. Curar")
    print("3. Usar bola de fuego")
    print("Elije una acción:", end="")
    return input()


while (pj1.hp > 0 and pj2.hp > 0):
    '''Turno jugador'''
    action = menu()
    print(action)
    if (int(action) == 1):
        if (pj1.hit(pj2.armor)):
            dmg = sword.attack()
            pj2.hp -= dmg
            print(pj1.name + " ha hecho " + str(dmg) + " de daño")
            if (pj2.hp <= 0):
                print(pj2.name + " ha muerto")
                break
        else:
            print(pj1.name + " ha fallado")
    elif (int(action) == 2):
        if (pj1.mp > healingSpell.cost):
            pj1.hp -= healingSpell.attack()
        else:
            print("No tienes suficiente maná")
            continue
    elif (int(action) == 3):
Beispiel #5
0
            run = False

    keystate = pygame.key.get_pressed()
    if keystate[pygame.K_SPACE]:
        if isBulletActive == False:
            isBulletActive = True
            bullet = Bullet(all_zombies.sprites()[0])
            all_sprites.add(bullet)
            player.score -= 1

    if keystate[pygame.K_ESCAPE]:
        isStopped = True

    hits = pygame.sprite.spritecollide(player, all_zombies, False)
    for hit in hits:
        player.hit()
        if (player.hp <= 0):
            run = False

    hits = pygame.sprite.spritecollide(player, all_walls, False)
    for hit in hits:
        player.hitwall(wall, keystate)

    if isBulletActive:
        hits = pygame.sprite.spritecollide(bullet, all_zombies, False)
        for zombie in hits:
            bullet.remove(all_sprites)
            # удалить спрайт пули
            isBulletActive = False
            zombie.Hp()
            player.score += 2