CSC 111: Introduction to Computer Science
Lab 4: Bouncing Shapes
Thursday, Feb 17, 2011
Create Animated Bouncing Shape(s)
For the lab this week, try Pair Programming. A short article on Pair Programming, with some sections strategically highlighted, is available
here.
Your lab project is to write a program that creates and graphs a very simple bouncing shape. Include the following in your program design:
- You will definitely want to use the .setCoords() method to make your graphics manipulation more manageable.
- Put a title at the top and inside the graphics window, using a Text object (which is different from the label you give the window
when you create the graphics window).
- Create and use any shape you like - you are not restricted to having only balls bounce in your personal Python world
- Rectangle
- Oval
- Your own polygon
- or a circle
- A good place to think about using a variable, so that you can vary it as you work with your program, is the SIZE of your shape.
If this doesn't make sense at first, keep it in the back of your mind as you work on the program.
- Use the random library to start your shape at some random point. You will want to restrict the options of the random starting
position, so the shape will be assured of having some distance to fall.
- You might want to start with only the 'y' coordinate being random, and predefine the 'x' coordinate
- Have your shape fall (i.e., move) to the bottom of your graphics window, and then bounce (or move) up to some new position
- You can adhere to actual physics, in which case your shape cannot bounce higher than the original position,
- You can have your own personal Python physics in which your shape could bounce anywhere
- You can have it bounce up and down in one line, such that the 'x' coordinate does not change, or you could have both 'x' and 'y'
coordinates changing...
- ...Or you could start with the simplest version, and add complexity as you get the first parts working
- To determine the number of bounces, you could
- Obtain the value from the user
- Randomly generate a (small) number
- "Hard wire" it into your code (using a variable name)
- Use the time.sleep() method so you can comfortably observe your shape doing its thing.
One way to start framing out your program (much as you always outline a paper before writing!), is to write in the parts you know you'll
need, and include comments to set up your program flow. One initial example of this is
# Your header comments
from graphics import *
from random import *
from time import *
def main():
# Create and label graphics window, and setCoords()
# Generate random starting position
# Draw shape in starting position
# Use 'for' loop to create the bounces of the shape
main()
A possible solution for a jumping, or bouncing ball:
BallJump.py