# CS21 Lab 5: Objects and Graphics

Due Saturday (Feb 25) before midnight

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

Goals

The goals for this lab assignment are:

• learn a little about computer graphics
• get comfortable using objects and methods
Programming tips/requirements
• Same as always: comment at the top, good variable names, write and test incrementally
• Don't hard-code values! Define variables, like width and height, then specify positions, sizes, lengths, etc. as a fraction of the width and height
• We have provided some images below. It's okay if your images look a little different, as long as they still satisfy the program requirements described below
• If you are working remotely, see our help page on remote access
• When using raw_input() to ask the user for input, doing that before setting up the graphics window is often easier (less clicking back and forth between windows)
• don't forget to run update21 and put your programs in your cs21/labs/05 directory
1. Pattern

For this problem we want you to write a function (drawShape) and then use that function to generate a pattern like this:

First, in a file called pattern.py, write a function called drawShape(win,pt,size) that draws some shape (something more than just a circle or a rectangle) in the given window (win), at the given point (pt), of the given size (size). You may create your own custom shape, if you want, or have your function make one of these:

Test your function by calling it from main(). For example, here's a main() that calls drawShape() twice, using different points and sizes:

def main():
width = 600
height = width
gw = GraphWin("pattern", width, height)
pt = Point(200,200)
drawShape(gw,pt,30)
pt.move(200,20)
drawShape(gw,pt,100)


Once you are certain your function works, ask the user for the number of rows, and use your drawShape() function to draw the pattern (1 shape on row 1, 2 on row 2, etc). Asking for the number of rows and calling drawShape() to draw the pattern should all be done from main().

Here's another image from the running program. For this run, the user asked for 18 rows. Notice that the window is the same size, but the shapes are all smaller than when there were only 5 rows.

Hints: If you are stuck on how to display the pattern of shapes, concentrate on drawing just one row. For example, how would you use a for loop in main() to call drawShape() over and over, to draw 5 shapes horizontally, like this?

Once you have that working, how can you repeat that for multiple rows?

2. Travelers

I've been watching the Travelers show on Netflix, and they have a cool text effect during the show's opening credits. It looks something like this:

Write a program, travelers.py, that asks the user for a word, and then displays the "travelers" effect for that word. In the above video, the user entered the word "hello". For the video below, the user entered "travelers":

Hints:

• use the setText() method to change the Text object (you don't have to undraw, recreate, and then redraw the Text object each time)
• you can also use the choice() function from the random library to easily select random characters from a string (e.g., try choice("abcdefg"))

#### Extra Challenge (just for fun, no bonus points)

In the above videos, the final word is displayed all at once. For fun (and more like the show's credits), have the final letters appear one-by-one, over time, ending in all letters being displayed.

3. Summer Scene

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

Here are the requirements and some tips for this program:

• have the user click the mouse for each action (sun, horizon, etc)
• all clicks should have a corresponding text object to tell the user what to click for
• use the getY() point method to get the y-coordinate of the horizon and beach clicks (Note: the diagonal beach line does not have to go through each click point -- just use the y-coordinate of each point to make a polygon that runs from the left to the right side of the graphics window)
• you can pick your own colors and sizes
• there should be some kind of animation at the end (sun setting, shark swimming by, etc)
• you do not need to use functions for this one (besides main()), but you can if you want

#### Extra Challenge (just for fun, no bonus points)

Add more stuff to your beach scene or animation! Let the user decide the size of the sun, or make the colors change as the sun sets.

4. Answer the Questionnaire; Run handin21

Once you are confident that all programs work, fill out the questionnaire in README-05.txt.

Then run handin21 a final time to make sure we have access to the most recent versions of the files required for this lab.