Search code examples
python-3.xpython-turtle

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


I have been trying to do a little game(penalty in soccer) where the ball is moving sideways until you press a botton and the ball moves foward untill it is in the same line as the goalpost, however the ball is moving too fast cause Im using a loop. I would like to know if there is something that could slow. thanks yall


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

Solution

  • When you are making the ball speed try using decimals like this:

    #   Ball speed
    
    bola.dx = 0.5
    bola.dy = 0.5
    time = 0
    

    Now its moving half the original speed.