0

I'm a beginner programmer and followed a tutorial for a pong game using the turtle module in python. I noticed that the movement was janky and wanted to smooth it out.

Here is the movement code:

   def paddleaup():
    y = paddlea.ycor()
    y += 20
    paddlea.sety(y)

def paddleadown():
    y = paddlea.ycor()
    y -= 20
    paddlea.sety(y)


def paddlebup():
    y = paddleb.ycor()
    y += 20
    paddleb.sety(y)


def paddlebdown():
    y = paddleb.ycor()
    y -= 20
    paddleb.sety(y)

Any suggestions on making it smoother and less jumpy?

Thank you for your time.

vaco234
  • 1
  • 1
  • Why `20`, have you tried smaller values? https://en.wikipedia.org/wiki/Magic_number_%28programming%29 – Doyousketch2 Jun 05 '21 at 19:58
  • That was even jankier... could it be something to do with the keybind code? wn.listen() wn.onkeypress(paddleaup, "w") wn.onkeypress(paddleadown, "s") wn.onkeypress(paddlebup, "Up") wn.onkeypress(paddlebdown, "Down") – vaco234 Jun 06 '21 at 02:21
  • Do you have a delay in your draw routine or left out a `screen.update()`? – Doyousketch2 Jun 06 '21 at 04:23
  • Does this answer your question? [How to fix inconsistent frame rate (speed) in python turtle](https://stackoverflow.com/questions/55495581/how-to-fix-inconsistent-frame-rate-speed-in-python-turtle) – ggorlen Jan 20 '23 at 18:27

2 Answers2

2

I am pretty new to programing myself but i found a rather smooth way of making a turtle walk across the screen without it "jumping" around, well its as smooth as i could get it.

import turtle
move = True

tim = turtle.Turtle("turtle")
tim.color("black","light green")
tim.penup()

window = turtle.Screen()

def up():
    global move
    if move == True:
        move = False
        tim.setheading(90)
        tim.forward(30)
        move = True

def down():
    global move
    if move == True:
        move = False
        tim.setheading(270)
        tim.forward(30)
        move = True

def left():
    global move
    if move == True:
        move = False
        tim.setheading(180)
        tim.forward(30)
        move = True

def right():
    global move
    if move == True:
        move = False
        tim.setheading(0)
        tim.forward(30)
        move = True

window.listen()
turtle.onkeypress(up, "w")  
turtle.onkeypress(down, "s")
turtle.onkeypress(left, "a")
turtle.onkeypress(right, "d")

turtle.mainloop()

By just seeing the peice of code its simple to understand but imagine it as once you press the button, every moment it is pressed your telling the turtle to move in that direction while its pressed. It jumps becouse its constantly being told to move before its even done moving. By doing this whole True and False thing allows the turtle to move, then move once the first action is done.

feel free to test the code and see for yourself! There is probably a better way of doing this but i hope this helped somehow.

Alexander
  • 21
  • 2
0

Python turtle will always be 'jumpy' but what you should do is use turtle.tracer() and turtle.update()

e.g

turtle.tracer(1)
turtle.forward(100)
turtle.update()

Learn more here and here

SecretLloyd
  • 109
  • 1
  • 13