如何用 Turtle(python) 缓慢移动对象?

How do I move an object with Turtle(python) slow?

我一直在尝试做一个小游戏(足球中的点球),球会向侧面移动,直到你按下一个按钮,球会向前移动,直到它与球门柱在同一条线上,但是球是移动太快导致我使用循环。我想知道是否有什么可以减慢速度。谢谢大家


tela = turtle.Screen()
tela.tracer(0)
tela.bgcolor("black")
tela.title("Jogo de Futebol")
tela.setup(width= 800, height= 600)

#  Creating ball
bola = turtle.Turtle()
bola.speed(0)
bola.up()
bola.color("white")
bola.shape("circle")
bola.goto(0, -250)


#  TGoalpost
trave_central = turtle.Turtle()
trave_central.speed(0)
trave_central.up()
trave_central.color("white")
trave_central.shape("square")
trave_central.goto(0, 300)
trave_central.shapesize(stretch_wid=2, stretch_len=15)

#  For ball move foward
def bola_frente():
    y = bola.ycor()
    y += 10
    bola.sety(y)


#   Ball speed

bola.dx = 1
bola.dy = 1
time = 0
#  Loop do jogo


while True:
    tela.update()



#   Making ball go sideways
    bola.setx(bola.xcor() + bola.dx)
    if bola.xcor() > 380:
        bola.dx *= -1
    if bola.xcor() < -380:
        bola.dx *= -1
    if bola.ycor() > 280:
        bola.goto(0, -250)

#   Ball moving foward
    tela.listen()
    tela.onkey(bola_frente, "w ")
    if bola.ycor() != -250:
        bola.dx = 0
        while True:
            bola_frente()

            if bola.ycor() == trave_central.ycor():
                bola.goto(0, -250)
            bola.dx = 1
            break

您始终可以使用 time.sleep(n) 延迟代码的执行,即 n 延迟的秒数。

import time

while True:
    time.sleep(1)
    print("One second has passed!")

当你计算球速时,尝试使用这样的小数:

#   Ball speed

bola.dx = 0.5
bola.dy = 0.5
time = 0

现在它的移动速度是原来的一半。