Lesson 2: The Art of Drawing with Python Turtle
Prerequisites: Before You Dive In!
Functions and Parameters
Before we dive deeper into using more Turtle functions, let's talk about what a function is.
A function is like a little machine in your program. You give it some information, it does something with that information, and then it might give something back. For example, in a game, you might have a function that calculates the score each time a player scores points.
Parameters are the bits of information you give to the function. Think of them as ingredients in a recipe. Just like you might need eggs, flour, and sugar to make a cake, a function needs parameters to do its job.
Here's a simple example:
Imagine you have a function in Turtle that draws a square. The parameter might be the size of the sides of the square. You tell the function how long each side should be, and it uses that information to draw the square for you.
Functions help you reuse code without having to write it over and over again. You can write the function once, and then use it many times with different parameters to do similar tasks. This makes your code cleaner, easier to read, and easier to manage.
Coach Note!
Here's what you'll be learning in this section, based on your current level:
Standard
- Explore the Turtle documentation, understanding how technical guides are laid out and how to use them to your advantage!
- Discover how functions act like little helpers in your code, taking in information (parameters) and performing tasks. You'll learn to use Turtle functions to draw basic shapes and control the screen.
- Start by creating simple geometric shapes. You'll draw your first square and see how changing commands can turn it into a rectangle. This is your first step in turning code into art!
- Learn how to change the background color of the Turtle screen to make your drawings stand out. Itβs a simple way to add flair to your projects!
- The basic commands for lifting the pen (
penup()
) and placing it back down (pendown()
).
Advanced
- Dive deeper by designing your own functions to draw more complex shapes. Youβll learn how to use loops and conditions to automate repetitive tasks and create intricate designs.
- Expand your drawing skills beyond basic shapes. Youβll tackle circles, polygons, and even fun challenges like drawing a smiley face using advanced Turtle commands.
- Take control of the Turtle canvas by learning to dynamically change screen features in response to user actions. Discover how to make your programs interactive and responsive.
- Pure functions versus side effects with respect to Turtle environment.
Objective π§πΏβ
Get ready to turn code into art as we explore how to use functions and parameters to draw shapes and spice up our screen with vibrant colors!
Drawing Shapes π ποΈβ
Now that we know the movements of the turtle, we can move on to making actual shapes!
Let's start by drawing polygons since they all consist of straight lines connected at certain angles.
Drawing a Squareβ
To draw a square, we need to move forward and turn right four times. Here's how you can do it:
t.fd(100)
t.rt(90)
t.fd(100)
t.rt(90)
t.fd(100)
t.rt(90)
t.fd(100)
CHALLENGE: π§βπ¨ποΈ Draw a Rectangle π§βπ¨ποΈ
Try to draw a rectangle. Remember, in a rectangle, all four sides are not equal. You'll need to adjust the code accordingly. Can you figure it out? Show your coach once you're done (or check out below for some help)!
HINT π€«
To draw a rectangle, you can change the length of the sides. Here's an example:
t.fd(200) # Move forward by 200 units (longer side)
t.rt(90) # Turn right by 90 degrees
t.fd(100) # Move forward by 100 units (shorter side)
t.rt(90) # Turn right by 90 degrees
t.fd(200) # Move forward by 200 units (longer side)
t.rt(90) # Turn right by 90 degrees
t.fd(100) # Move forward by 100 units (shorter side)
Drawing Preset Figuresβ
Suppose we want to draw a circle. If we attempt to draw it in the same way as we drew the square, then it would be extremely tedious, and youβd have to spend a lot of time just for that one shape!
Thankfully, the Python turtle library provides a solution for this. You can use a single command to draw a circle:
t.circle(60)
The number within the parentheses is the radius of the circle. You can increase or decrease the size of the circle by changing the value of its radius.
In the same way, you can also draw a dot, which is nothing but a filled-in circle. Type in this command:
t.dot(20)
CHALLENGE: π Draw a Smiley Face π
Time to put on your thinking cap! Your challenge is to draw a smiley face using the knowledge we've learned so far.
You may need some help since we haven't gone over all the turtle functions necessary to do this. If you get stuck, take a peek at the code below and continue trying it out yourself!
HINT π€«
t.speed(0) # Set the drawing speed
# Draw the face
t.penup()
t.goto(0, -100) # Move to the starting position for the face
t.pendown()
t.circle(100) # Draw a circle with radius 100 for the face
# Draw the eyes using dot
t.penup()
t.goto(-35, 40) # Position for the left eye
t.dot(20, 'black') # Draw the left eye
t.goto(35, 40) # Position for the right eye
t.dot(20, 'black') # Draw the right eye
# Draw the smile
t.goto(-60, -20)
t.pendown()
t.setheading(-60) # Angle the turtle to start the smile
t.circle(70, 120) # Draw the smile
Screen Color π₯οΈ π¨β
By default, Turtle always opens up a screen with a white background. This is kind of boring, right? Let's change things up with the following command:
s.bgcolor("blue")
From the turtle documentation, we can see that the bgcolor()
function will take in a color string or a colorβs hex code number.
Screen Color Practice
Try changing the color of the background to your favorite color. Feel free to use a color picker site to find your unique shade!
Show your coach once youβre done!
Need some help? π
To change the background color to your favorite color, use the screen.bgcolor()
function and specify the color name or hex code as a string.
For example, to change the background color to red we have three different options:
# We can pass in red as the word
s.bgcolor("red")
# We can pass in red as its hex code
s.bgcolor("#FF0000")
# We can pass in red as its RGB
s.bgcolor((255, 0, 0))
Pen Width ποΈ πβ
Sometimes we may need to increase or decrease the thickness of the pen. Turtle gives us the ability to do so. Check it out below!
We'll want to make sure we pass in a positive number into the pensize()
function.
We can do so with these simple commands:
t.forward(100)
t.pensize(5)
t.forward(100)
Go ahead and adjust our pen using width()
and show your coach once complete!
Need some help? π
Here's how we can change the pen's width to 3.
t.forward(100)
t.width(3)
t.forward(100)
Now try it on your own, with your lucky number as the new width!
Picking the Pen Up and Down β¬οΈ β¬οΈβ
Sometimes, you may want to move your turtle to another point on the screen without drawing anything on the screen itself.
To do this, you use .penup()
. Then, when you want to start drawing again, you use .pendown()
.
t.fd(100)
t.rt(90)
t.penup()
t.fd(100)
t.rt(90)
t.pendown()
t.fd(100)
t.rt(90)
t.penup()
t.fd(100)
t.pendown()
- Well done! You've transformed lines of code into beautiful drawings and learned how to customize your canvas and turtle to bring your creative ideas to life!