Lab Exercise 6: Object Collections
The purpose of this lab is to get you working with the Zelle graphics objects, which are a somewhat higher level graphics package than the turtle. Using the Zelle graphics primitives you can create more complex objects. A scene is then a collection of complex objects.
- Create a new directory for today's lab, download the graphics.py file, start up a terminal and cd to your working directory. Open a new python file and call it lab6.py. We'll use this file to experiment with the graphics package. Start by importing the graphics, time, and random packages.
Define a main function. Then flesh out your main function as below.
def main(): # assign to win a GraphWin object made with appropriate parameters # assign to c a circle object made with appropriate parameters # assign to l a line object made with appropriate parameters # assign to r a rectangle object made with appropriate parameters # assign to objlist a list containing c, l, and r # for each object in objlist # call the draw method of the object, passing in win # while True # call time.sleep with a half-second delay # for each object in objlist # assign to dx a random integer between -10 and 10 # assign to dy a random integer between -10 and 10 # call the move method of the object, passing in dx and dy # if win.checkMouse() is True # break out of the while loop # close the window
Set up a call to your main function inside the conditional that runs only if the file is run on the command line. Then test your program. The shapes should bounce around the screen in Brownian motion.
- Make the three objects different colors. Give them different outline colors. Then make the line width thicker. Try your program again. Set up your program so that each object takes on a different color every half-second. Now you should have a better idea of what you can do with the Zelle graphics objects.
Make a new python file called clubhouse.py. Import the graphics, time and
random packages. Then define a function called clubhouseInit(x, y,
scale). The function should have the following overall structure.
def clubhouseInit(x, y, scale): # assign to obj an empty list # assign to r a big rectangle for the clubhouse # color the rectangle grey # give the rectangle a black outline # append r to obj # assign to r a small rectangle for one window, placed appropriately # color the rectangle yellow # give the rectangle a black outline # append r to obj # assign to r a small rectangle for another window, placed appropriately # color the rectangle yellow # give the rectangle a black outline # append r to obj # return obj
Note the structure of the above function. First, create an empty list. Second, build all of the individual objects needed to create the aggregate object. For each of those objects, append it to the list. Third, return the list. All of the primitive objects will be contained in the list returned by the function.
- Create a new function called clubhouseAnimate( objlist ) that loops through the window objects in the list and randomly switches them to either yellow (200, 190, 100) or dark (40, 50, 60). You can use a slice like mylist[1:] to loop over all the elements in a list except the first one.
- Create a main function that creates a GraphWin, calls clubhouseInit and assigns its return value to a variable like cbhouse. Then loop over the variable and call the draw method on each primitive object. Then call the getMouse and close methods of your GraphWin object. Test your clubhouse.
If all of your complex objects have the same structure--they are all
lists of primitive objects--then we should be able to write some
functions to draw them, move them, undraw them, and clone them. Each
of these functions just needs to loop over the elements in the object
list and call the appropriate method.
To encapsulate this functionality, create a new file aggregate.py and build functions for draw, move, and undraw. The skeletons are below.
def draw( objlist, win ): # for each thing in objlist # call the draw method on thing with win as the argument def move( objlist, dx, dy ): # for each item in objlist # call the move method on item with dx and dy as arguments def undraw( objlist ): # for each thing in objlist # call the undraw method on thing
When you are done with that, go back to the clubhouse.py file and your main function. Import aggregate, then replace the for loop that calls draw with a call to aggregate.draw( cbhouse ). Test it out.
As a final test, download and run lab6test.py . It will import your clubhouse and aggregate modules and create a simple animated scene.
At the end of this lab, you will have the files clubhouse.py, aggregate.py, and lab6.py. The aggregate.py file will be part of your project this week. The clubhouse.py is a template for you to design other complex scene elements. If you make it more interesting, you may use clubhouse.py as part of your project.
Once you have finished the lab, go ahead and get started on project 6.