我已经制作了一个程序,通过按下键盘来移动 turtle 的位置,但是有没有办法让它以较小的像素增量移动?
还有,有没有可能让 turtle 上下移动,而不是只左右移动?
from turtle import Screen, Turtle
TURTLE_SIZE = 200
# functions
def go_left():
t.direction = 'left'
def go_right():
t.direction = 'right'
screen = Screen()
screen.setup(1152,648)
screen.tracer(0)
# player
t = Turtle()
t.shape("circle")
t.speed("slowest")
t.color("blue")
t.penup()
t.setx(338)
t.sety(0)
t.direction = 'stop'
# Keyboard
screen.onkeypress(go_left, 'Left')
screen.onkeypress(go_right, 'Right')
screen.listen()
while True:
x = t.xcor()
if t.direction == 'left':
if x > TURTLE_SIZE - 576:
x -= 3
t.setx(x)
else:
t.direction = 'stop'
elif t.direction == 'right':
if x < 576 - TURTLE_SIZE:
x += 3
t.setx(x)
else:
t.direction = 'stop'
screen.update()
screen.mainloop()