CS21 Lab5: Objects and Graphics

Due Saturday (February 28) before midnight

This lab assignment requires you to write three programs in Python. First, run update21. This will create the cs21/labs/05 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/05 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/05
$ pwd
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Programming tips

As you write your first programs, start using good programming practices now:

Zelle Graphics Reference

1. Modern art
Mondriaan Tableau I Write a program modern.py that creates a modern art image consisting of three colored rectangles and twelve lines in a style similar to Mondrian

Your program should first prompt the user to click on the corners of each rectangle. After clicking on two points, the program should draw a rectangle in the color of your choice. Repeat getting mouse clicks from the user and draw two more rectangles in different colors.

After drawing the three rectangles, draw lines across the top, bottom, left and right of all three rectangles. The horizontal and vertical lines should extend the entire way across the screen.

Prompt the user to click once more to close the window. A sample run is shown below

Modern Art example

Note it is OK to have rectangles overlap, but the lines must be drawn after the rectangles. In some cases a line on the edge of one rectangle will pass through the middle of another rectangle as shown in the example below.

Modern Art example

Hint: Save the mouse clicks from the rectangle drawing in a python lists using the list append() method. Loop over this list after drawing all the rectangles to draw the lines.

2. Bounce

Write a program bounce.py that animates a bouncing ball. Start by drawing a circle in the center of the graphics window. Prompt the user to click on the screen. The distance from the click to the center of the circle will represent the velocity of the circle. Thus if the distance from the center to the click is dx and dy in the horizontal and vertical directions, respectively, you should move the circle by an amount dx,dy on each time step.

When the ball hits the vertical or horizontal boundaries of the window, reverse the corresponding velocity so the ball bounces away from the window edge. Have the ball continue bouncing along the walls until the user clicks anywhere on the window with the mouse. At that point you should close the window and end the program.

An example is shown in the video below:

If you click closer to the center of the circle, the circle moves more slowly.

3. Freestyle Design
Use your imagination and the graphics library to create your own picture in freestyle.py. Draw a snowman, a ninja (CS or other), a sunny beach, or something totally different. Your program must:

Be creative (not all features should be circles!). For symmetrical features (such as eyes or ears), you may use the clone method to make a copy of the original shape. Then draw the cloned shape in the window and move it to the desired location.
snowman dragon cat

Optional challenge
This is an optional bonus exercise. It is challenging and is not required, and you should not attempt it until you are completely finished with the other lab exercises. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Write a program, textBounce.py similar to the second program but have the ball leave a trail of text. The text can be a phrase that you choose or that the user types before starting the animation. The phrase should loop repeatedly as the ball continues to bounce


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.