Python 21点游戏问题

2024-04-25 07:48:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在编一种21点游戏。在这个游戏中,我想让庄家和玩家各有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()

Tags: tofalsetruegetifcardtotalplayer
2条回答

如果您想包含多个参与者,那么您肯定希望使用面向对象的方法。如果您不熟悉此页,请参阅:https://www.tutorialspoint.com/python/python_classes_objects.htm

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

只需将玩家和庄家的生命值设为100,每次输球只需减去10。只需在庄家赢下减少玩家生命值,在庄家赢下减少庄家生命值。很简单,真的

相关问题 更多 >