Animation: Bouncing ballΒΆ

"""
Bounce a ball on the screen, using gravity.
"""

# --- Set up the constants

# Size of the screen
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Size of the circle.

# How strong the gravity is.
GRAVITY_CONSTANT = 0.3

# Percent of velocity maintained on a bounce.
BOUNCINESS = 0.9

def draw(delta_time):
"""
Use this function to draw everything to the screen.
"""

# Start the render. This must happen before any drawing
# commands. We do NOT need an stop render command.

# Draw our rectangle

# Modify rectangles position based on the delta
# vector. (Delta means change. You can also think
# of this as our speed and direction.)
draw.x += draw.delta_x
draw.y += draw.delta_y

draw.delta_y -= GRAVITY_CONSTANT

# Figure out if we hit the left or right edge and need to reverse.
if draw.x < CIRCLE_RADIUS and draw.delta_x < 0:
draw.delta_x *= -BOUNCINESS
elif draw.x > SCREEN_WIDTH - CIRCLE_RADIUS and draw.delta_x > 0:
draw.delta_x *= -BOUNCINESS

# See if we hit the bottom
if draw.y < CIRCLE_RADIUS and draw.delta_y < 0:
# If we bounce with a decent velocity, do a normal bounce.
# Otherwise we won't have enough time resolution to accurate represent
# the bounce and it will bounce forever. So we'll divide the bounciness
# by half to let it settle out.
if draw.delta_y * -1 > GRAVITY_CONSTANT * 15:
draw.delta_y *= -BOUNCINESS
else:
draw.delta_y *= -BOUNCINESS / 2

# Below are function-specific variables. Before we use them
# in our function, we need to give them initial values. Then
# the values will persist between function calls.
#
# In other languages, we'd declare the variables as 'static' inside the
# function to get that same functionality.
#
# Later on, we'll use 'classes' to track position and velocity for multiple
# objects.
draw.delta_x = 2
draw.delta_y = 0

def main():
# Open up our window

# Tell the computer to call the draw command at the specified interval.