forked from eskayML/turtle-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBouncing Ball.py
45 lines (35 loc) · 845 Bytes
/
Bouncing Ball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import turtle
# Set up the screen
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("Bouncing Ball Animation")
# Create a Turtle object for drawing the ball
ball = turtle.Turtle()
ball.shape("circle")
ball.color("blue")
ball.penup()
ball.goto(0, -200)
ball.speed(0)
# Set initial ball movement parameters
ball.dx = 2
ball.dy = 2
# Animation loop
while True:
# Move the ball
ball.sety(ball.ycor() + ball.dy)
ball.setx(ball.xcor() + ball.dx)
# Bounce the ball off the top and bottom
if ball.ycor() > 200:
ball.sety(200)
ball.dy *= -1
if ball.ycor() < -200:
ball.sety(-200)
ball.dy *= -1
# Bounce the ball off the sides
if ball.xcor() > 200:
ball.setx(200)
ball.dx *= -1
if ball.xcor() < -200:
ball.setx(-200)
ball.dx *= -1
wn.mainloop()