python 乌龟波动写错了?

python turtle fluctuation on write whats wrong?

我使用“mypen.undo()”来避免覆盖分数和生命值,但我的乌龟在我的屏幕上波动(输出)。当我删除 a 但它覆盖时,它会以某种方式得到改进。也许是关于“为了”和“如果”。如何避免不覆盖和波动(输出)??

import turtle
import math
import random
import winsound

#Collision Checking Goals
        if isCollision(player, goals[count]):
            goals[count].setposition(random.randint(-300,300), random.randint(-300, 300))
            goals[count].right(random.randint(0,360))
            score += 10


    #Move the wrongs
    for wcount in range(maxWrongs):
        wrongs[wcount].forward(3)

        #Boundary Checking
        if wrongs[wcount].xcor() > 290 or wrongs[wcount].xcor() < -290:
            wrongs[wcount].right(100)
        
        if wrongs[wcount].ycor() > 290 or wrongs[wcount].ycor() < -290:
            wrongs[wcount].right(100)
            
        #Collision Checking Wrongs
        if isCollision(player, wrongs[wcount]):
            wrongs[wcount].setposition(random.randint(-300,300), random.randint(-300, 300))
            wrongs[wcount].right(random.randint(0,360))
            
            score -= 20
            life -= 1

        #Draw the score on the screen
        mypen.undo()
        mypen.penup()
        mypen.hideturtle()
        mypen.setposition(-290,310)
        scorestring = "Score: %s" %score
        mypen.write(scorestring, False, align="left", font=("Arial",14,"normal"))

        #Draw the Life on the screen
        mypen.undo()
        mypen.penup()
        mypen.hideturtle()
        mypen.setposition(230,310)
        wlife = "Life: %s" %life
        mypen.write(wlife, False, align="left", font=("Arial",14,"normal"))

        if life<1:
            mypen.undo()
            mypen.penup()
            mypen.hideturtle()
            mypen.setposition(-140.951,-20.117)
            end = "END GAME"
            mypen.write(end, align="left", font=("Arial",40,"normal"))
            scorestring = "Score: %s" %score
            mypen.setposition(-90.540,-62.596)
            mypen.write(scorestring, align="left", font=("Arial",28,"normal"))
            turtle.stop()

没有适当的最小可运行代码示例,我无法给出明确的答案,但基于我所看到的:

    mypen.undo()
    mypen.penup()
    mypen.hideturtle()
    mypen.setposition(-290,310)
    scorestring = "Score: %s" %score
    mypen.write(scorestring, False, align="left", font=("Arial",14,"normal"))

我的建议是不要重复使用 mypen。而是为每个静态文本元素创建一个,例如mypen_score。将那只乌龟 once 放在代码的初始化中。把它藏起来,把它的笔举起来。在你代码的运行部分,你只需要那只乌龟来撤销和写入,仅此而已。

# initialization time
mypen_score = Turtle()
mypen_score.hideturtle()
mypen_score.penup()
mypen_score.setposition(-290, 310)
mypen_score.write("Score: 0", font=("Arial", 14, "normal"))

   # main loop time
    mypen_score.undo()
    scorestring = "Score: %s" % score
    mypen_score.write(scorestring, font=("Arial", 14, "normal"))

“生命”等同上