< Back

Lab 1: Programming, Day 1

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab1
$ cd lab1


Practice Problem #1

Write a Python program that uses turtle graphics to draw a diamond.

Example

Practice Problem #2

Write a Python program that uses turtle graphics to draw a square that is centered in the window.

Example


Star

One of the first shapes you learned to draw in grade school was probably a star. It is a pretty easy shape to draw, looks pretty cool, and can be done entirely with straight lines. The only tricky part is determining how to go from one point of the star to the next. Today, you will figure that part out.

Details

In Emacs, create a Python program in a file called star.py. Your program should use the turtle module to draw a five-pointed star.

Example

$ python3 star.py
Turtle Graphics Star

Hint

  • Drawing the star is much easier if you use the functions turtle.forward and either turtle.left or turtle.right.
  • Note that the turtle will need to turn once for each point of the star and that it will turn a total of 720°. So, you can calculate the number of degrees in each turn.
  • Finally, drawing the star is simply alternating between moving forward and turning.

Challenge

Use the turtle.circle function to draw a pentagram like the following:

Turtle Graphics Pentagram

House

One of the great advantages to using a computer for drawing is that it is possible to draw precisely. It makes creating something perfectly symmetrical very easy.

Details

In Emacs, create a Python program in a file called house.py. Your program should use the turtle module to draw a simple house. The house should be a square with a triangle for a roof, and a rectangle for a door. The house should be symmetrical about a vertical line.

Example

$ python3 house.py
Turtle Graphics House

Hint

The trickiest part of the house is getting the roof to fit perfectly. To get it to fit perfectly, you need to calculate the angle of the roof and the length of the roof edges. If you use a 45-45-90 triangle, then the roof angle 45 is degrees and the roof length is the house width divided by the square root of 2.

Challenge

Spruce up the house. Add things like a chimney, the sun, a tree, a mailbox, a fence, whatever you want, as long as it impresses me.