CS 151: Lab #7

Lab Exercise 7: Organizing Objects

In this lab we'll take a look at the animation aspect of the current project. In particular, we'll look at how you might want to organize your graphics objects so that animation and manipulation are easy to do. We'll also take a look at the image manipulation tools in the ImageMagick package (not a part of python).


Tasks

  1. Connect to your network directory.
  2. Make a copy of your file from the current project that contains the building function definition. Call it city3.py. Strip the file of everything but the one building function and any functions it requires to run. Alternatively, you can download my simple building code and use it.
  3. Download the file scape.py and run it. If your building function is working properly, it should create a window, draw a building and then wait for the user to click the mouse.
  4. Now we want to modify the building function so that it stores all of its constituent objects in a list and returns the list. First, create an empty list at the beginning of the building function, the append each object you draw to the list. You don't want to store objects like Points that you only use to create other objects. At the end of the function, return the list of graphics objects.
  5. Back in the main function in scape.py, we want to capture the list returned by the building function, so assign the output of the building function to a variable. This variable will hold the list of objects constituting the building. Run the program just to make sure the functionality has not changed.
  6. Now we want to separate the building creation from the building drawing. Create a function in building.py called buildingDraw. The function should take in a list of building components and a window in which to draw them. To draw all of the components, just use a for loop that iterates over the elements in the building list and calls the draw method for each one. Remove all of the draw method calls from the building function. Add a call to buildingDraw() in the main function in scape.py after the building is created, then run the program to ensure functionality is the same.
  7. Now let's make a function that enables us to move an entire building by moving each of the constituent parts by the same amount. Put the function in city3.py and call it buildingMove. The function should take three parameters: the list of the building components and how much to move in x and y. Inside the function, loop over the objects in the building list and call the move method for each one, applying the x and y motion parameters. Back in the main function in scape.py, have the building move to a new location after a mouse click.
  8. Next, make a function that clones all of the elements of a building and returns a list with all of the cloned objects. Call the function buildingClone. The function should just take the list of building objects as its argument. Inside the function, have a for loop clone each element of the list and append it to a new list that is set to the empty list before the loop starts. In the main function, add a call to buildingClone and store the cloned building in a new list. Then call buildingMove followed by buildingDraw on the new list. Run the program to make sure it works.
  9. Finally, we want to animate the building(s). Create a function buildingAnimate that takes in the list of building components and modifies one or more of the components appropriately (like setFill(), for example). Back in the main function, call buildingAnimate inside a while loop that waits for the user to click the mouse.
        while win.checkMouse() == None:
            city3.buildingAnimate(bldg);
            time.sleep(random.random()*0.2)
    

    Remember that you'll need to import the time package in order to call the sleep function. Run your program to see how it works. When it works with one building, put calls to buildingAnimate for both of the buildings inside the loop, possibly with a short sleep in between.

Now you have some nicely organized code for creating and manipulating buildings that consist of lots of objects.

Once you've finished the above exercise, keep going on the current project.