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()

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source