CS 150 - Assignment 2
Due: Wednesday 3/1 at the beginning of class
For this assignment, we will be using the turtle graphics
module to draw a picture. For your prelab, you should have
designed the drawing and figured out the rough placement of the
different components. See the prelab for the details of what
should be included in your drawing. For this assignment you will be
working on a single python file/program that generates your picture.
Here is an example completed picture:
Before you start coding, a few brief comments on style. We've talked
about style components in class and now we're going to start utilizing
these in your assignments. In particular, make sure you keep the
following in mind as you're writing your program:
- All functions should have appropriate docstrings
- Comment your code appropriately. Comment complicated parts of
the code and include your name, date, etc. at the top of the file.
- Follow the variable naming conventions discussed in class and use
good variable names
- Use whitespace appropriately to make your program easier to read
- Use constants. If there are values that don't change within your
program but represent constant values, you should define capitalized
constants for these at the top of your program.
You should use your prelab as a guide to help you as you start
to put your picture together. However, feel free to deviate from your
original design. The prelab was just a brainstorming session to get
you started and to help with the basic layout.
As you start to code up your picture, you may also notice that some of
your x and y coordinates as well as sizes are not exactly right either
because of your measurements or because of a differences in screen
size. There are functions in the turtle module that allow
you to query the size of the window. Feel free to use these or to
just use reasonable values.
3. Basic Shapes
To get started, we're going to write some functions to make some basic
shapes for us. Make sure that you have these working before moving on
to the next part.
I encourage you to refer to the documentation online as you work on this:
Don't forget to include the import statement for the turtle
module at the top of your file:
from turtle import *
- triangle - Write a method called triangle that
draws an equilateral triangle (i.e. a triangle with all three sides
the same length). Your function should take three parameters: the x
and y coordinate where it should be drawn and the length of the side
of the triangle. The triangle should be drawn so that the left edge
of the triangle is vertical. For those rusty on geometry, the
interior angles of an equilateral triangle are all 60 degrees, which
means the angle between a straight line drawn from one side and the
adjacent side is 120 degrees.
Your x and y coordinates may indicate any part of the triangle
(e.g. the top left, the center, etc.). Pick whichever seems easiest.
For example, here are three triangles of increasing size:
- polygon - Write a method called polygon that
draws a polygon. An n-sided polygon has n equal length edges and the
angle between a straight line drawn from one side and the adjacent
side is 360/n. Your function should take 4 parameters: the x and y
location of the polygon, the number of sides and the length of the
each side. Again, the x and y coordinates may indicate any point on
the polygon, so pick whatever seems easiest.
Unlike the triangle where you know when you're writing the function
exactly how many sides the object will have, for the polygon you can't
just hard-code the different line segments. Instead, you'll have to
use a for loop.
For example, below are some polygons of differing number of sides and
size. All can be drawn with the polygon function.
4. The Background
At this point, you should have two functions written that draw
triangles and polygons anywhere on the screen. We'll now work on
filling in the background.
The key component of the background is randomly spaced circles. Write
a function called add_circles that takes as a parameter the
number of circles to add and randomly places circles of radius 4
throughout the screen. Some hints for how to do this:
To check that everything is working right, try adding differing
numbers of circles and make sure that they're distributed throughout
the screen and that the right number are actually being drawn.
Once you're sure it's working, add a bit more code to make the size of
the circles random. You'll have to play with different size ranges to
see what looks best. For example, if you look at the picture on the
first page of this handout, you'll see that the bubbles range in size.
Think about using constants here rather than hard-coding numbers in
- The circle function draws a circle with a given radius
- You'll need to figure out the dimensions of the screen (think prelab)
- The randint function from the random module may be useful
- To make this available, don't forget to include from random
import randint at the top of your program
5. Putting it all together
You now have all the components required to put together your final
picture. Create a function called generate_picture that
draws the entire picture. One way to do this is:
- Change the background to the appropriate color.
- Change your triangle and polygon functions so
that they create filled shapes (see the turtle documentation
on how to do this if you don't remember).
- Change your add_circles method to also draw filled
- Actually create your picture using your three functions. If your
design from the prelab is good, this should be fairly painless.
- Change the fill color of the objects to make it look more
realistic (e.g. orange fish and brown rocks).
6. Extra Points
You can earn up to 3 extra points on this assignment by including
additional elements to your picture. The amount of points given will
be assigned based on creativeness and difficulty of implementation.
Here are some examples, but I encourage you to include your own:
To receive extra points you MUST include in your comments at the top
of the program what your extra point additions were (otherwise, it can
be hard to figure out sometimes).
- Instead of circles, write a function called star that
draws stars (not asterisks) (use a for loop)
- Include other types of objects in your scene
- Modify the triangle function to draw more interesting
triangles, for example isosceles triangles or triangles with fins
Note: If your picture gets very fancy, it is possible that you
no longer need the triangle and polygon functions. Even if you don't use them,
leave them in your code since they will be graded!
7. When you're done
When you're all done you should have a working program that draws your
picture. Make sure that your program is properly commented:
In addition, make sure that you've used good style (named
constants, good names for functions, parameters, and variables, white
- You should have comments at the very beginning of the file
stating your name, course (including section number), assignment
number and the date.
- Each function should have an appropriate docstring
- Other miscellaneous comments to make things clear
Please submit both your program and a screenshot of your picture via
the digital submission link on the course home page. Your program
should have a ".py" extension; your image should be saved in .png
To take a screenshot of the drawing window:
Make sure you submit both your code and your image by the deadline.
- On a mac:
Hold command+shift+4 and then while holding these, hit the spacebar
(on a mac). If you then click on the window where your picture is
drawn, an image file will be saved on your desktop entitled "Screen
shot...". You can double-click on this file to make sure it worked.
- On a PC, you can use the "Snipping Tool". Save your image in
PNG format on the desktop.
correct x, y 1
correct direction 1
correct shape 3
varying sizes 1
x, y 1
correct number of circles 1
covers entire area 1
random locations 2
random sizes 1
background color 1
proper fills 1
6+ fish/ships 2
6+ rocks/planets 2
Comments, style 5
Extra points 3
Total 27 + 3