Skip to content

Commit ab8d8fb

Browse files
committed
Added chapter 5 - no changes
1 parent 04c0850 commit ab8d8fb

2 files changed

Lines changed: 354 additions & 23 deletions

File tree

Lines changed: 336 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,347 @@
1+
---
2+
title: Drawing with Turtle Graphics
3+
description: Create your first turtle drawings using the Turtle and Screen objects, movement commands, pen control, colors, shapes, and speed settings.
4+
generated_by: claude skill chapter-content-generator
5+
date: 2026-06-28 09:48:25
6+
version: 0.09
7+
---
8+
19
# Drawing with Turtle Graphics
210

3-
## Summary
11+
By the end of this lesson you'll be able to:
412

5-
Now the real fun begins! You create a `turtle.Turtle()` object — think of it as
6-
a robot pen — and give it commands to draw on the screen. This chapter covers
7-
everything you need for basic drawing: creating the screen, moving forward and
8-
backward, turning left and right, lifting and lowering the pen, changing colors
9-
and pen size, and controlling how the turtle looks and moves.
13+
- Create a `turtle.Screen()` and a `turtle.Turtle()` object to set up a drawing window
14+
- Move the turtle forward, backward, left, and right to draw lines
15+
- Lift and lower the pen to skip spaces in your drawing
16+
- Change the turtle's color, thickness, shape, and drawing speed
1017

11-
## Concepts Covered
18+
Imagine a tiny robot with a marker strapped to its belly.
19+
You tell the robot where to walk, and the marker draws a trail wherever it goes.
20+
That is exactly what **turtle graphics** does — and in this chapter you get to write the commands that bring that robot to life.
1221

13-
This chapter covers the following 10 concepts from the learning graph:
22+
!!! mascot-welcome "Welcome to Chapter 5!"
23+
![Monty waving welcome](../../img/mascot/welcome.png){ class="mascot-admonition-img" }
24+
This is the chapter where Python becomes truly visual! Every command you type makes your turtle move and draw — instantly, right here in the browser. There is no better way to build your Python instincts than to type a command, click Run, and watch the turtle obey. Let's code it together!
1425

15-
1. turtle.Turtle() Object
16-
2. turtle.Screen() Object
17-
3. Screen Setup and bgcolor()
18-
4. Movement forward() backward()
19-
5. Turning left() right()
20-
6. Pen Control penup() pendown()
21-
7. Pen Size and Color
22-
8. Turtle Appearance shape()
23-
9. Hiding and Showing Turtle
24-
10. Speed Control speed()
26+
## Setting Up the Drawing Stage
2527

26-
## Prerequisites
28+
Before any drawing can happen, Python needs two things in place: a **screen** to draw on, and a **turtle** to do the drawing.
2729

28-
This chapter builds on concepts from:
30+
### The turtle.Screen() Object
2931

30-
- [Chapter 4: Functions and Objects](../04-functions-and-objects/index.md)
32+
A **`turtle.Screen()`** creates the drawing window — the blank canvas where all your shapes will appear.
33+
Think of it as the stage in a play: you have to set up the stage before the actors can perform.
3134

32-
---
35+
You store the screen in a variable so you can send it instructions later:
36+
37+
```python
38+
import turtle
39+
wn = turtle.Screen()
40+
```
41+
42+
The name `wn` stands for "window" — that is the most common nickname programmers use for this variable, but you can call it anything you like.
43+
44+
Once you have the screen, you can change its background color using **`bgcolor()`** (short for "background color"):
45+
46+
```python
47+
wn.bgcolor("lightblue")
48+
```
49+
50+
Put any color name in quotes — `"lightyellow"`, `"pink"`, `"white"`, `"black"`, or any of the 140 named web colors Python understands.
51+
`bgcolor()` changes the window background the moment Python runs that line.
52+
53+
### The turtle.Turtle() Object
54+
55+
A **`turtle.Turtle()`** creates your drawing agent — the small arrow (or turtle shape!) that moves around the canvas, leaving a colored line wherever it goes.
56+
57+
In Chapter 4 you learned that **objects** have their own **methods** that you call with a dot.
58+
A turtle is a perfect example of this idea in action.
59+
You create one object and then call methods on it to make it move, turn, and draw.
60+
61+
```python
62+
t = turtle.Turtle()
63+
```
64+
65+
The variable `t` now holds your turtle.
66+
Whenever you want the turtle to do something, write `t.` followed by a command name and parentheses.
67+
68+
!!! mascot-tip "Scratch Bridge"
69+
![Monty with a tip](../../img/mascot/tip.png){ class="mascot-admonition-img" }
70+
In Scratch, the **Stage** was the white backdrop where your sprites lived, and a **Sprite** was the character you programmed to move. In Python turtle, `turtle.Screen()` is your Stage and `turtle.Turtle()` is your Sprite. The big difference? Instead of dragging a sprite with your mouse, you write Python commands to control it — and that gives you far more power than drag-and-drop ever could!
71+
72+
## Moving the Turtle
73+
74+
The turtle always starts at the center of the screen, facing right (East).
75+
You have four movement commands to work with:
76+
77+
| Command | What it does |
78+
|---------|-------------|
79+
| `t.forward(n)` | Move `n` pixels forward in the direction the turtle faces |
80+
| `t.backward(n)` | Move `n` pixels backward (tail-first) |
81+
| `t.right(n)` | Rotate the turtle `n` degrees clockwise |
82+
| `t.left(n)` | Rotate the turtle `n` degrees counter-clockwise |
83+
84+
The `n` in `forward()` and `backward()` is measured in **pixels** — tiny dots on your screen.
85+
One hundred pixels is roughly the length of your thumb held next to a typical monitor.
86+
87+
The `n` in `right()` and `left()` is measured in **degrees**, just like in your geometry class.
88+
A full circle is 360°.
89+
A right-angle corner — like the corner of a piece of paper — is exactly 90°.
90+
91+
These four commands map directly to blocks you already know from Scratch.
92+
The **Move N steps** block is `forward(n)` in Python.
93+
The **Turn ↻ N degrees** block is `right(n)`, and the **Turn ↺ N degrees** block is `left(n)` — same ideas, just written as text.
94+
95+
Before you run the first lab, take a moment to read the code carefully.
96+
97+
!!! mascot-thinking "What Do You Think Will Happen?"
98+
![Monty thinking](../../img/mascot/thinking.png){ class="mascot-admonition-img" }
99+
Look at the program below before you click Run.
100+
The turtle moves forward 100, turns right 90°, moves forward 100, turns right 90°, then moves forward one more time.
101+
How many sides of a square do you think you will see?
102+
Make your guess — then click Run to find out!
103+
104+
## Try It Now
105+
106+
Edit the code below and click **Run** to see the result right on this page.
107+
No account needed — everything runs in your browser.
108+
109+
<script src="https://skulpt.org/js/skulpt.min.js"></script>
110+
<script src="https://skulpt.org/js/skulpt-stdlib.js"></script>
111+
112+
<div id="skulpt-lab">
113+
<div id="editor-container">
114+
<textarea id="code" spellcheck="false">import turtle
115+
wn = turtle.Screen()
116+
wn.bgcolor("lightyellow")
117+
t = turtle.Turtle()
118+
t.forward(100)
119+
t.right(90)
120+
t.forward(100)
121+
t.right(90)
122+
t.forward(100)
123+
</textarea>
124+
<div id="button-row">
125+
<button id="run-btn" onclick="runSkulpt()">&#9654; Run</button>
126+
<button id="reset-btn" onclick="resetSkulpt()">&#8635; Reset</button>
127+
</div>
128+
<pre id="output"></pre>
129+
</div>
130+
<div id="canvas-container">
131+
<div id="turtle-target"></div>
132+
</div>
133+
</div>
134+
135+
Were you right? The turtle drew exactly three sides of a square — one 90° right turn for each corner!
136+
137+
**If you see a red error message:** Check your spelling carefully.
138+
Python is case-sensitive, so `Forward(100)` will not work but `forward(100)` will.
139+
Also make sure each line ends with a closing parenthesis `)`.
140+
141+
## How It Works
142+
143+
Here is what each line of the program does:
144+
145+
| Line | What it does |
146+
|------|-------------|
147+
| `import turtle` | Loads the turtle library so Python knows all the drawing commands |
148+
| `wn = turtle.Screen()` | Creates the drawing window and stores it in `wn` |
149+
| `wn.bgcolor("lightyellow")` | Sets the window background to light yellow |
150+
| `t = turtle.Turtle()` | Creates a turtle object and stores it in `t` |
151+
| `t.forward(100)` | Moves the turtle forward 100 pixels in the direction it is currently facing |
152+
| `t.right(90)` | Rotates the turtle 90° clockwise — a perfect right-angle corner |
153+
154+
Notice the pattern: every command uses the object (`wn` or `t`), then a dot, then the method name with a number inside the parentheses.
155+
This is exactly the object-method pattern from Chapter 4.
156+
157+
## Pen Control: penup() and pendown()
158+
159+
Right now the turtle draws a line everywhere it moves.
160+
But what if you want two separate shapes with a gap between them?
161+
162+
Think about drawing with a real pen: you would lift the pen off the paper, slide it to the new spot, then press it back down.
163+
Turtle graphics works the same way.
164+
165+
- **`t.penup()`** lifts the pen off the canvas. The turtle can move without drawing anything.
166+
- **`t.pendown()`** sets the pen back on the canvas. Every step after this leaves a line.
167+
168+
The turtle always starts with the pen down, which is why the very first `forward()` call draws a line.
169+
170+
In Scratch you may have used the **Pen Up** and **Pen Down** blocks from the pen extension — `penup()` and `pendown()` work exactly the same way.
171+
172+
Here is a program that draws two separate lines with a gap in the middle:
173+
174+
```python
175+
import turtle
176+
t = turtle.Turtle()
177+
t.forward(80) # draw a line
178+
t.penup() # lift the pen
179+
t.forward(40) # move 40 pixels — nothing drawn here
180+
t.pendown() # lower the pen
181+
t.forward(80) # draw another line
182+
```
183+
184+
Try this in the lab below — pay close attention to the blank space in the middle of the drawing.
185+
186+
<div id="skulpt-lab-2">
187+
<div id="editor-container-2">
188+
<textarea id="code-2" spellcheck="false">import turtle
189+
t = turtle.Turtle()
190+
t.forward(80)
191+
t.penup()
192+
t.forward(40)
193+
t.pendown()
194+
t.forward(80)
195+
</textarea>
196+
<div id="button-row-2">
197+
<button id="run-btn-2" onclick="runSkulpt('-2')">&#9654; Run</button>
198+
<button id="reset-btn-2" onclick="resetSkulpt('-2')">&#8635; Reset</button>
199+
</div>
200+
<pre id="output-2"></pre>
201+
</div>
202+
<div id="canvas-container-2">
203+
<div id="turtle-target-2"></div>
204+
</div>
205+
</div>
206+
207+
The gap is where the turtle traveled with its pen in the air.
208+
Try changing the `40` in the middle `t.forward(40)` to `80` — the gap doubles in width.
209+
210+
!!! mascot-warning "Don't Forget pendown()!"
211+
![Monty warning](../../img/mascot/warning.png){ class="mascot-admonition-img" }
212+
The most common `penup()` mistake is forgetting to call `pendown()` afterward. If your turtle moves but the canvas stays completely blank, look for a missing `pendown()`. The turtle moves exactly as you command it — it just leaves no trace because the pen is still lifted. Add `t.pendown()` before the line you want drawn and you are back in business!
213+
214+
## Pen Size and Color
215+
216+
A black line on a white background is just the beginning.
217+
You can choose any color and any thickness for your turtle's pen.
218+
219+
Two commands control how the line looks:
220+
221+
- **`t.pensize(n)`** sets the line thickness in pixels. The default is `1`. Try `3`, `5`, or `10` to draw bolder lines.
222+
- **`t.pencolor("color")`** sets the color of the line the turtle draws. Use a color name in quotes: `"red"`, `"blue"`, `"green"`, `"orange"`, `"purple"`, or any named web color.
223+
224+
Just like the **Set pen color** block in Scratch, `pencolor()` changes the color for every line drawn after that call.
225+
You can change colors in the middle of a program — each call to `pencolor()` affects only the lines that come after it.
226+
227+
## Turtle Appearance: shape(), hideturtle(), and showturtle()
228+
229+
The default turtle looks like a small arrowhead.
230+
The **`t.shape()`** command swaps it for a different look.
231+
Here are all six built-in shapes:
232+
233+
| Shape name | What it looks like |
234+
|------------|-------------------|
235+
| `"arrow"` | A small arrowhead (the default) |
236+
| `"turtle"` | A top-down cartoon turtle |
237+
| `"circle"` | A filled circle |
238+
| `"square"` | A small filled square |
239+
| `"triangle"` | A small triangle |
240+
| `"classic"` | A slightly different arrowhead shape |
241+
242+
Sometimes you want the turtle cursor to disappear from a finished drawing so it does not distract from your art.
243+
Two commands handle this:
244+
245+
- **`t.hideturtle()`** makes the turtle cursor invisible. Your drawing still shows — the turtle just steps out of the way.
246+
- **`t.showturtle()`** brings a hidden turtle back into view.
247+
248+
## Speed Control: speed()
249+
250+
Every turtle starts at a medium drawing speed.
251+
The **`t.speed(n)`** command controls how fast it moves, using a number from `0` to `10`:
252+
253+
| Value | Speed |
254+
|-------|-------|
255+
| `1` | Slowest — good for watching each step |
256+
| `5` | Medium — the default |
257+
| `10` | Fast |
258+
| `0` | Instant — skips the animation entirely |
259+
260+
Use `speed(0)` when your program draws many lines and you do not want to wait.
261+
Use `speed(1)` when you are learning or debugging and want to see the turtle move one step at a time.
262+
263+
Now try a lab that combines color, appearance, and speed — all in one program:
264+
265+
<div id="skulpt-lab-3">
266+
<div id="editor-container-3">
267+
<textarea id="code-3" spellcheck="false">import turtle
268+
t = turtle.Turtle()
269+
t.shape("turtle")
270+
t.speed(5)
271+
t.pensize(4)
272+
t.pencolor("blue")
273+
t.forward(100)
274+
t.right(90)
275+
t.pencolor("red")
276+
t.forward(100)
277+
t.hideturtle()
278+
</textarea>
279+
<div id="button-row-3">
280+
<button id="run-btn-3" onclick="runSkulpt('-3')">&#9654; Run</button>
281+
<button id="reset-btn-3" onclick="resetSkulpt('-3')">&#8635; Reset</button>
282+
</div>
283+
<pre id="output-3"></pre>
284+
</div>
285+
<div id="canvas-container-3">
286+
<div id="turtle-target-3"></div>
287+
</div>
288+
</div>
289+
290+
Notice that the first line is blue and the second is red — changing `pencolor()` in the middle of a drawing affects only the lines that come after that call.
291+
When the program finishes, the turtle cursor itself disappears because of `hideturtle()`.
292+
293+
## Learning Check
294+
295+
You have now seen all ten commands from this chapter.
296+
Time to put them to the test!
297+
The program below draws two sides of a square and then stops.
298+
299+
!!! mascot-thinking "Your Turn — Add the Missing Line"
300+
![Monty thinking](../../img/mascot/thinking.png){ class="mascot-admonition-img" }
301+
The program below draws two sides of a square and then stops short.
302+
After the second `t.right(90)`, the turtle is already pointing in the right direction for the third side.
303+
Add **one line** to make it draw that third side — 100 pixels long.
304+
Hint: which command moves the turtle forward?
305+
306+
<div id="skulpt-lab-4">
307+
<div id="editor-container-4">
308+
<textarea id="code-4" spellcheck="false">import turtle
309+
t = turtle.Turtle()
310+
t.shape("turtle")
311+
t.speed(5)
312+
t.pencolor("green")
313+
t.pensize(3)
314+
t.forward(100)
315+
t.right(90)
316+
t.forward(100)
317+
t.right(90)
318+
# Add ONE line below to draw the third side of the square
319+
</textarea>
320+
<div id="button-row-4">
321+
<button id="run-btn-4" onclick="runSkulpt('-4')">&#9654; Run</button>
322+
<button id="reset-btn-4" onclick="resetSkulpt('-4')">&#8635; Reset</button>
323+
</div>
324+
<pre id="output-4"></pre>
325+
</div>
326+
<div id="canvas-container-4">
327+
<div id="turtle-target-4"></div>
328+
</div>
329+
</div>
330+
331+
If you got it right, you will see three sides of a square drawn in green.
332+
The missing line is `t.forward(100)` — the turtle was already facing the right direction after two right turns, so it just needed one more push forward!
333+
334+
## Experiments
335+
336+
Try these changes to the programs above.
337+
For each one, predict what will happen first — then run it to check!
338+
339+
1. In Lab 1, change both `t.right(90)` calls to `t.right(120)`. **You'll know it worked when** the turtle draws the corner of a triangle instead of a square corner.
340+
2. In Lab 1, change `wn.bgcolor("lightyellow")` to `wn.bgcolor("lightblue")`. **You'll know it worked when** the background turns a soft sky blue.
341+
3. In Lab 2, change the `40` in `t.forward(40)` (the middle one, between penup and pendown) to `100`. **You'll know it worked when** the gap between the two lines is much wider than the lines themselves.
342+
4. In Lab 3, try each of the six shape names in `t.shape()`: `"arrow"`, `"turtle"`, `"circle"`, `"square"`, `"triangle"`, `"classic"`. **You'll know it worked when** the turtle cursor changes shape before it starts drawing.
343+
5. **Challenge:** In Lab 3, change `t.speed(5)` to `t.speed(1)` and watch the turtle draw slowly. Then change it to `t.speed(0)` and run again. What is different? When would each speed setting be useful?
33344

34-
TODO: Generate Chapter Content
345+
!!! mascot-celebration "You Can Command a Turtle!"
346+
![Monty celebrating](../../img/mascot/celebration.png){ class="mascot-admonition-img" }
347+
You have just learned ten Python turtle commands and used them to draw real shapes on the screen — that is a big deal! In the chapters ahead you will combine these commands with loops and functions to draw patterns no human could make by hand. You have earned this one. Let's keep coding it together!

logs/ch-05-content-generation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2026-06-28 09:48:25 - Start
2+
2026-06-28 09:55:23 - End
3+
4+
## Chapter 5 — Drawing with Turtle Graphics
5+
6+
| Metric | Value |
7+
|--------|-------|
8+
| Start Time | 2026-06-28 09:48:25 |
9+
| End Time | 2026-06-28 09:55:23 |
10+
| Elapsed | ~7 minutes |
11+
| Word Count | 2,336 |
12+
| Concepts Covered | 10/10 ✓ |
13+
| Mascot Admonitions | 6/6 (welcome, tip/Scratch Bridge, thinking/prediction, warning, thinking/learning-check, celebration) |
14+
| Skulpt Labs | 4 (Lab 1: movement+Screen, Lab 2: pen control, Lab 3: colors+appearance, Lab 4: learning check) |
15+
| Explanation Tables | 2 |
16+
| Markdown Lists | 3 |
17+
| Scratch Bridges | 1 admonition + 3 prose-level connections |
18+
| Reading Level | Junior High (ages 10–13) |

0 commit comments

Comments
 (0)