Python Modules

So you've probably been wondering where the sprites and graphics are in python. It turns out that there are many different ways to display graphics in Python by using modules. A module is code that someone else has written that we can then import into our file and use.

A simple and popular graphics module for Python is called turtle. To import the turtle module, add the following line of code to the top of your file:


import turtle
        

This imports the turtle module and allows us to use all of its functionality. If we want to call a function from the turtle module we would do something like below:


turtle.forward(100) # moves the sprite 100 steps forward
        

Sometimes we might want to rename the modules that we import so that they are more easy to type or have a more intuitive name. Let's say that we felt like turtle was too long of a word to type before each function call. We could have instead written the following import line: import turtle as t. This would then allow us to access the turtle module via the alias name t. This would allow us to call a function like this:


import turtle as t
t.forward(100)
        

Turtle Functions

The turtle module has all of the same drawing functions as Snap! and much more. A few of the module's functions are shown below, but if you want to learn more about the incredible things that you can do with turtle please refer to the documentation. The first time that you call any one of the turtle functions a new window will open for the turtle to draw in. You can think of this window as being analogous to the stage in Snap! its just that we only see the "stage" in python when we actually draw something.


# moves the sprite 100 steps forward
turtle.forward(100)

# moves the sprite 100 steps backward
turtle.backward(100)

# rotate to the right 45 degrees
turtle.right(45)

# rotate to the left 45 degrees
turtle.left(45)

# Pen doesn't draw when moving
turtle.penup()

# Pen draws when moving
turtle.pendown()

# Go to a coordinate (x: 130, y: -50)
# The turtle starts at the center of the stage which is at position (x: 0, y: 0)
turtle.goto(130, -50)

# Write the string "Hello World!" to the stage at the sprite's current position.
# Don't worry too much about the font=(...) part, just make sure it's there.
# Refer to the official documentation for more information (linked above).
turtle.write("Hello World!", font=("Arial", 30, "normal"))

# clear all drawings on the stage
turtle.clear()
        

As you can see almost all of these functions are identical to what is in Snap!. Many of these functions have abbreviations as well, such as turtle.fd(100) which moves the sprite forward. Check out the documentation for other tips, tricks, and help.

Exercise 4

Write a function that when called draws the fractal pattern shown below (this is the c-curve from the past recursion lab). Also use the turtle's write function to write your name (or something funny or both!) on the stage somewhere. You should use recursion and the turtle module to complete this exercise (this exercise is not graded by autograder).

There should be an image here There should be an image here There should be an image here There should be an image here There should be an image here There should be an image here

Congrats, You've Finished Besides Blocks Lab 1!

To confirm that you've completed all of the exercises enter the folowing command and show the output to your teacher (you'll have to show your turtle result as well, as the autograder only tests exercises 1-3):


python3 autograder.py lab1.py