簡體   English   中英

為什么我的記分板上的分數沒有更新?

[英]Why isn't the score updating on my scoreboard?

我需要幫助,為什么當 Python 龜與食物碰撞時我的分數沒有更新。 我還在下面發布了一個包含完整代碼的鏈接:

if turtle.distance(food) < 20:
            food.goto(randint(-280, 280), randint(-280, 280))
            # Adding sections
            new_section = Turtle()# Turtle() is the same as turtle.Turtle()
            new_section.shape('square')
            new_section.speed(0)
            new_section.color('orange', 'grey')
            new_section.penup()
            new_section.goto(old_position)
            sections.append(new_section)# Adds new section of body at the end of body

            # Score
            score = 0
            high_score = 0

            # Increase the score
            score = + 10


            if score > high_score:
                high_score = score
            pen.clear()
            pen.write("Score: {} High Score: {}".format(score, high_score), align="center",font=("Courier", 24, "normal"))

***Need help on updating score have also posted link below***


    screen.update()
    screen.ontimer(move, DELAY)

pastebin.com獲取完整代碼。

scorehighscore都是move()本地的,並且每次運行時都重新設置為零。 它們需要是全局的,並聲明為global

如果我們從分數的角度來看move() ,它看起來像:

def move():
    score = 0
    high_score = 0
    # Increase the score
    score = + 10
    if score > high_score:
        high_score = score
    pen.write("Score: {} High Score: {}".format(score, high_score), ...))

其中scorehighscore對於move()本地的。 我們真正期待的更像是:

score = 0
high_score = 0

def move():
    global score, high_score

    # Increase the score
    score += 10
    if score > high_score:
        high_score = score
    pen.write("Score: {} High Score: {}".format(score, high_score), ...))

閱讀有關 Python global關鍵字和 Python 全局變量的一般信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM