I'm trying to make an object (dot) float around the screen smoothly using the turtle library, but the movement on that is being produced right now is very "laggy" and "jumpy". Is there any way to fix this? If not, is there another library I can use to animate a dot floating around the screen?
# screen
Screen = turtle.Screen()
Screen.title("Moving Block")
Screen.bgcolor("black")
Screen.setup(width=1000, height=700)
# game
Block = turtle.Turtle()
Block.speed(0)
Block.shape("square")
Block.color("grey")
Block.penup()
Block.turtlesize(5,5)
Block.goto(0, 0)
# movement
def go_up():
y = Block.ycor()
if y <= 290:
Block.sety(y 10)
def go_down():
y = Block.ycor()
if y >= -280:
Block.sety(y - 10)
def go_left():
x = Block.xcor()
if x >= -440:
Block.setx(x - 10)
def go_right():
x = Block.xcor()
if x <= 430:
Block.setx(x 10)
# keyboard
Screen.onkey(go_up, "Up")
Screen.onkey(go_down, "Down")
Screen.onkey(go_left, "Left")
Screen.onkey(go_right, "Right")
Screen.listen()
Screen.mainloop()
CodePudding user response:
I do just want to go ahead and say that since turtle is a GUI module for beginners, there are lots of limitations to customization and speed.
That being said, you can use onkeypress
instead of onkey
to be able to hold a button down.
It still has a little glitch but that's mainly the limitation of the keyboard and not anything you can really control.
However, if you were to change the amount of space the block moves each time, it will look smoother. You just have to substitute speed for smoothness.
delta = 5 #change this to make it smoother or faster. Small numbers make it smoother. Large numbers make it faster.
# movement
def go_up():
y = Block.ycor()
if y <= 290:
Block.sety(y delta)
def go_down():
y = Block.ycor()
if y >= -280:
Block.sety(y - delta)
def go_left():
x = Block.xcor()
if x >= -440:
Block.setx(x - delta)
def go_right():
x = Block.xcor()
if x <= 430:
Block.setx(x delta)
# keyboard
Screen.onkeypress(go_up, "Up")
Screen.onkeypress(go_down, "Down")
Screen.onkeypress(go_left, "Left")
Screen.onkeypress(go_right, "Right")
Hope this helps!
CodePudding user response:
Below is the generic tracer(0)
and update()
approach, see if it's sufficient to your needs. One significant issue with "when I hold the button" is that the operating system can step in to control the rate of key repeat, of which Python is unaware:
from turtle import Screen, Turtle
# movement
def go_up():
if (y := block.ycor()) <= 290:
block.sety(y 10)
screen.update()
def go_down():
if (y := block.ycor()) >= -280:
block.sety(y - 10)
screen.update()
def go_left():
if block.xcor() >= -440:
block.backward(10)
screen.update()
def go_right():
if block.xcor() <= 430:
block.forward(10)
screen.update()
# game
block = Turtle('square')
block.color('grey')
block.penup()
block.turtlesize(5)
# screen
screen = Screen()
screen.setup(width=1000, height=700)
screen.title("Moving block")
screen.bgcolor('black')
screen.tracer(False)
# keyboard
screen.onkey(go_up, 'Up')
screen.onkey(go_down, 'Down')
screen.onkey(go_left, 'Left')
screen.onkey(go_right, 'Right')
screen.listen()
screen.mainloop()