Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Python Palette

CSC160, Prof. Hank Feild

In this lab you'll have to understand the code in an already written program, and then extend it
by modifying existing functions and adding new functions. During the lab period, you must
perform all of these steps with your partner. If you do not finish during the lab period, you may
work by yourself or with a partner of your choosing on the remaining parts before the start of
the next lab period.

If you choose to not work with your lab partner after lab, make sure you and your partner each
make a copy of the lab to complete separately. Please do not allow your in-lab partner (or
anyone else) to use your solutions if they were not actively participating in the work to find those
solutions.

Team members
Please indicate who you worked with during the lab as well as after lab (if you didn't complete
the lab by the end of the lab period).
In lab Olijah Williams & Hannah Risko

After lab

Learning Objectives
By the end of this activity, you should feel comfortable:
● working with a partner to read and modify code (this is called pair programming)
● reading and making sense of an existing Python program
● modifying an existing Python program to change its behavior
● working with event-driven programs

Grading
The following rubric will be used to grade your work on this lab. See the syllabus for information
about redo tokens for increasing your grade.

Yes No

You worked diligently with 2.5 pts 0 pts


your partner during the lab
period, sharing the work and
communicating throughout.

You made a reasonable effort 2 pts 0 pts


to answer all of the questions
in Part 1.

You answered the questions 2 pts 0 pts


in Part 2 accurately.

The code produced for Part 3 3.5 pts 0 pts


works as expected, is well
styled, and uses appropriate
Python constructs covered in
class and the readings.

Part 1: Exploring the code


0. During this part, one of you will be the scribe, which involves having this document open
and typing in responses. The non-scribe should be in charge of viewing and running the code
(we'll refer to this role as the driver). List each of your roles below:

Scribe Olijah Williams

Driver Hannah Risko

1. Download palette.py from this lab's Google Drive folder. Read through the code with your
partner. Without actually running the code, what do you think the program would do if you were
to run it, press the 's' key on your keyboard, and then click in the middle of the Turtle canvas?

The code would start drawing squares on your click.

2. Now run the program. Once the white Turtle canvas window appears, press 's' on your
keyboard and click near the center of the canvas. Was your prediction correct? If not, what
actually happened? (Keep your program running as you answer the next several questions.)

Yes we were correct.

3. What function is called when the 's' key is pressed? What line of code is responsible for calling
that function when 's' is pressed?
Setlistners and it turns the square on(line 70).

4. Without actually doing it, examine the code and predict what will happen if you press the 'd'
key and then click in the upper left corner of the canvas?

Gets rid of squares and replaces the shape with dots.

5. Make sure the Turtle canvas window is focused (or run your program again if you accidentally
closed out of it). Press 'd' and then click in the upper left corner of the canvas. Was your
prediction correct? If not, describe what happened.

Our prediction was correct.

6. Looking just at the code, how would a user go about clearing all the objects from the canvas?

They would press ‘c’.

7. Try that out in the running program. Does it work? If not, take another look at the code and
come up with another hypothesis. Keep doing this until you make an accurate prediction and
describe that below.

Our original hypothesis was correct.

Part 2: Simple modifications


As you address these questions, take a look at the Turtle class documentation for help on
finding what methods are available and how to use them.

0. Switch the scribe and driver roles.

Scribe Hannah Risko

Driver Olijah

1. Right now, the squares that are drawn are all transparent. Modify them so they are colored in.
Copy and paste the function you modified below.
def drawSquare(x, y):
"""Draws a square at the given coordinates.

Parameters:
x: the x coordinate of the square
y: the y coordinate of the square
"""
print(f'Click at ({x},{y}) -- drawing square.')
width = 100
pat.penup()
pat.goto(x-width/2,y+width/2)
pat.fillcolor('black')
pat.begin_fill()
pat.pendown()
pat.setheading(0)
for sides in range(4):
pat.forward(width)
pat.right(90)
pat.end_fill()
pat.penup()

2. Now make it so that the color is randomly chosen each time the drawSquare function is
executed. Here's code that will randomly choose a value among the strings 'red', 'blue', and
'green' (feel free to modify this code so that it chooses among more or different colors):

random.choice(['red', 'blue', 'green'])

This call to random.choice will work because I've already imported the random module at the
top of the program. All this does is pick a color string; you'll need to incorporate that into your
code so that the fill color is set to the color string that was chosen. Once you do and you test it
and it works, copy and paste your modified function below.

def drawSquare(x, y):


"""Draws a square at the given coordinates.

Parameters:
x: the x coordinate of the square
y: the y coordinate of the square
"""
print(f'Click at ({x},{y}) -- drawing square.')
width = 100
pat.penup()
pat.goto(x-width/2,y+width/2)
pat.fillcolor(random.choice(['red', 'blue', 'green']))
pat.begin_fill()
pat.pendown()
pat.setheading(0)
for sides in range(4):
pat.forward(width)
pat.right(90)
pat.end_fill()
pat.penup()

3. Right now, the dot that is drawn in drawDot makes the dot size 4. Modify the code so that the
dot is 3 units larger than the pen size. You can find the current pen size by doing
pat.pensize() (note that in order to get the pen size, no arguments are passed to the pensize
method). Test your code by changing the pen size in the main function, e.g., pat.pensize(5))
and ensure that the dot size appears larger or smaller based on the argument you pass to
pensize. Once you know it works, remove the change to pen size in main and paste your
updated drawDot function below:

def drawDot(x, y):


"""Draws a dot at the given coordinates.

Parameters:
x: the x coordinate of the dot
y: the y coordinate of the dot
"""
print(f'Click at ({x},{y}) -- drawing dot.')
pat.penup()
pat.goto(x,y)
pat.dot(pat.pensize() + 3)

4. Modify the program so that pressing "z" instead of "c" clears all the objects from the screen.
Paste the code that you modified to make that happen below.
screen.onkeypress(clearcanvas, ‘Z’)

Part 3: Adding new functionality


For this part, you should alternate who drives periodically so you each have a similar amount
of time driving.

There's nothing to answer in this document for these parts. Instead, you will submit the Python
file with your modifications from this part. See the Submission section for instructions.

1. Selecting colors with the number keys. The goal here is to allow the user to use the
number keys (1, 2, 3, 4, ...) to select the color that the next shape will be drawn and filled with.
For example, if 1 is mapped to the pen color red and fill color blue, then if the user presses "1",
then presses "s", and then clicks at (100,120), a blue square outline in red will appear at the
coordinates (100,120). Note that the fill color will not have an effect on the dot.

To make this work, you'll need to add a function for each color combo you want to support, e.g.,
setColorsRedBlue, setColorsBlueGreen, .... This function should set the pen and fill
colors of the pat turtle.

In addition, you will need to add a keypress listener that maps a number key to the
corresponding color setting function (e.g., map "1" to setColorsRedBlue). The code to add the
keypress listeners should be added to the setListeners function.

You will also have to remove some of the code you added to the drawSquare function that sets
the fill color; your setColors... functions will take care of that now.

One of the color options should choose a random color (e.g., pressing "1" causes a random color
to be picked for each of the pen color and fill color). You should support a total of four or more
color options, including the "random colors" option.

Ensure that your program works before moving onto the next question. For example, make sure
the sequence of actions:

1 s click click d click 2 click click s click 3 s click d click

produces the following on the turtle canvas:

● two squares and a dot of whatever pen and fill colors correspond to 1
● two dots and one square of whatever pen and fill colors correspond to 2
● one square and one dot of whatever pen and fill colors correspond to 3
2. Adjusting the pen and dot sizes with the up and down arrow keys. The goal of this
modification is to allow the user to press the up arrow key to increase the size (one size unit per
press) and press the down arrow to decrease the size (again, one size unit per press).

To do this, you'll need to define two functions: one to handle incrementing the pen size and the
other to decrease it. In both functions, you should update the size of the pen to the current pen
size plus or minus 1. You saw how to get the current pen size as well as how to set the pen size in
Part 2 Question 3 above.

After you have both of those functions implemented, you need to attach them to the keypress
events for the up and down arrows in the setListeners function. Use "Up" as the key name for
the up arrow key and "Down" for down arrow key.

To make sure your implementation works, try the following sequence:

1 s click d click Up Up Up Up 2 s click d click Down Down 3 s click d click

You should see a thin square and small dot of whatever color 1 is; a thick square and dot of
whatever color 2 is, and medium-thickness square and dot of whatever color 3 is mapped to.

3. Add support for one additional shape. Your shape can be simple (e.g., a circle) or
complicated (e.g., a house). It's up to you. It should behave like the other two shapes (the square
and the dot) in that the user will press a key that you specify and then your shape will be drawn
centered on wherever they click. You will need to define a function to draw the shape and you
will need to add a keypress listener in setListeners for whatever keyboard key you select with
your shape's draw function.

Be sure to test your implementation.

Reflection
Please answer the following questions individually, though you may discuss your answers with
your group.

1. What confused you most about this activity?

Adjusting the arrow keys was confusing

2. What questions do you have either about the activity directly or about related topics that came
to mind while you did the activity?

1. How do we adjust the outline weight for the dots so the pen color is more prominent?
2. How do we make calling functions in code cleaner
3. Do you feel like you achieved all of the learning objectives listed at the top of each part that
you completed? If you feel uncertain about any of them, please list them here.

Submission
Make sure that your and your collaborator's names are at the top of your Python file.

Download a copy of this document as a .pdf or .docx. Upload that copy along with your code to
the Canvas assignment for this lab.

You might also like