在 Python 中使用 turtle.goto() 的替代方法
Alternative to using turtle.goto() in Python
此程序使用 turtle.goto()
创建弹跳球,是否有替代方案?
from random import *
from turtle import *
from base import vector
# The ball is drawn, the actions are used from the base file
def draw():
ball.move(aim)
x = ball.x
y = ball.y
if x < -200 or x > 200:
aim.x = -aim.x
if y < -200 or y > 200:
aim.y = -aim.y
clear()
# Can this program serve a purpose without it?
goto(x, y)
dot(10, 'blue')
ontimer(draw,50)
一般来说,goto()
函数有什么意义?
是的,您可以在不使用 turtle.goto()
的情况下创建弹跳球,这是一个粗略的示例:
from turtle import Screen, Turtle
def draw():
ball.forward(10)
x, y = ball.position()
if not (-200 < x < 200 and -200 < y < 200):
ball.undo() # undo 'forward()'
ball.setheading(ball.heading() + 90)
ball.forward(10)
screen.update()
screen.ontimer(draw, 50)
screen = Screen()
screen.tracer(False)
box = Turtle('square')
box.hideturtle()
box.fillcolor('white')
box.shapesize(20)
box.stamp()
ball = Turtle('circle')
ball.color('blue')
ball.setheading(30)
ball.penup()
draw()
screen.exitonclick()
此程序使用 turtle.goto()
创建弹跳球,是否有替代方案?
from random import *
from turtle import *
from base import vector
# The ball is drawn, the actions are used from the base file
def draw():
ball.move(aim)
x = ball.x
y = ball.y
if x < -200 or x > 200:
aim.x = -aim.x
if y < -200 or y > 200:
aim.y = -aim.y
clear()
# Can this program serve a purpose without it?
goto(x, y)
dot(10, 'blue')
ontimer(draw,50)
一般来说,goto()
函数有什么意义?
是的,您可以在不使用 turtle.goto()
的情况下创建弹跳球,这是一个粗略的示例:
from turtle import Screen, Turtle
def draw():
ball.forward(10)
x, y = ball.position()
if not (-200 < x < 200 and -200 < y < 200):
ball.undo() # undo 'forward()'
ball.setheading(ball.heading() + 90)
ball.forward(10)
screen.update()
screen.ontimer(draw, 50)
screen = Screen()
screen.tracer(False)
box = Turtle('square')
box.hideturtle()
box.fillcolor('white')
box.shapesize(20)
box.stamp()
ball = Turtle('circle')
ball.color('blue')
ball.setheading(30)
ball.penup()
draw()
screen.exitonclick()