CS 151: Lab #6

Lab Exercise 6: Zelle Graphics Objects

This lab exercise is intended to give you some practice with the Zelle graphics package. Since this is a self-paced exercise, a solution for each step is provided. Try to implement each step on your own before looking at the solution. If you run into trouble, or get stuck on a step for more than 10-15 minutes, look at the solution and move on, trying to understand why you were stuck.


Tasks

  1. Connect to your network directory.
  2. Download the graphics.py file from the Zelle website. Put it in your working directory for this lab.
  3. Create a new file in the editor of your choice. Call it graphtest.py. Within the file, import the graphics package and create a main function. Make sure you call the main function at the end of the program.
  4. Within the main function, create a window, wait for a mouse click, then close the window. The window object is called GraphWin, the window method that waits for a mouse click is getMouse(), and the method close() closes a window.

    step1.py

  5. After creating the window in the main function, create a Line, a Rectangle, and a Circle. When creating the objects, try creating a couple of Point objects and use them to initialize the other objects. The documentation site for the graphics package contains all of the information on parameters for the object constructors and object methods.

    Once the object have been created, call the draw method for each one.

    step2.py

  6. After drawing the three shapes, initialize a variable to the empty list. Then enter a for loop for some number of iterations (like 10). Each time through the for loop ask the user to click in the image, create a Circle at that location, store the reference to the circle object in the list, and draw the circle in the image.

    step3.py

  7. Now we want the circles to wander randomly around the image without leaving it. Have the program enter a while loop that goes forever. Within the while loop, use a for loop to go over each circle in the list generated in the prior step. For each circle, generate a random offset of up to 3 pixels in X and Y. Move the circle by its offset, unless it would send the circle outside the image. At the end of the while loop, after processing all of the circles, use a checkMouse() call to find out if the mouse has been clicked. If so, exit the while loop. If the mouse has not been clicked the function will return the defined value None.

    step4.py

  8. Let's have the circles chase some text around the image. Before the while loop from the prior step, create a Text object called goal in the middle of the image. Draw the object.

    Inside the while loop, where the offsets are created, we want to bias the random choice towards the goal. One way to implement that is to test if the new location would be closer to the goal than the old location. If the new location is better, make the move, otherwise make the move with some probability (e.g. 0.5). We can make this test before the test for legal moves inside the window.

    To enable the user to move the goal, when they click in the image move the text to that location instead of breaking out of the loop.

    Finally, to enable the user to quit, test if the user's mouse click is in the upper left corner and break out of the while loop if it is within a 10x10 box in the upper left corner of the image. Remember that that coordinate system for the Zelle graphics is (0, 0) in the upper left corner, with positive Y going down and positive X going right.

  9. step5.py

Once you've finished the above exercise, get started on the next project.