Lab 7 - Turtle Graphics & Functions

Directions for Labs


  • Submit your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
  • It is recommended that students attend in-person lab sections for lots of help!
    • If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
    • There is no need to attend the section you are enrolled in.
    • There is no need to attend any lab sections: there is no attendance taken.
    • You can complete the lab on your own time or own computer if you like.
  • While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.


Part 1) Turtle Graphics Intro


  1. Inside your cmpt120 folder, create a new folder for lab7 and make a lab7.py file.
    • See lab 1 for more detailed directions.
  2. Create a simple Turtle graphics program that draws a single line on the screen.
    • You can name your turtle anything you want; this code will refer to it as picasso.
    • Hint - Import: Remember to import turtle and create your turtle with: turtle.Turtle()
    • Hint - Keep windows open: You can keep the program open using turtle.Screen().exitonclick()
    • Hint - Full code:
      
      import turtle
      picasso = turtle.Turtle()
      
      # Part 1: Turtle Graphics picasso.forward(100)
      # Hold window open until click. turtle.Screen().exitonclick()
  3. Change the code to draw a square of size 100 where the turtle starts.
    • You must use a loop instead of coding the turtle movements for each side explicitly.
    • Hint - Turn and Move: You'll need to call picasso.right(90) and picasso.forward(100) for each side.
    • Hint - Loop: Use a for loop to draw each side. Go through the loop 4 times.

Part 2) Making Functions


  1. Create function named draw_rigid_square().
    • Move the code from part 1.3 (which drew the square using a loop) into this new function.
    • Call the function.
    • Hint - Placement: Put the definition of the draw_rigid_square() function near the top of your file, after you have imported the turtle library and created your turtle. Call the function from near the end of your program.
    • Hint - No parameters: Your draw_rigid_square() function won't need any information to do its job since all sides are hard-coded to 100; therefore, you don't need to pass any parameters (arguments).
    • Hint - Function: Your function will look like:
      
      def draw_rigid_square():
          for i in range(4):
              picasso.right(90)
              picasso.forward(100)
      ...
      draw_rigid_square()
      
      
  2. Make a function named turn_go_straight()
    • Have this function turn the turtle a given number of degrees to the right, and then go straight.
    • Pass in as parameters the number of degrees and the distance to move.
    • Change your draw_rigid_square() function to call this function for each side.
    • Hint - Parameters: You must pass in two parameters: 1) the degrees to turn, and 2) the distance. So your function might start out as: turn_go_straight(degrees, distance):
    • Hint - Code: Your code will look like:
      
      def turn_go_straight(angle, distance):
          picasso.right(angle)
          picasso.forward(distance)
      
      
  3. Create a function to draw a triangle above where the turtle starts.
    • Name your function something like draw_triangle_near_top().
    • Make each side of the triangle be length 100.
    • Draw the triangle starting at X,Y coordinates (-200, 200).
    • Hint - Move: You'll need to move the turtle to start with using the turtle's goto(x,y) function: picasso.goto(-200, 200)
    • Hint - Pen: When moving the turtle, you'll want to put the pen up with the turtle's penup() and later pendown() functions.
    • Hint - Sides: Keep using your turn_go_straight() function!
  4. Create a new function named draw_triangle(x, y)
    • Have it draw a triangle at the X,Y location it is passed in as a parameter.
    • Call it to draw a triangle at X,Y (300, 0).
  5. Create a draw_square(x, y, size) function.
    • Pass in the X,Y for where to draw the first corner of the square. It does not matter which is the first corner if you are drawing it a little different.
    • Pass in the size for how big all sides should be.
    • Call it to draw a square at X,Y (-200, -300) of size 100.

Part 3) Draw a Tunnel


  1. Comment out your above code.
  2. Draw ten squares, one inside the next.
    • Make the smallest box of size 20.
    • Make each box's sides be 20 longer than the previous.
    • Line-up the centre of all the boxes.
    • Hint - Loop: Setup a for loop to draw all 10 boxes.
    • Hint - Size: Compute the size of the square based on the index of the for loop.
    • Hint - Placement: Our draw_square(...) function specifies the starting location of a corner, but we want the centres to line up. Rather than starting all squares off at (0,0), you'll need to compute the starting location for each square.

[Optional] Challenge: Draw a Spiral


  1. Optional: Draw a square spiral.
    • You may want to speed up the drawing with: picasso.speed(0)
    • Hint - Size: Each line you draw should be a little longer than the last.

Submission


Submit your lab7.py file to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)

Topics Covered

  • Calling functions
  • Creating functions which have no arguments.
  • Creating functions which have a few arguments
  • Using turtle graphics