CS101 - HW9 Prelab

Complete as much of this prelab as possible before lab on Thursday / Friday.

Create your file, add proper comments

From within the Thonny application, create a single Python file username_hw9.py with all your work for this assignment in it. Include a comment at the top with your name, the date and homework number, and your lab section.

Get re-acquainted with the Turtle

This week we'll use the turtle again! Recall you can import the turtle library with the command import turtle as t, and then use commands such as t.penup(). You can find more information in the turtle documentation. A quick reference is included at the bottom of this page.

Create a Point class

Create a Point class as we did in the class example on Thursday April 19. We'll use this to create Point objects that hold x,y coordinates of locations where we'll want to draw.

Draw Dots

Write a drawDots(points) function that takes a list points as input and draws a dot on the drawing window for each Point. For example, you should be able to call your function as follows:


>>> p = Point(10, 20)
>>> q = Point(20, 30)
>>> points = [p, q]
>>> drawDots(points)


What should be in the file

For the prelab portion, you should have completed code for a basic Point class as well as a drawDots(points) function. Your class, methods, and functions should each have a docstring, and the top of the file should include a comment listing (at a minimum) your name, the date and homework number, and your lab section. You will continue working on this same Python file and add more function definitions when you continue working on the rest of Homework 9.


Turtle reference

Python includes a library called turtle, which allows us to draw some simple graphics with a turtle that can draw wherever it walks. So, we issue commands to tell the turtle where to go, and a drawing appears as it moves. Here are the most important commands. More can be found in the online documentation.

forward(distance)
Move the turtle forward by the specified distance in the direction it is facing.
backward(distance)
Move the turtle backwards by the specified distance in the opposite direction to where it is facing.
right(angle)
Turn the turtle to its right by the specified angle. Note that this can be any angle, not the four cardinal directions we had with LightBot.
left(angle)
Turn the turtle to its left by the specified angle.
goto(x, y)
Drive the turtle immediately to the specified location without changing its orientation.
pendown()
Put the pen down so that the movement of the turtle leaves a track.
penup()
Pick the pen up so that there are no tracks when the turtle moves.
pencolor(colorstring)
Change the color of the pen that the turtle is using. This can be a string such as ‘red’, ‘yellow’, ‘blue’, etc. Of course, you have to pick a valid color name.
fillcolor(colorstring)
When this is set, the turtle will try to “fill in” any closed shapes that you draw with the given color.
begin_fill()
Call this before starting to draw a filled shape.
end_fill()
Call this function when you have finished drawing a shape that you want to be filled. This tells the turtle the shape is done and that it can try and fill it in.
tracer(state)
Turn on or off watching the turtle trace out the shapes. This is useful when you are trying to draw complex shapes and don’t want to wait. Pass in either True or False to turn the tracing on or off.
update()
If you turn off tracing, you should use this function to get everything the turtle has drawn so far to appear.
window_width()
Returns the current width of the turtle window.
window_height()
Returns the current height of the current turtle window.
dot()
Draws a circular dot. More information regarding size and color can be found in the online documentation.