This program creates a bouncing ball using turtle.goto()
, is there an alternative?
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)
Generally, what is the point of having a goto()
function?
Yes, you can create a bouncing ball without using turtle.goto()
, here's a rough example:
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()