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?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types