Turtle Animation - Bouncing Balls

Lab 3

Due Feb 19, 11:55pm

Moodle Link

Goal

To create an 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:

Let's review the code:

  • What is "global"?
    • More... Close Global allows functions to access variables defined outside the function. Useful in certain applications to avoid passing too many variables.
  • What is dX and dY?
    • More... Close This is the amount to move the ball in the X or Y direction each time. We are picking a randoms value. A negative value will move things backword. Try it!
  • What is the animate() function?
    • More... Close This function gets called everytime you want to update the animation. It uses the dX and dY to update the location of the ball.
Fork the program. Play around with the "distance".

Try this: Make the loop run forever. Negate Dx and Dy if ballX is every greater then 100, or ballX is ever less then -100.

Step 2 - Put a box around the ball

Add the following code to draw a bounding box around the ball

sWidth=400
sHeight=400

def makeBoundry(width,height):
  boundry=turtle.Turtle()
  boundry.penup()
  boundry.speed(5)
  boundry.hideturtle()
  boundry.goto(-width//2,-height//2)
  boundry.color("white")
  boundry.down()
  for i in range(4):
    boundry.forward(width)
    boundry.left(90)

makeBoundry(sWidth,sHeight)

Step 3 - Create a timer for the animation

Simply running a loop is problematic because it wastes time running continuously. This rules out capturing user events like keyboard presses and mouse clicks. What we can do is set up a timer that triggers a function to be called after a given amount of time. The timer is based on milliseconds (1/1000 of a second). Try the code below (click on the colsole to see the resulting message:

Note that the function call screen.ontimer(animate,1000) causes the function animate to be called after 1000 milliseconds (one second). Then in the function animate the command isdone again, scheduling another call one second after the previous, keeping things going.

Step 4 - Add a timer to the ball animation

Next we wish to use the timer to cause the ball to animate (move). Remove the for loop from the code, and add in the time code to animate the ball. Change the ball speed to 0 so that the built in ball animation doesnot slow down the animation.

Step 5 - Make the ball bounce off the walls of the boundry box.

What happens when a ball hits a wall?

  • More... Close The direction of the ball reverses. Thus if the ball hits a side wall dX becomes -dX and if it hits a bottom or top wall dY becomes -dY.
How do we know when ball hits a wall?
  • More... Close If the ball x or y value exceeds the wall dimensions.
What are the wall dimensions?
  • More... Close The top is sWidth//2, the bottomis -sWidth//2, the right is sHeight//2, and the bottom is -sHeight//2
Add code to the animate that determine if a bountry has been hit, and reverses the direction of the ball.

Step 6 - Fix the the bounce over-shoot

Notice that the ball goes slightly beyond the boundry of the screen. 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 it so when you do a mouse click speeds the ball up, or down, or changes it randomly (see: https://repl.it/@JimSkon/TurtleMouseClickExample)
  3. Add a bumper: add a fixed shape (like a circle or square) somewhere in the boundries. Have the ball bounce off this.
  4. Add a boundry appear when you click on the screen and have it bounce off this (only one at a time is reasonable). You could make it so a box appear wher you click, and remains until the ball hits it and bounces. The box then disappears, and you canmake another.
  5. Make the ball change to a random color everytime it hits the wall
  6. Add gravity so the ball bounces off the floor.. See how here: https://processing.org/examples/bouncingball.html. Also an example here: https://cs.kenyon.edu/apps/KinecticJS/Gravity4.html (obviously too complete for now)
  7. Add a second ball
  8. Make 2 balls boune off each other. You can see a simulation here https://cs.kenyon.edu/apps/KinecticJS/animateball9.html
  9. Think of something else cool!
Turn in a link to the repl.it!

Solutions

Edit | Attach | Watch | Print version | History: r11 | r9 < r8 < r7 < r6 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r7 - 2019-02-09 - JimSkon
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback