Reference
How the pen works
Drawing with code is just a pen that goes forward and turns. Once you can picture where the pen is and which way it faces, you can read a loop and SEE the shape before you run it. Here’s the model. Open the drawing lab →
- The pen has a place and a heading
Think of a pen (a “turtle”) sitting on the paper, pointing in some direction. Everything you draw is relative to where it is and which way it faces right now.
- forward draws, turn rotates
forward(50)slides the pen 50 steps the way it faces, leaving a line.turn(90)spins the pen in place by 90° — it draws nothing on its own, it just changes the direction of the NEXT forward. A turn goes clockwise, like a clock’s hand. - A loop repeats to build a shape
for i in 1...4 { … }withforward(60)andturn(90)on their own lines inside does “forward, turn” four times. Four equal sides and four right-angle turns close into a square. The number of repeats is the number of sides. - The turn is the corner
A gentler turn makes a wider corner and a rounder shape: 90° → square, 72° → pentagon, 60° → hexagon. The turns of a closed shape always add up to one full 360° trip around.
- A star is a turn that overshoots
Turn MORE than the polygon angle and the pen skips across the middle: five sides with a 144° turn (not 72°) cross into a five-pointed star instead of a pentagon.
- penUp lifts the pen (a gap)
penUp()picks the pen off the paper so the next forward MOVES without drawing;penDown()puts it back. That is how you make gaps — dashes, or separate pieces. - Change something each loop
If a length GROWS each time (
d = d + 10), the sides get longer and the path winds outward into a spiral. Draw a whole shape many times, turning a little between each, and you get a flower.
The pen commands
for i in 1...4 {
forward(60)
turn(90)
} forward(n)Move n steps the way the pen faces, drawing a line (if the pen is down).turn(deg)Rotate the pen in place by deg degrees, clockwise like a clock’s hand. Draws nothing — it aims the next forward.penUp()Lift the pen: the next forward moves but leaves no line (a gap).penDown()Put the pen back down so forward draws again.for i in 1...n { … }Repeat the body n times — the heart of drawing a shape with a loop.
The trick to reading a pen program is to trace the loop one step at a time: forward moves, turn aims, repeat. The count tells you the sides; the turn tells you the corner. Read a pen program →