CS21 Lab 5: Objects and Graphics

Due Saturday (October 8) before midnight

This lab assignment requires you to write two Python programs using objects from the Zelle graphics library.

Remember, we will only grade files submitted by handin21 in your cs21/labs/05 directory.


Programming tips

As your programs become larger, it is even more important to develop good habits:

The above are mandatory and you will be graded on them.

We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code.

We have provided some images of what our solutions look like when we run them. It's okay if your programs look a little different, as long as they still satisfy the requirements described below. Be sure to test your program!


1. Winter Scene

Write a program called snowman.py that generates a winter scene, using 3 mouse-clicks from the user. Here's an example:

Here are the requirements for the program:

And here are some useful hints and tips:

snowman picture


2. Random Art

Write a program, randomart.py that generates a random work of art in a Zelle graphics window. Here is the masterpiece generated by a sample run of our program:



Minimum Requirements

Your program should choose at least two shapes (Circle, Rectangle, etc.) and draw each shape at randomly chosen points in the graphics window. Your program should randomly choose the size of each shape from a range of options and randomly determine the color of each shape, either by choosing randomly from a list of color options or by randomly generating colors.

Your program must utilize a function for creating each specific shape. For example, in the program that generated the image above, we would expect functions for drawing circles (drawRandomCircle) and stars (drawRandomStar). One way to design these functions would have them taking four arguments:

  1. The graphics window in which to draw the shape
  2. A list of colors
  3. A minimum size
  4. A maximum size
Then inside the function you would:
So when we call these functions it might look like: drawRandomCircle(window, colors, minSize, maxSize) or drawRandomStar(window, colors, minSize, maxSize). If you follow these guidelines for your functions, it will help you avoid hardcoding. But if you want, you can design your functions to take different parameters. For instance, if you want to generate random colors your function may not need the colors argument.

Your functions (and program in general) should be flexible and work for any window size and/or shape size. That is, you should not hardcode the width, height, and size values.

After you write these auxiliary functions, you should test them in your main() function. You might test them by calling each function inside a for loop that repeats 10 times. Once you are convinced that these functions work properly, move on to the next step: adding user interaction, as described in the next paragraph.

Use the sleep method from the time library to animate the gradual addition of more and more random shapes to the graphics window. Your program should continue adding shapes until the user presses the s key to indicate that they like the picture and want the program to stop generating shapes. From that point forward the resulting art should be displayed indefinitely, until the user clicks anywhere in the window. Decide whether window.checkKey() or window.getKey() is the appropriate method to use.


Additional Tips

In addition to the shapes defined in graphics.py we have provided an additional shape, the star, which you can use as one of your shapes. The constructor for the Star object takes as arguments the center of the star (a Point), the radius of the star (a number), and the number of points you want the star to have (an int). If you leave off the third argument, you'll get a five-pointed star by default. The radius of a star is the distance from its center to one of its points. All of the methods from Zelle's Polygon class are also available to Star objects. This link has more information about the Star class, including an example program.

Note: If you want to see all the available colors, you can use the colorPicker application in the Python shell as follows:

      $ python
      >>> from colorPicker import *
      >>> colorPicker()
      # then click on a color and its name will appear in the python interpreter
    

You can also create your own colors with the color_rgb(red, green, blue) function.


Extra Challenge - Create your own shape

This does not affect your grade so please only attempt this after completing the rest of your lab

Rather than limit yourself to stars and the shapes provided by the Zelle graphics library, create your own shape and display many randomly generated instances of it to the window. Some ideas: hearts, moons, clovers, rainbows, shooting stars, or any of the other marshmallow shapes that you find in Lucky Charms cereal :)

Here's an example of this extra challenge with stars in different shades of yellow and three-dimensional boxes in 50 shades of grey. Notice that even the number of points for the stars is random, too!



Don't forget to create a new function (like drawRandomBox) for the shape you create.


Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.