简体   繁体   English

需要帮助在pygame中创建动态更新分数

[英]Need help creating a dynamically updating score in pygame

I'm intigrating my text based blackjack game with pygame. 我正在用pygame来启动基于文本的二十一点游戏。 I can't seem to make the total of the player's hand update. 我似乎无法全部更新玩家的手牌。 Every time it justs adds it over the previous text, and it becomes impossible to read. 每次它只是将其添加到以前的文本中,就变得不可能阅读。

Here are the relevant sections of code: 以下是相关代码部分:

def update(player, comp):
drawText('Money: $%s' % (money), font, windowSurface, 50, 30)
drawText('Press "H" to hit. Press S to stand', font2, windowSurface, 500, (30))
drawText('Player Total: %s' % (sumHand(player)), font2, windowSurface, 500, (50))
drawText('Dealer Total: %s' % (sumHand(comp)), font2, windowSurface, 650, (50))
pygame.display.update()


        while True:
        update(player, comp)
        mainClock.tick(FPS)
        if sumHand(player) < 22:
            pygame.display.update()
            hCount += 1
            print('Your cards are: %s with a total value of %d' % (player,sumHand(player))) #old
            print('The dealers visible card is %s' % (comp)) #old
            print('Hit or Stand?') #old
            for event in pygame.event.get():
                event = waitForPlayerToPressKey()
                if event.key == ord('h') and hCount == 0:
                    player.append(getCard(cards))
                    cardPrint3(player)
                    update(player, comp)
                elif event.key == ord('h') and hCount == 1:
                    player.append(getCard(cards))
                    cardPrint4(player)
                    update(player, comp)
                elif event.key == ord('h') and hCount == 2:
                    player.append(getCard(cards))
                    cardPrint5(player)
                    update(player, comp)
                elif event.key == ord('h') and hCount == 3:
                    player.append(getCard(cards))
                    cardPrint6(player)
                    update(player, comp)
                    break
                    money+=500
                else:
                    break
            else:
                break

Here is a pastebin of the full program if I missed something important. 如果我错过了一些重要的事情,这是完整程序的粘贴框。 http://pastebin.com/70EhteQ1 http://pastebin.com/70EhteQ1

You need to "clear" the screen, or at least the relevant portions of it, before you start drawing again. 在再次开始绘制之前,您需要“清除”屏幕或至少屏幕的相关部分。 Otherwise you are just drawing over the existing drawing. 否则,您只是在现有图形上绘制。 Try this at the top of your loop: 在循环的顶部尝试一下:

while True:
    windowSurface.fill((255, 255, 255)) # this will draw over the entire screen surface with white
    update(player, comp)
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM