CS21 Lab 6: Graphics and Objects

Due Sunday, March 16th 11:59pm

This lab assignment requires you to write three programs in Python. First, run update21. This will create the cs21/labs/06 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/06 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. Note: Because of Spring Break, this lab is due on Sunday March 16th instead of its regular due date. However, since working on graphics programs remotely doesn't work very well (it is very slow), I encourage you to finish this program before you leave for Spring Break. Opportunities for help will be limited over Spring Break, and I will have limited access to email over break.

$ update21
$ cd cs21/labs/06
$ pwd

Here is some general documentation on the graphics library. If you're curious, here is the original source of the library.

We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

1. Words, words, everywhere

In this exercise you will write a program, words.py, that accepts a phrase from the user and then draws that text in a graphics window in a silly way. Your program should draw the first letter of the phrase, then the first two, then the first three, up to the entire phrase. Each time you print one of these phrase prefixes, you should set its anchor point and style according to the following rules.

The first letter should be printed at the anchor point (10,10). Each time you add a letter, you should move the anchor point 30 units to the right and 20 units down. If the anchor point ever goes outside the bounds of the window, you should move it to the opposite side of the window by subtracting the appropriate window dimension. Note: you do not need to do anything fancy like wrap the text around from one side of the window to the other. This is simply an adjustment of the anchor point.

The first letter should be printed in red, the first two in green, the first three in blue, and the rest should follow the same color pattern. The first letter should be printed in normal font, the first two in bold, the first three in italic, the first four in bold italic, and the rest should follow the same cyclic pattern. The sizes should follow a similar cyclic pattern from 5 to 30 in increments of 5. The following table illustrates these stylings.

("bold italic" is actually a valid style name in the graphics library we are using.)

Prefix length Color Style Size
1 red normal 10
2 green bold 15
3 blue italic 20
4 red bold italic 25
5 green normal 30
6 blue bold 10
7 red italic 15
8 green bold italic 20
9 blue normal 25

Here are some examples of how your program should behave (for these examples the window size was 400 by 600):

$ python words.py
Type some words, then hit enter. The quick brown fox
$ python words.py
Type some words, then hit enter. Lorem ipsum dolor sit amet, consectetur

Hint 1: The slice operator (list_expression[start:end]) is your friend.

Hint 2: With some clever uses of lists and the modulus operator, you can complete this exercise without a single if statement. The modulus (a.k.a. remainder) operator is written a % b and evaluates to the remainder of dividing a by b

2. Eagle eye

This exercise is inspired by one of the games developed by Lumosity. Your program, eagle_eye.py, is a game of reflexes and visual memory. It will briefly flash a small circle at some random spot on the screen. After the circle disappears, the user has to click where the circle was. If they miss, the game is over. If they get it right, they get to try again with a shorter flash.

It's probably easier understand how the game works by looking at someone playing it, rather than reading a long description. Here is a video of an eagle-eyed instructor playing the game:

Here are some specific constraints your game should satisfy:

3. Freestyle

Show us how creative you are! For this exercise you should use the graphics library to draw a pretty picture in a program called picture.py. Your picture should use at least two different kinds of graphics objects and at least 5 distinct objects. Furthermore, your picture should be animated in some way.

If you're having trouble deciding what to draw, a popular choice in CS21 classes passed has been drawing a flag (real or imaginary) that goes up a flagpole.

Hacker's challenge. More!

There is no normal challenge this week. If you want to have some more fun, make your animation more interesting. Maybe add some interactivity via the mouse or keyboard.


Once you are satisfied with your programs, hand them in by typing handin21 at the Linux prompt.

You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline

Remember: for this lab, programs must appear in your cs21/labs/06 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/06 directory. For example:

 mv  myprog.py  ~/cs21/labs/06/myprog.py