Python Blackjack游戏问题

时间:2016-10-24 05:38:57

标签: python python-2.7

我正在编写一种二十一点游戏。在这个游戏中,我想让它成为经销商和玩家各自拥有100健康,如果他们失去了一只手,那么他们将失去10点健康。这种情况一直持续到其中一个达到0健康状态。我无法弄清楚如何为游戏增添健康。

以下是目前的游戏:

import random
newgame = 0

def get_card():
    #I did random from 1,11 to reduce card counting. 
    return random.randint(1, 11)

def player():
    blackjack = False
    total = 0

    print('************ YOUR TURN ************')

    card1 = get_card()
    card2 = get_card()
    total = card1 + card2

    print("Cards: " + str(card1) + " " + str(card2))
    print("Total: " + str(total))
    if total is 21:
        blackjack = True

    while total < 21:
        option = raw_input('Type "S" to stay  or "H" to hit ')
        if option == 's' or option == 'S':
            break
        card1 = get_card()
        print("\nDraws: " + str(card1))
        total += card1
        print("Total: " + str(total))
    return total, blackjack


def dealer():
    print("\n********** DEALER'S TURN **********")

    total = 0
    card1 = get_card()
    card2 = get_card()
    total = card1 + card2

    print("Cards: " + str(card1) + " " + str(card2))
    print("Total: " + str(total))
    while total <= 16:
        raw_input('Press <enter> to continue ...')
        card1 = get_card()
        print("\nDraws: " + str(card1))
        total += card1
        print("Total: " + str(total))
    return total

def main():
    play_again = True


    while play_again:
        player_total, blackjack = player()
        player_wins = False
        dealer_wins = False 
        if blackjack:
            print('Blackjack!')
            player_wins = True


        if player_total > 21:
            print('Bust!')
            dealer_wins = True

        if player_wins is False and dealer_wins is False:
            dealer_total = dealer()
            if dealer_total > 21:
                print('Bust!')
                player_wins = True
            elif player_total > dealer_total:
                player_wins = True
            else:
                dealer_wins = True

        print("\n********** GAME OVER **********")
        if player_wins:
            print('You win!')
        elif dealer_wins:
            print('Dealer wins!')
        while True:
            again = raw_input('Type "P" to play again or "Q" to quit: ')
            if again.upper() == "Q":
                print("Game ended.")
                play_again = False
                break
            elif again.upper() == "P":
                break
main()

2 个答案:

答案 0 :(得分:-1)

如果你想要包含几个玩家,你肯定想要使用面向对象的方法。如果您不熟悉,请参阅此页面:https://www.tutorialspoint.com/python/python_classes_objects.htm

如果您只想将其添加到main函数中,我建议添加hp = 100变量。每次它是一个半身像,只需从中扣除10。如果hp == 0,请结束游戏。

答案 1 :(得分:-1)

只需将玩家和经销商的健康状况设置为100,每次丢失时直接取消10。只是在玩家获胜的情况下减少玩家的健康状真的很简单