Difference between revisions of "CSC111 Lab 8"
(→Moving Ball, Part 1) |
(→Moving Balls, Part 2) |
||
Line 20: | Line 20: | ||
* You will need to create a new circle, say '''c2''' in main(), and pass it to the '''simul()''' function as well. But make sure '''simul()''' contains only one for-loop. The same for-loop should move both balls around. | * You will need to create a new circle, say '''c2''' in main(), and pass it to the '''simul()''' function as well. But make sure '''simul()''' contains only one for-loop. The same for-loop should move both balls around. | ||
+ | |||
+ | :Your program will have a structure close to this one (but you may be inspired to organize your program differently): | ||
+ | |||
+ | <code><pre> | ||
+ | #---------------------------------------------------------------- | ||
+ | def simul( c1, dx1, dy1, c2, dx2, dy2 ): | ||
+ | |||
+ | for step in range( 2000 ): | ||
+ | ... | ||
+ | ... | ||
+ | ... | ||
+ | |||
+ | #---------------------------------------------------------------- | ||
+ | def main(): | ||
+ | c1 = Circle( Point( ..., ... ), 15 ) | ||
+ | c2 = Circle( Point( ..., ... ), 15 ) | ||
+ | ... | ||
+ | |||
+ | waitForClick( win, "Click to Start" ) | ||
+ | dx1 = ... | ||
+ | dy1 = ... | ||
+ | dx2 = ... | ||
+ | dy2 = ... | ||
+ | simul( c1, dx1, dy1, c2, dx2, dy2 ) | ||
+ | |||
+ | ... | ||
+ | |||
+ | </pre></code> |
Revision as of 13:22, 24 March 2010
This lab deals with the moving ball example we saw in class recently. Your work for today is to add new functionality similarly to the way we added functionality to the graphics example with buttons.
You should start the Mac in Mac OS mode for today (graphics is more easily done this way). Make sure you start the X11 utility first, then the Terminal window.
Moving Ball, Part 1
- Create a program called movingBall.py, which contains the version we ended up in class yesterday. It is available here.
- make sure the graphics.py library is in the same directory where your program is located.
- Run the program and make sure it works.
- Clean up the testing of x and y coordinates of the center, in the function simul(), so that it is simpler. Use AND or OR to simplify the testing.
- Verify that your ball still moves well. You may increase the number of simulation steps in simul to make the simulation go longer.
Moving Balls, Part 2
- Add an extra ball to the simulation. The result should be two balls moving around the box and bouncing off the sides.
- You will need to create a new circle, say c2 in main(), and pass it to the simul() function as well. But make sure simul() contains only one for-loop. The same for-loop should move both balls around.
- Your program will have a structure close to this one (but you may be inspired to organize your program differently):
#----------------------------------------------------------------
def simul( c1, dx1, dy1, c2, dx2, dy2 ):
for step in range( 2000 ):
...
...
...
#----------------------------------------------------------------
def main():
c1 = Circle( Point( ..., ... ), 15 )
c2 = Circle( Point( ..., ... ), 15 )
...
waitForClick( win, "Click to Start" )
dx1 = ...
dy1 = ...
dx2 = ...
dy2 = ...
simul( c1, dx1, dy1, c2, dx2, dy2 )
...