how to make objects bounce in trutle
import turtle
from time import sleep
# Screen setup
wn = turtle.Screen()
wn.setup(width = 500, height = 500)
wn.tracer(0)
# Object setup (The ball)
ball = turtle.Turtle()
ball.color("black")
ball.shape("circle")
wn.update()
running = True
# you can change the shapes by changing the value of x and y
x = 5
y = 2
def bounce():
global x,y
ball.setx(ball.xcor() + x)
ball.sety(ball.ycor() + y)
if ball.ycor() > 250:
ball.sety(250)
y = y * (-1)
elif ball.ycor() < -256:
ball.sety(-256)
y = y * (-1)
elif ball.xcor() > 250:
ball.setx(250)
x = x * (-1)
elif ball.xcor() < -256:
ball.setx(-256)
x = x * (-1)
def end():
global running
running = False
def stop():
global end
wn.onkeypress(end,"Escape")
wn.listen()
# * if you want to let the ball stop drawing press escape
while running == True:
stop()
bounce()
sleep(0.002)
wn.update()