Animations and Interactive Programming

Bouncing Balls

import turtle
import time


def drawCircle(t, r):
    t.penup()
    t.pendown()
    t.begin_fill()
    t.circle(r)
    t.end_fill()


screen = turtle.Screen()
screen.tracer(0)

turtleList = []
goingUpList = []
speedList = []
colors = ("red", "purple", "pink", "orange", "green")

for i in range(len(colors)):
    t = turtle.Turtle()
    t.goto(-len(colors) * 110 / 2 + i * 110, 0)
    t.speed(20)
    t.hideturtle()
    t.fillcolor(colors[i])

    turtleList.append(t)
    goingUpList.append(False)
    speedList.append(i + 3)

while True:
    for i in range(len(turtleList)):
        myTurtle = turtleList[i]
        myTurtle.clear()

        if (goingUpList[i]):
            myTurtle.goto(myTurtle.xcor(), myTurtle.ycor() + speedList[i])
        else:
            myTurtle.goto(myTurtle.xcor(), myTurtle.ycor() - speedList[i])
        drawCircle(myTurtle, 30 + i * 5)
        if (goingUpList[i] and myTurtle.ycor() > 200):
            goingUpList[i] = False
        elif (not goingUpList[i] and myTurtle.ycor() < -300):
            goingUpList[i] = True
    screen.update()
    time.sleep(0.01)