Skip to main content
 首页 » 编程设计

python之更新记分员

2025年05月04日8bhlsheji

我目前在使用 Python 更新我的基本游戏的 GUI 分数时遇到了很多困难。

我目前的代码显示在左上角,分数显示为“O”。然而,在整个程序中,分数保持在 0(因为它只被渲染一次。我不确定如何不断地重新渲染分数来更新它?我已经尝试了很多方法并且一直不成功。带有分数渲染的代码部分以及我使用变量 conlisionNumber 跟踪分数的尝试是:

new = bears[:] 
    for bear in new: #this makes a copy of the array 
        if player.colliderect(bear): 
            windowSurface.blit(bearImageTwo, bear) 
            windowSurface.blit(playerImageTwo, player) 
 
        def explosion(): 
            for bear in bears: 
                if player.colliderect(bear) and (moveLeft == False and moveRight == False and moveUp == False and moveDown == False): 
                    bears.remove(bear) 
                    colisionNumber += 1 
 
        if player.colliderect(bear) and (moveLeft == False and moveRight == False and moveUp == False and moveDown == False): 
                t = Timer(1, explosion) 
                t.start() 
 
    scoreFont = pygame.font.SysFont("impact", 20) 
    score = scoreFont.render("SCORE:" + str(colisionNumber), True, (192,192,192)) 
    windowSurface.blit(score, (10,10)) 

请注意:在程序的开头我放了:

global colisionNumber 
colisionNumber = 0 

这样做是因为如果我在 explosion 中定义了 colisionNumber,那么 score 变量中就不会定义 colisionNumber。

如有任何帮助,我们将不胜感激。

请您参考如下方法:

colisionNumber = 0 
 
def explosion(): 
    global colisionNumber 
    colisionNumber += 1