python海龟波动写什么错?

时间:2020-07-05 13:43:16

标签: python turtle-graphics

我使用“ 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()

1 个答案:

答案 0 :(得分:0)

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

    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。将乌龟一次放置在代码初始化中。隐藏它,然后放笔。在代码的运行部分,您只需要该乌龟即可撤消和编写,仅此而已。

# 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"))

Ditto代表“生命”等