SCMP 118 – Lab 3: Turtle Animation

Turtle Animation – Bouncing Balls

Goal

To create a simulation of bouncing balls.

Consider:

Step 1 – Animate a moving ball

Consider the code below that makes a round turtle, and moves it across the screen. We are using: https://pythonsandbox.com/. (Must be in Turtle mode)

Run code below: Link

import turtle
import random
import math
screen = turtle.Screen() 
screen.bgcolor('yellow')
#Starting position and rate
ballX=0
ballY=0
dX=2
# create turtle, hide it, move to start position
ball = turtle.Turtle()
ball.penup()
ball.shape('circle')
# Animate!
while ballX<300:
  ball.goto(ballX,ballY)
  ballX+=dX
  1. What is dX? What if we change it?
  2. Can we make the ball reverse direction every time it goes over x > 200 or below x < -200? (Hint: use dX)
  3. Can we add a dY and make the ball move up and down also? Can we bounce it off the top and bottom, e.g y > 200 or y < -200?

Step 2 – Break into functions, add object variables

Now consider some start code that breaks things up into functions, a function to make a box for walls to bounce off of, a function to set up the ball, and a function to animate the ball. It uses something new: object variables to associate a variable with a turtle object.

This code only bounces the ball to right to left. We will also make it go up and down.

Code: Link

import turtle
import random
import math
screen = turtle.Screen() 
screen.bgcolor('yellow')
# Set up boundry box size
xMax=200
xMin=-200
yMax=200
yMin=-200
# Function to draw a box to bounce in
def makeBoundary(right,left,top,bottom):
  # Finish Function...
  return
# Function to setup ball
def setupBall(ball,x,y,dX):
  # Set up starting values
  ball.penup()
  ball.shape('circle')
  # x, y, dX are object variables
  ball.x=x
  ball.y=y
  # We should make this random
  ball.dX=dX
  # Place ball at starting point
  ball.goto(ball.x,ball.y)
# Function to animate ball one step
def animate(ball):
  # use globals for boundries
  global xMax,xMin,yMax,yMin
  # Code here to bounce off the walls
  if ball.x > xMax or ball.x < xMin:
    # Reverse direction
    ball.dX = -ball.dX
  # Move to next position
  ball.x+=ball.dX
  ball.goto(ball.x,ball.y)
  
# Call function to draw box
makeBoundary(xMax,xMin,yMax,yMin)
# create turtle, and initialize it
ball1 = turtle.Turtle()
setupBall(ball1,0,0,2)
# Animate!
while (True):
  animate(ball1);

Let’s review the code:

  • What is “global”?
    • A way for a function to access variables defined outside of function.
  • Why use a global here?
  • Note the ball.x and ball.y variables.
    • Here we are associating new variables with a turtle instance.
    • This allows us to associate a value with an object instance.
  • What is the animate() function?
    • Moves the ball one step (dX and dY).

Step 3 – Put a box around the ball

Add the code to draw a bounding box around the ball.
Do this by drawing the lines:

  • (xMin,yMax) to (xMax,yMax) – top line
  • (xMin,yMin) to (xMax,yMin) – bottom line
  • (xMin,yMax) to (xMin,yMin) – left line
  • (xMax,yMax) to (xMax,yMin) – right line

Step 4 – Add code to bounce the ball up and down

You should add a dY and then move the ball up (and down) bouncing off the top and bottom.

Step 5 – Make dX and dY random values

Add the code to initialize dX and dY to a random values between -5 and 5 at startup.

Step 6 – Fix the the bounce overshoot

Notice that the ball goes slightly beyond the boundary of the box. Fix this.

Step 7 – Add some features

Choose at least two features to add:

  1. Make it so the ball slows down over time (perhaps due to friction)
  2. Make two balls
  3. Make the ball change to a random color every time it hits the wall.
  4. Add gravity so the ball bounces off the floor.. See how here: Link. Also an example here: https://cs.jimskon.com/apps/KinecticJS/Gravity4.html (obviously too complex for now)
  5. Make 2 balls bounce off each other. You can see a simulation here https://cs.jimskon.com/apps/KinecticJS/animateball9.html
  6. Think of something else cool! Turn in a link to the repl.it!

Turn In

  1. Complete the assignments in pythonsandbox.com, and click on the save button (below code), and turn in URL in on Moodle

Grading

RequirementsGrading CommentsPointsScore
One fully operational ball in a rectangle that correctly bounces off the boundaries. 20
Two additional features from the list40
Code broken up into small, single purpose, functions 20
Appropriate code formatting5
Meaningful identifier names (variables, functions)10
Comments at the top, and on all functions5
Total100
Scroll to Top