【问题标题】:Overwrite text object and infinite loop覆盖文本对象和无限循环
【发布时间】:2020-02-27 21:49:29
【问题描述】:

我无法获得新的总数来替换以前的总数,它只是将它打印在上面。另外,如果有人可以帮助解释为什么我的经销商卡处于无限循环中。

这是我的代码的一部分,一旦到达 while true 语句,它就会混乱。

def main():

    # card deck and shuffle it
    win = GraphWin('Blackjack', 800, 600)
    deck = CardDeck()
    deck.shuffle()

    # hit me button
    hitp = Point(650, 125)
    hitButton = Text(hitp, "Hit me")
    hitButton.draw(win)
    hitbox = Rectangle(Point(600, 100), (Point(700, 150)))
    hitbox.draw(win)

    # player's starting hand
    card = deck.dealOne()
    value, filename = cardInfo(card)
    drawCard(filename, 100, 100, win)
    playerTotal = value
    card = deck.dealOne()
    value, filename = cardInfo(card)
    drawCard(filename, 200, 100, win)
    playerTotal = playerTotal + value
    # display total
    hand_total = Text(Point(100, 200), playerTotal)
    hand_total.draw(win)


    # dealer's starting hand
    card = deck.dealOne()
    value, filename = cardInfo(card)
    drawCard(filename, 100, 300, win)
    dealerTotal = value
    # display total
    dhand_total = Text(Point(100, 400), dealerTotal)
    dhand_total.draw(win)


    while True:
        pt = win.getMouse()
        x = pt.getX()
        y = pt.getY()
        if 600 < x < 700 and 100 < y < 150:
            value, filename = cardInfo(card)
            drawCard(filename, 300, 100, win)
            playerTotal = playerTotal + value
            hand_total = Text(Point(100, 200), playerTotal)
            hand_total.draw(win)
            if playerTotal > 21:
                break
        else:
            break
    if playerTotal <= 21:
        while dealerTotal < 17:
            card = deck.dealOne()
            value, filename = cardInfo(card)
            drawCard(filename, 200, 300, win)
            dealerTotal2 = value
            dhand_total = Text(Point(100, 400), dealerTotal2)
            dhand_total.draw(win)
            if dealerTotal >= 21:
                break

    # wait for mouse click before closing window
    win.getMouse()
    win.close()

这是一个二十一点游戏,一旦玩家爆牌或庄家获得 17 点或更多,就应该中断。

【问题讨论】:

    标签: python blackjack zelle-graphics


    【解决方案1】:

    由于您没有提供足够的代码来运行您的程序,我只能提出未经测试的建议。您的问题似乎是这段代码:

    hand_total = Text(Point(100, 200), playerTotal)
    hand_total.draw(win)
    

    以及经销商的匹配dhand_total 代码。每次循环都在创建一个新的文本对象,而不删除旧的。一个快速而肮脏的修复可能是添加以下行:

    hand_total.undraw()
    hand_total = Text(Point(100, 200), playerTotal)
    hand_total.draw(win)
    

    但更好的方法是将两行都替换为更改当前 Text 实例文本而不创建新行的行:

    hand_total.setText(playerTotal)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2017-01-03
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      相关资源
      最近更新 更多