Unit 1: Tell the turtle what to do
Your initials
A computer does exactly what you tell it, in the order you tell it, and nothing else. In this step you give a turtle exact steps to draw your initials, and you end with a PDF of your work.
Planning on paper first makes it fast. People who plan finish early. People who skip it spend the whole time fixing.
How the turtle thinks
The turtle’s paper is like a graph. The middle is (0, 0), and y goes up: a bigger y is higher on the page.
The turtle also faces a direction, its heading:
| Heading | The turtle faces |
|---|---|
| 0 | up |
| 90 | right |
| 180 | down |
| 270 | left |
right and left turn the turtle. With the pen down it draws as it moves. With the pen up it moves without drawing.
Build an L
First watch one. Read the blocks in the frame from the top down. The grid shows where the turtle is: the numbers are x and y.
Before you press Run: the turtle starts at (-150, 60) facing down. Where will it end up, and which way will it face? Write your prediction down. Then press Run (it is slow on purpose) and check. Look at the turtle’s nose.
Check your answer
It ends at (-90, -60), facing right. left 90 turns the turtle from facing down to facing right.
Now build the same L yourself.
The round buttons on the left open the palettes of blocks (point at one to see its name). Drag a block out, and snap the next one under it. Tap a number on a block to change it.
| Palette | Blocks |
|---|---|
| Flow | start |
| Turtle | clean, set xy, set heading, forward, left |
| Pen | pen up, pen down |
Use the same blocks, in the same order, with the same numbers. Leave out the speed block: it only slowed the frame down. clean wipes the paper so every run starts the same way.
Plan your initials
Sketch your initials on this grid with straight lines. It is the same size as the turtle’s paper, and the numbers are the x and y you type into set xy. A letter about 120 tall works well. Print this page or copy the grid onto your own paper. (TurtleLab can show the same grid: press the grid button on the toolbar, the square with lines in it.)
For each letter, write down where the turtle starts, which way it faces, and the moves, like forward 120, left 90, forward 60.
Between letters
Look at the first four blocks of the L: pen up, set xy, set heading and pen down. They take the turtle to where the letter starts without drawing anything on the way. You use the same four blocks again between every two letters.
Here is an L and an I. Press Run and watch the turtle between the letters.
Work out:
- Which four blocks walk the turtle from the L to the I?
- The turtle ends the L facing right. Why does the I still draw downward?
- Change the 180 in the
set headingjust above the I to 90, and press Run. What happens to the I, and why? (Start over puts it back.)
Check your answers
pen up,set xy,set headingandpen down. The pen goes up so nothing is drawn on the way.set xypicks the place.set headingmakes the turtle face the way the letter starts. The pen goes down to draw.set heading 180turns the turtle to face down before the pen goes down. Which way it faced at the end of the L does not matter any more.- The I lies on its side and runs to the right, because the turtle now faces right when it draws. A letter is drawn in whatever direction the turtle faces, so set the heading before every letter.
Build your initials
Build one letter at a time, and tap its stack to test it before you start the next one.
Draw every letter piece by piece, one line or one curve at a time, with blocks like forward, left, right, set xy and arc. Do not use the write block from the Notes palette. It writes the letters for you, and building them yourself is the whole point of this step.
To travel between letters, use the four blocks from the L and I frame: pen up, set xy, set heading (180 is down, 0 is up) and pen down.
When your initials are done, draw a square frame around them and count how many blocks the frame took. Remember that number. You will see it again in Step 2.
Check yourself
- Every initial can be read by someone who has not seen your plan.
- There are no stray lines between your letters.
- You predicted where the L would end and which way it would face, and you checked.
- You built every letter yourself, piece by piece, with no
writeblock.
Make your PDF
Add a sticky note first: open the Notes palette, drag out a note, tap it and type your name.
Then press the PDF button on the toolbar. It saves your blocks, your note and your drawing, with a grid of x and y numbers on the drawing so anyone can check where each line is.
Keep your PDF. To get your blocks back later, press Open in TurtleLab and choose the PDF, or drag the PDF onto the page.
Done early?
Curves. Letters like C, O, S, D, B and U have curves. Use the arc block (Turtle palette). It takes an angle and a radius. A positive angle curves right and a negative angle curves left. Find out what an arc of 180 with a radius of 50 looks like, then use arcs in your letters.
Color. Give each letter its own color. The set color block (Pen palette) takes red, green and blue, each from 0 to 255. Red is 255, 0, 0. Black is 0, 0, 0. White is 255, 255, 255. Mix your own.
Unit 1: Tell the turtle what to do
Say it once
A square takes eight blocks the long way: forward and right, four times over. (If you did Step 1, your frame around your initials took eight blocks too.)
Computers are very good at repeating things, so you only have to say it once. The block for that is repeat (Flow palette). It runs the blocks inside it again and again.
Try a sample
The frame below is a real TurtleLab. Change a number, press Run, and look at the drawing. Start over puts the demo back the way it was. Nothing you do in a frame is saved, and Open full size opens it in a tab of its own. The square draws by itself.
Look at the blocks. The ones inside the repeat are the ones that repeat. The number on the repeat says how many times. The whole square is just one forward and one right, said once and repeated 4 times.
A triangle
A triangle has 3 sides, so change the 4 on the repeat to a 3 and press Run. Is it a triangle? Why not?
The turn is still 90, and that is too big. To come back to where it started, facing the same way, the turtle must turn one full circle, 360 degrees, all together. A triangle has 3 turns. Before you try, work out: what number, three times, makes 360?
Type your number into the right block and press Run. Then check it against this triangle.
Now change the 120 in the frame to 100 and press Run. What happens, and why?
Check your answers
The turn is 120, because 3 × 120 is 360. With 100 the triangle stays open: three turns of 100 make 300, not 360, so the turtle does not come back facing the way it started.
A pentagon
A pentagon has 5 sides. Predict the turn first: what number, five times, makes 360? Write your prediction down, then change the repeat to 5 in the square frame, type your turn into the right block and press Run. Then check it here.
Change the 5 on the frame’s repeat to 6, keeping the turn, and press Run. What does the sixth side do?
Check your answers
The turn is 72, because 5 × 72 is 360. The sixth side draws over the first side again. Five sides with a turn of 72 already closed the shape, so a sixth side starts it over.
The pattern
Look at the three shapes you have made:
| Shape | Sides | Turn |
|---|---|---|
| Triangle | 3 | 120 |
| Square | 4 | 90 |
| Pentagon | 5 | 72 |
The turn times the number of sides is always 360. So:
turn = 360 ÷ number of sides
Now use the pattern. Find the turn for a hexagon, an octagon and a shape with 12 sides. Change the repeat and the turn in the square frame, then run it. If your numbers are right, the shape closes up.
You choose the sides
The turtle can do that sum for you. In this frame the program asks how many sides you want, and it works out the turn itself, 360 ÷ your number. Two of the blocks in it are new: ask waits for you to type a number, and the box called box1 keeps it. You will learn both later. For now, just use it.
Press Run, type a number, and press Enter. Press Run again to be asked again.
- Type 3, then 5, then 8. What shapes do you get?
- Type 40, then 100. What does the shape look like, and why?
- Type 2. What does the turtle draw, and why?
Check your answers
- A triangle, a pentagon and an octagon.
- Nearly a circle. With 40 sides each turn is only 9 degrees, and with 100 sides it is 3.6, so the corners are too small to see. A circle is what you get when the number of sides keeps growing.
- A single line, out and back. The turn is 180, so the turtle turns around and walks back the way it came.
Two turns in one loop
So far the repeat held one forward and one right. It can hold more. Put in forward 100, right 90, forward 50, right 90 and repeat it 2 times. What shape is it?
Each trip through the loop turns 90 + 90 = 180, so two trips turn 360, and the shape closes.
A rhombus has four sides of the same length, but its corners are not square, so its two turns are different. Pick two turns that add up to 180, put the same number in both forward blocks, and run it.
Check your answers
The first program is a rectangle. For the rhombus, 60 and 120 work (120 and 60 work too). If the two forward numbers are different you get a parallelogram, not a rhombus.
Make your picture
Now put it all in one drawing. Open a fresh TurtleLab with Open full size under any frame, and build:
- Three regular shapes, such as a hexagon, an octagon and a shape with 12 sides. Use a
repeatfor each one and work out its turn with 360 ÷ sides. - A different color for each shape (
set color, before the shape). - One rhombus, from the section above.
- Every shape must close up.
Use only repeat, forward, right and set color. Add a sticky note with your name.
Save your work
To keep a copy of your blocks and your drawing, press the PDF button on the toolbar. A frame does not have one, so build in a tab of your own.
Unit 1: Tell the turtle what to do
Let chance draw
So far, every time you press Run you get the same picture. A loop repeats steps, but the steps are the same ones.
A program can also use chance. The random block (Math palette) picks a number each time it is used, so the picture can come out different on every run. Art, games and simulations all use it.
Try a sample
The frame below is a real TurtleLab. Change a number, press Run, and look at the drawing. Start over puts the demo back the way it was. Nothing you do in a frame is saved, and Open full size opens it in a tab of its own.
Before you run it a second time, make a guess: will the second picture match the first?
Then run it three times. Look at the set color block. Each of its three slots has a random 0 255 block plugged into it. A color mixes red, green and blue, and each one is a number from 0 to 255. That is why the random numbers go from 0 to 255.
Work out:
- What is the smallest number
random 0 255can give? The largest? - The sample draws 50 rays and turns 7.2 each time. 50 × 7.2 is what? Why does that number make sense?
- If you change it to 100 rays, what should the turn be?
Try your answer to question 3. Numbers on a block do not have to be whole numbers.
Change it
Make two changes to the sample, one at a time, and run it after each:
- The line length is 200. Drag a
randomblock out of the Math palette and drop it onto the 200 so it plugs in. Choose its two numbers so the rays have different lengths. - Change the number of rays and the turn so the rays still go all the way around.
Build confetti
This is the last practice before the capstone. Scatter 60 dots on the paper, each in a random place and a random color.
Here is a start. The pen is fat, so a very short line looks like a dot. The dots are already spread across the paper from left to right by a random block in the x slot of set xy. Press Run.
Two things are wrong, and you can fix both.
- The band. The dots land in a thin band. Look at the numbers in the
randomblock in the y slot. The paper runs from -280 to 280 up and down. Change the numbers so the dots cover the whole paper. - The color. Every dot is red, because
set colorhas three plain numbers. Plug arandomblock into each of the three slots, the same way as the color burst, and choose the numbers a color can be.
Check your answers
- The y block needs
random -280 280. The dots were picked between -20 and 20, which is a thin band. - Each slot of
set colorgetsrandom 0 255, so every dot has its own mix of red, green and blue.
When your confetti works, run it twice. Do the two runs match? Why or why not?
Make your picture
Now make your own. Open a fresh TurtleLab with Open full size under any frame, and build a confetti picture with:
- At least 80 dots, from a
repeat, spread over the whole paper. - A random color for every dot.
- A random size for every dot: plug a
randomblock intoset pen size. Choose numbers that give some small dots and some big ones.
Use only repeat, random, pen up, pen down, set xy, set color, set pen size and forward. Add a sticky note with your name.
Save your work
To keep a copy of your blocks and your drawing, press the PDF button on the toolbar. A frame does not have one, so build in a tab of your own.
Unit 1: Tell the turtle what to do
Choose a path
So far your programs do the same steps every time, even when chance changes the numbers. Sometimes a program has to decide.
Think of a fork in the road. A question has a yes or no answer. If the answer is yes, you take one road. If it is no, you take the other.
The block for that is if with an else (Flow palette). The Flow palette has two blocks that say if. Use the one with an else mouth. It has a pointed slot for the question and two mouths:
- The top mouth runs when the answer is yes.
- The bottom mouth, marked
else, runs when the answer is no.
Exactly one of the two runs. Never both, and never neither. That one idea is the whole block.
Try a sample
The frame below is a real TurtleLab. Press Run and look at the drawing. Start over puts the demo back the way it was. Nothing you do in a frame is saved.
Find the question in the pointed slot. It reads random 1 2 = 1. Think of it as a coin flip: the random block gives 1 or 2. If it gives 1 (the answer is yes), the turtle takes the top mouth. If it gives 2 (the answer is no), it takes the else mouth.
Work out:
- When the random number is 1, which way does the turtle turn? Which way does it turn in the
else? - Can the turtle ever do both in one pass? Can it do neither?
- Run it three times. Are the drawings the same? Why?
Trace it on paper
Pretend the coin gave these six flips in a row: 1, 1, 2, 1, 2, 2. The turtle starts facing up (heading 0). On a 1 it turns right 90. On a 2 it turns left 90.
What is the heading after each of the six flips? Write the six headings down. The first one is 90, because the first flip is a 1 and right 90 adds 90 to 0.
Check your answers
The headings after each flip are 90, 180, 90, 180, 90 and 0.
Change it
For these changes, and to save your PDF at the end, work in a full-size tab: use Open full size under the frame.
Do these one at a time and run after each:
- Change the turns so heads turns right 60 and tails turns left 120. What happens to the path?
- Make the color tell you which road was taken. Put a
set colorinside the top mouth and a differentset colorinside theelsemouth.
When you drop a block into a mouth, the mouth grows to hold it. This is what the stack should be shaped like:
repeat 120
if random 1 2 = 1
set color (one color)
right 90
else
set color (another color)
left 90
forward 15
Save your work
Your stack now has a repeat, a random, and an if and else that sets a color. Add a sticky note: open the Notes palette, drag out a note, tap it and type your name and one sentence saying what your if and else decides.
Then press the PDF button and keep your PDF.
Done early?
Make the turtle turn right only about 1 time in 4, and left the other 3 times. You need a random block with different numbers, and a < or = block.
Unit 1: Tell the turtle what to do
Capstone: random art
You now have three tools. A loop repeats. Random brings in chance. If and else makes a decision. In this capstone you use all three in one piece of art that is yours.
Nobody will make the same picture as you, because you choose what repeats, what is random, and what gets decided.
What you will build
A picture drawn by a loop, with something random in it and something decided by an if and else. It has at least two colors. A sticky note on the page says what it all does, in your own words. The list at the end of this page shows what to check.
Pick a theme
Choose one, or invent your own.
- Night sky. Stars at random places. A decision makes some stars big and bright and the others small and dim.
- Confetti party. Dots at random places. A decision picks between two team colors.
- Wandering path. A turtle that walks and turns by chance, and a decision that picks the turn and the color. Try
fill screen(Pen palette) first to make a dark background.
Plan it
Sketch on paper what you imagine the finished picture looks like. You can print the planning sheet and sketch on that.
Then finish these three sentences:
- My program repeats __ times.
- Each time, random picks __.
- The if and else decides __.
If you can fill in all three sentences, you are ready to build. If you cannot, go back to your sketch.
The shape of your program
Every capstone has this shape. Your job is to decide what goes in the gaps.
start
clean
repeat (how many times?)
(something random: a place, a size, a color, a turn)
if (a yes or no question about a random number)
(one thing happens)
else
(the other thing happens)
(draw something)
Build it
Build in small pieces and run after each one:
- First, a
repeatthat draws one thing, with no random yet. - Then plug in
randomwhere you want chance. - Last, add the
ifandelse, and put something visible in each mouth.
If your picture is slow to draw, add a speed block (Flow palette) near the top. Speed 4 is instant.
Check yourself
Before you save, check that:
- Your picture has a
repeatthat draws part of it. - You can see
randomat work in the picture. Run it twice. Does it change? - Your
ifandelsechanges something you can see, like a color, a size, or a turn. - Your picture has at least two colors.
- Your sticky note has your name. It says, in your own words, what your loop, your random, and your if and else each do.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
The Notes palette also has a write block. Leave it out of this one, so the picture comes from your loop, your random and your if and else.
Then press the PDF button. Your blocks, your note and your drawing are all on the page. Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
Something that grows. Open the Square spiral sample. It uses a box (a variable) that grows each time the loop runs. Read how box1 works, then use a box in your picture so something gets bigger or smaller each time through.
Unit 2: Remember and reuse
Variables: boxes that keep a number
So far you have typed every number right into a block: forward 100, right 90. That works, until you want a bigger picture. Then you hunt for every number that meant “the size” and change each one, and you miss one.
There is a better way. Keep the number in one place, and let every block ask for it. A place that keeps a value like this has a name in programming: a variable. It is called a variable because the value in it can vary: it can change while the program runs.
Variables in TurtleLab are boxes
TurtleLab draws a variable as a box, because a box is a good way to think about one:
- A box has a name on its lid. A variable has a name too.
- A box holds one thing. A variable holds one value.
- You can look inside a box whenever you like, and looking does not empty it.
- If you put something new in, the old thing is gone.
TurtleLab has four variables, named box1, box2, box3 and box4. They live in the yellow Boxes palette, and two kinds of block use them:
store in box1is a command, likeforward. It has a number slot on its right, and the number in that slot is what goes into the box.box1is a round yellow block. It stands for whatever number is in the box right now. You can plug it into any number slot, anywhere you could have typed a number.
Watch a variable change
The frame below is a real TurtleLab, and its program is slow on purpose, so you can see every block run. The block that is running lights up. The yellow strip at the bottom right of the drawing shows what is in each box. The Output box at the top right shows what a print block says. (Press Run to watch it again, or Start over.) Nothing you do in the frame is saved, and Open full size opens it in a tab of its own.
First look at the blocks, and predict before you watch:
- The first
store in box1has a 40 in its slot. What is inbox1after it runs? - The next two
store in box1blocks each have a+block in their slot:box1plus 10. What is inbox1after both have run? - The two
printblocks come next. What does each one print? Does printing change what is in the box? - The next
store in box1has a 100 in its slot. What happens to the number that was in the box before? - The last block is
forwardwith thebox1block in its slot. How long is the line the turtle draws?
Now press Run and watch the strip and the Output box. Were you right?
Check your answers
- 40.
-
- The first
+block works out 40 plus 10, which is 50, and stores it. The second works out 50 plus 10 and stores 60. Each time, the block works out the answer first, and only then puts it in the box.
- The first
- Each prints 60. Printing only looks inside the box. It does not change it.
- The 60 is gone. A variable holds one value, so storing a new one replaces the old one. The box now holds 100.
- 100, because
box1stands for the number in the box, and that is 100 now. (The Output box shows 60, 60 and 100, from the threeprintblocks.)
That is everything a variable does:
- It holds one value.
- Storing a value replaces whatever was there.
- Looking at it (using the
box1block) does not change it.
Try it: a house from one variable
In this frame, one variable sizes a whole house. Change a number, press Run, and look at the drawing.
Work through these, one at a time:
- Find the
store in box1block. What number is in its slot? The yellow strip shows what is in each box. - Count the
forwardblocks that have the yellowbox1block in their slot. How many are there? - Change the 80 in
store in box1to 120 and press Run. How long is every side of the house now? - Change it to 50 and press Run. How many numbers did you have to change?
Check your answers
- 80.
- Three: the
forwardin the walls, theforwardthat climbs to the roof, and theforwardin the roof. -
- The walls and the roof are all 120 long, because they all ask the same variable.
- One. Without a variable you would have had to find and change three numbers, and the house would look wrong if you missed one.
A variable can do the math too
In Step 2 you found that turn = 360 ÷ number of sides. Here the program works that out by itself, because the number of sides is in a variable.
- Change the 5 in
store in box1to 3, then 6, then 12. Press Run each time. - Find the two blocks that use
box1. What does each one do with it? - Why does one number in one variable make the shape close up every time?
Check your answers
- The
repeatuses it as the number of sides. The turn uses it inside the÷block, as360 ÷ box1. - Both blocks use the same variable, so they can never disagree. If you typed the sides and the turn in separately, you could give them numbers that do not go together, and the shape would not close.
Build one of your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Make a program that draws a square whose size is kept in a variable. You need store in box1 and box1 (Boxes palette), and repeat, forward and right (Flow and Turtle palettes). Run it. Then change one number and run it again to get a bigger square.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Use two variables: box1 for the size and box2 for the number of sides. Make a shape that can be any size with any number of sides by changing only the two stored numbers. You will need a ÷ block (Math palette) for the turn.
Unit 2: Remember and reuse
Boxes that count
In Step 6 you stored a number in a variable (a box) and used it. A variable can also change itself.
Take a store in box1 block. In its number slot, plug a + block from the Math palette. In the first slot of the +, put the box1 block, and in the second slot, type a 1. Now store in box1 does this, in order:
- The
+block looks insidebox1and adds 1. store in box1puts that answer back inbox1.
If box1 held 5, it holds 6 afterwards. Put that store in box1 block inside a repeat and the variable counts: one more every lap.
Watch a box count
Open the frame and press Run. The Output box at the top right of the drawing shows what the print block says. Here it prints box1 on every lap, and the same number is the length of the side the turtle draws.
Before you change anything, work out:
- The box starts at 0. What does the first lap print? What does the last lap print?
- Change the
10inbox1 + 10to a5and press Run. What does the last lap print now? - Change it back to 10, then change the
8on therepeatto a 12. What does the last lap print?
Check your answers
- The first lap prints 10, because the box goes from 0 to 10 before it is printed. The last lap prints 80.
-
- Eight laps of 5 is 40.
-
- Twelve laps of 10.
Trace a box on paper
Programmers follow a box by hand to see what a program does. Try it with this one, which multiplies instead of adds. Pretend you are the computer and write down what is in box1 after every lap.
store in box1: 2
repeat 3
store in box1: box1 × 2
print box1
- What does it print, lap by lap?
- What would the last number be if the
repeatsaid 5 instead of 3?
Check your answers
- It prints 4, then 8, then 16. The box starts at 2, and each lap doubles it.
-
- The five laps print 4, 8, 16, 32 and 64.
Counting down
A box can count down too. This sample counts with box1 - 1.
- What is in the box when the circle starts to draw?
- Why does the circle wait until the numbers have finished?
- Change the 3 in
store in box1to a 5, but leave therepeatat 3. What does it print? Then change therepeatto 5 as well.
Check your answers
-
- It started at 3 and lost 1 on each of three laps.
- The
repeathas to finish all of its laps before the blocks below it run. - With only 3 laps it prints 5, 4 and 3, then stops early. With a
repeatof 5 it prints 5, 4, 3, 2 and 1. The number in the box and the number of laps are two separate numbers, so you have to change both.
Make a spiral of your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Make a spiral where each side is longer than the last. A box holds the length of the side. On every lap the turtle draws a side as long as box1, turns, and then makes box1 bigger.
- Start with a turn of 90 and make the box 8 bigger each lap.
- Predict, then try a turn of 120. Then 60. What shapes do you get?
- Put the
store in box1that makes the box bigger inside therepeat. If it is outside, it only happens once.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Make the turn change too. Use a second box, box2, that starts at 10 and grows by 2 each lap, and use box2 as the number in right. Describe what you see.
Unit 2: Remember and reuse
Teach the turtle a word
In Step 2 you learned to say it once: a repeat runs the same blocks again and again. Now you will say it once in a different way.
Picture a flower with eight petals. Each petal is two curves and two turns, so four blocks. Eight petals the long way is 32 blocks. But the petals are all the same, so you could teach the turtle a new word, petal. Once it knows the word, one block draws a whole petal.
A word you teach the turtle is a block of your own. Programmers call it a function.
How it works
Open the My Blocks palette and drag out the to block. It is a hat, like start.
- Tap the first box and type the name, like
petal. - Put blocks under it. Those blocks are what the word means.
- A new block called
petalnow shows up in My Blocks. Drag it into your program wherever you want a petal.
Nothing under to runs by itself. It only runs when your program uses the block, and then it runs once for each use.
Teach it a size
Now the word needs to work for big petals and small petals. Tap the dashed input box on the to block and type a name, like size. Two things happen:
- Your
petalblock gets a place to type a number. - A
sizeblock appears in My Blocks. Plug it in wherever the number belongs, like the number inarc.
An input is a lot like a box. It holds one number, and the number is put in for you each time the block is used. Use petal 60 and size is 60 inside. Use petal 30 and it is 30.
Try it: three squares
The frame has a block called square with one input, size. The main program uses it three times.
- Which blocks are under the
toblock? What do they draw? - What is
sizethe first timesquareis used? The second time? The third time? - How many lines does the turtle draw in all?
- Change one of the three sizes and press Run. What changes in the drawing, and what stays the same?
Check your answers
- A
repeatof fourforwardandrightblocks, which draws one square. Theforwarduses thesizeblock for its length. - 40, then 80, then 120. They are the numbers typed into the three
squareblocks. -
- Three squares, four sides each.
- Only that one square changes. The other two use their own numbers.
A flower from one petal
Here is the flower. The petal is a block of your own.
- How many times does the main program use
petal? - Change the
60in the main program to 30. What changes? - After each petal the turtle turns 45. Why 45?
- If you change the
repeatfrom 8 to 6, what turn closes the flower? - Now find the
to petalblock and change the120inside it to 90. Press Run. How many petals changed?
Check your answers
- Eight times, because the
repeataround it says 8. - All eight petals get smaller, because they all use the same
size. - 8 petals times 45 is 360, a full circle, so the flower closes up. This is the same rule as turn = 360 ÷ number of sides.
- 60, because 6 times 60 is 360.
- All of them. You changed the word once, and every use of it changed. That is the real power of a block of your own: you can fix or improve something in one place.
Make a word of your own
Warm up first. Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Teach the turtle a word that draws a shape, and give it a size input. Triangle, square, star, hexagon: you choose. Then use your word three times with three different sizes. You will need to turn or move the turtle between the uses, or the shapes will sit on top of each other.
Your name, again
In Step 1 you built letters one line at a time. Now every letter can be a word the turtle knows, and your name is just those words in order.
Here is a B drawn two ways, each a block of its own. The main program only says which letter, and where. Press Run and watch.
- How many blocks are inside each B? Which one took fewer?
- Four blocks come before each B in the main program. Which four, and what do they do?
- Change the 0 in the
set headingjust above the curved B to 90, and press Run. What happens to the B, and why? (Start over puts it back.)
Check your answers
- The straight B has 15 blocks and the curved B has 9. An
arcdraws a whole round bowl in one block. pen up,set xy,set headingandpen down, the same four you used between letters in Step 1. The pen goes up so nothing is drawn on the way.set xypicks the place.set headingmakes the turtle face up. The pen goes down to draw.- The whole B turns a quarter turn to the right, and its spine runs sideways. A letter block draws in whatever direction the turtle is facing. The turtle ends the first B facing left, so without a
set headingthe second one would come out turned too. Every letter starts facing up, which is heading 0.
Now spell your name. Use your first name, or the letters you like from it if it is long.
- Make one block of your own for each different letter. A letter that comes twice, like the A in ANNA, only needs one block: ANNA is two blocks, A and N, used four times.
- A letter block draws from where the turtle is. It starts at the same corner of the letter and faces the same way every time (the B above starts at its bottom left, facing up). Do not put
set xyinside a letter block. A letter that always jumped to the same place would land on top of the last one. - In the main program, write the name: for each letter, the four blocks that travel, then the letter.
- Give every letter a color of its own. Put a
set colorblock (Pen palette) before a letter block to color that one use, or inside the letter block to make that letter always the same color. What changes if you move it?
You can start from your Step 1 PDF: press Open in TurtleLab, choose it, and move each letter’s blocks under a to block. A letter 120 tall and 60 wide, with 20 between letters, takes 80 across, and the paper is 700 wide, so it holds eight letters if the first starts at x = -320.
Check yourself
- Every different letter in your name is a block of your own, and a letter that comes twice uses the same block.
- No letter block has a
set xyinside it. - Every letter has a color of its own.
- You built every letter yourself, with no
writeblock. - Someone who has not seen your blocks can read your name.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Two inputs. Give a block two inputs, for example petal size and one more number that decides how wide the petal is. Add the second input by tapping the dashed input box again. A block can have two.
A size for every letter. Give each letter block an input called size and use it in the numbers, with the Math blocks: size for the height, size ÷ 2 for half of it. Then write your name at two sizes.
Angled letters. A B can have angled lines too. This E and B get from one letter to the next without set xy: the turtle walks. Press Run and watch it between them.
Which blocks take the turtle from the E to the B? How is that different from the four you used before?
Check your answer
pen up, forward 20, left 90 and pen down. The E ends facing right, so the turtle walks 20 along the paper, turns left to face up, and the B can start. set xy always lands in the same place. Walking only works when you know where the last letter ended and which way the turtle faces. Use whichever you like for your name.
A whole alphabet to read. Open A, B and C with straight lines or A, B and C with curves. Each letter is a block, and a block called place with two inputs, x and y, does the travelling. Look at the A if your name has slanted lines.
Unit 2: Remember and reuse
A loop inside a loop
A repeat runs the blocks inside it again and again. So what if one of the blocks inside is another repeat?
A square is a repeat of 4 sides. Now put that whole square inside a second repeat. The inside loop draws one square. The outside loop says “do that again”, and turns the turtle a little each time. Together they make a ring of squares from only a few blocks.
The rule to remember: the inside loop runs all of its laps every time the outside loop goes round once.
Try it: twelve squares
Open the frame and press Run. Look at the two repeat blocks, one inside the other.
Before you change anything, work out:
- How many
forwardblocks run in all? (Think: how many sides does one square have, and how many squares are there?) - After each square the turtle turns 30. Why 30?
- You change the outside
repeatto 6. What turn closes the ring?
Then try it: 6 squares with a turn of 60, and 36 squares with a turn of 10. Last, predict what happens with 12 squares and a turn of 20, and then run it.
Check your answers
-
- The inside loop runs 4 sides, and the outside loop runs it 12 times, so 12 times 4 is 48.
- 12 squares times 30 is 360, a full circle, so the ring closes. It is the same rule as before: laps times turn must make 360.
- 60, because 6 times 60 is 360.
With 12 squares and a turn of 20, the turns add up to 240, only two thirds of a circle. The squares fan out and leave a gap instead of closing a ring.
A flower of hexagons
The same idea makes a flower. This time the inside loop draws a hexagon: 6 sides, turning 60 each time.
- How many lines does the turtle draw in all?
- Change the inside
repeatfrom 6 to 5 and the turn inside it from 60 to 72. What shape does the inside loop draw now? To make a flower of five of them, what should the outsiderepeatand the outside turn be? - You change both
repeatblocks to 20. How many lines is that? Work it out before you try it.
Check your answers
-
- Six hexagons with 6 sides each.
- Pentagons. For a flower of five, the outside
repeatis 5 and the outside turn is 72, because 5 times 72 is 360. -
- A loop inside a loop multiplies: 20 times 20. This is why a few small numbers can draw a very detailed picture.
Which loop does what?
When you read a nested loop, ask two questions:
- What does the inside loop make? (One square. One hexagon.)
- What does the outside loop do with it? (Repeat it in a ring.)
Then check that the outside turn times the outside laps makes 360, if you want a ring that closes.
Make your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Make a ring of triangles, or of any shape you like, using a loop inside a loop. Pick the inside shape first and get it right. Then put it inside the outside loop. Work out the outside turn before you type it.
Then make it colorful: put a set color block inside the outside loop but outside the inside loop. Why does it change color for each shape, and not for each side?
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Open the Nested squares sample. It uses a loop inside a loop and a box. Find the box and work out what it does to each square. What does the Output box at the top right show?
Unit 2: Remember and reuse
Color is three numbers
Every color on a screen is made from three lights: red, green and blue. Your screen has a tiny red, a tiny green and a tiny blue light in every dot, and it makes each color by turning each light up or down.
TurtleLab’s set color block works the same way. It takes three numbers, one for each light, from 0 (that light is off) to 255 (all the way on).
Try it: a bar of color
Open the frame and press Run. The screen is filled with black, and the bar is drawn with the numbers 255 0 0: red all the way up, green and blue off.
Predict the color of the bar for each set of numbers. Then change the three numbers in set color, press Run, and check:
0 255 00 0 255255 255 0(two lights at full)255 255 255128 128 1280 0 0(careful: what is behind the bar?)
Then see if you can make orange. It is mostly red, with some green and no blue.
Check your answers
- Green.
- Blue.
- Yellow. Red light and green light together look yellow.
- White, all three lights at full.
- Gray. Any color where all three numbers are equal is a gray: a low number is dark gray, a high one is light gray.
- The bar vanishes. Black is all three lights off, and the screen behind it is black too.
For orange, try 255 128 0.
Why does it stop at 255?
The computer keeps each of the three numbers in 8 bits. A bit is a switch that is either off (0) or on (1). Eight switches make a row, and each switch is worth twice the one beside it:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |—|—|—|—|—|—|—|—|
To make a number, add up the values of the switches that are on. Everything on is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, and nothing on is 0. That is why the biggest number is 255. It is the biggest number eight switches can make.
Write each number as eight 1s and 0s, with a space in the middle to make it easier to read:
- 255 is
1111 1111. - 128 is
1000 0000, because only the 128 switch is on. - 10 is
0000 1010, because 8 + 2 is 10.
Look inside the color
The colored box under the words set color is a color picker. Use Open full size under the frame, then press that colored box. The picker shows each light as a number, as two hex digits, and as eight binary digits, and it shows all 24 bits at the bottom. Change one and the others follow.
- Type 10 into the red number. What binary does the picker show? Does it match your answer above?
- Which number gives the binary
0100 0000? - Hex uses sixteen digits, 0 to 9 and then A to F, and two hex digits hold one light. FF is 255. What color is
#FFFF00? What color is#00FF00? - Eight bits make how many different numbers? Three lights make how many different colors?
Check your answers
0000 1010.- 64, because only the 64 switch is on.
#FFFF00is yellow: red FF and green FF, blue 00.#00FF00is green.- Eight bits make 256 numbers, 0 to 255. Three lights make 256 times 256 times 256, which is 16,777,216 colors. That is 24 bits in all, which is what the picker shows at the bottom.
A color from a box
Last, put variables and colors together. In this sample, the variable box1 grows by 7 on every lap, and the color is made from it: red is box1, and blue is 255 - box1.
- The first square is drawn after box1 becomes 7. About what are its three numbers, and which light is strongest?
- The last square is drawn when box1 is 252. About what are its three numbers?
- Why does
255 - box1make blue fall exactly as red rises?
Check your answers
- Red 7, green 60, blue 248. Blue is the strongest, so the first square looks blue with a little purple.
- Red 252, green 60, blue 3. Red is the strongest, so the last square looks red.
- The two always add up to 255. When red is small, blue is big, and when red is big, blue is small.
Make your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Draw a row of squares, or a spiral, that fades from one color to another. Use a variable that grows each lap, and make the color from it, as the sample does. Predict the first color and the last color before you run it.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Pick any color you like in the picker. Write its three numbers, then write each one in binary. Check your eight 1s and 0s against what the picker shows.
Unit 2: Remember and reuse
Find the bug
A mistake in a program is called a bug. Every programmer writes them, all the time, and a big part of the job is finding them. The computer does exactly what you told it to do. A bug is only a place where what you told it is not what you meant.
Here is a way to hunt one down:
- Run it and look at what you got.
- Say what you expected. What should it have done?
- Find the difference, and the block that could cause it.
- Change one thing, then run it again.
Change only one thing at a time. If you change three, you will not know which one fixed it.
Below are four programs with one bug each. Each frame says what its program is meant to do. Run it, then find the bug and fix it in the frame. If you get stuck, open the hint. Open the answer last.
Bug 1: the triangle
Run it. Does the shape close up? Where does the turtle end up compared with where it started?
Hint
Think about Step 2. For a shape to close, the turns have to add up to something. What?
Answer
The turn is 100, but a triangle needs 120, because 360 divided by 3 sides is 120. With 100 the turns only add up to 300, so the shape never gets back to its start. Change the 100 in right to 120.
Bug 2: the box
Run it. Is the square 100 long? Look at the yellow strip at the bottom right of the drawing, which shows what is in each box.
Hint
What does the strip say box1 holds? How long is each side that is drawn? Is anything actually using box1?
Answer
Box1 holds 100, but no block asks for it. The forward has the plain number 50 typed into it, so the sides are 50 long. Storing a number in a box does nothing until another block looks inside. Drag a box1 block from the Boxes palette into the forward, on top of the 50.
Bug 3: the coin flip
Run it a few times. Does it wander? What does the drawing look like each time?
Hint
Look at the question in the pointed slot of the if. What numbers can random 1 2 give? Can the answer to the question ever be yes?
Answer
random 1 2 gives 1 or 2. The question asks whether it equals 3, and that is never true, so the program always takes the else and turns left every time. It draws the same small square over and over. Change the 3 to a 1 (or a 2) and the coin decides.
Bug 4: the house
Run it. What is missing? Did the turtle walk the roof, or not?
Hint
Look at the Pen palette. Is the pen up or down while the roof is being drawn? Who put it there?
Answer
The program lifts the pen with pen up to climb to the roof, and never puts it down again. The turtle walks the roof without drawing it. Put a pen down block after the forward that climbs and before the roof.
Make a bug of your own
Open TurtleLab in a new tab. Build or open a program that works, like your spiral from Step 7 or your flower from Step 8. Then break it in one small way: a wrong number, a block in the wrong place, a pen left up. Ask someone to run it and find your bug using the four steps above.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Look at the red outline TurtleLab puts around a block that has a problem, and read the message under the drawing. Break a program on purpose in a way that makes a red outline appear, for example by leaving the pointed slot of an if empty, or by dividing by zero. What does the message say, and does it tell you how to fix it?
Unit 2: Remember and reuse
Capstone 2: boxes and blocks
In the first capstone you put a loop, random and an if and else into one piece of art. Since then you have learned three more tools. A variable (a box) keeps a number and can change it. A block of your own is a word you teach the turtle. And a loop can go inside another loop.
In this capstone you use them together in one picture that is yours. Nobody will make the same one, because you choose what the box holds, what your block draws, and how it all repeats.
What you will build
A picture with these in it:
- A block of your own that has an input, and that your program uses more than once.
- A variable (a box) that changes while the program runs, so something grows, shrinks or fades.
- A loop that repeats.
- At least two colors.
- A sticky note on the page that says what it all does, in your own words.
The list at the end of this page shows what to check.
Pick a theme
Choose one, or invent your own.
- Growing garden. A
flowerorpetalblock with asizeinput. A loop uses it again and again, and a box makes each one bigger than the last. - Rainbow spiral. A block draws one side or one square. A box grows each lap to make the spiral wider, and the color is made from the box, as in Step 10.
- Tower of shapes. A block draws a shape with a
sizeinput. A loop stacks the shapes up, and a box makes each one smaller than the one below.
Plan it
Sketch on paper what you imagine the finished picture looks like.
Then finish these three sentences:
- My box holds __, and it changes by __ each lap.
- My block is called __. It draws __, and its input is __.
- My loop repeats __ times and uses my block __ time(s) each lap.
If you can fill in all three, you are ready to build. If you cannot, go back to your sketch.
The shape of your program
Every capstone has this shape. Your job is to decide what goes in the gaps.
to (the name of your block) (an input)
(blocks that draw one part, using the input)
start
clean
store in box1: (a number to start with)
repeat (how many times?)
(use your block, giving it box1)
(change box1: add, subtract, multiply)
(turn or move, and maybe set a color)
Build it
Build in small pieces and run after each one:
- First make your block work on its own. Use it once with a plain number, and check the drawing is what you meant.
- Then put it inside a
repeat, still with a plain number. - Last, put
box1where the plain number was, and add the block that changesbox1every lap. Remember it goes inside therepeat.
If your picture is slow to draw, add a speed block (Flow palette) near the top. Speed 4 is instant.
Check yourself
Before you save, check that:
- Your picture has a block of your own with an input, and your program uses it more than once.
- You can watch the box change: the yellow strip changes, and so does the picture.
- Your picture has a
repeatthat draws part of it. - Your picture has at least two colors.
- Your sticky note has your name. It says, in your own words, what your box, your block and your loop each do.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
The Notes palette also has a write block. Leave it out of this one, so the picture comes from your box, your block and your loop.
Then press the PDF button. Your blocks, your note and your drawing are on the first page. The last page lists what your program is made of, with a count of each kind of block: variables, loops, blocks you made, and more. Look at it. Does it match what you meant to build? Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
A loop inside a loop. Put your block inside an inside loop, and put that inside an outside loop, so the box makes a whole ring of shapes grow. Work out the outside turn first, so the ring closes.
Unit 3: Listen to the person
Ask for a number
So far you typed every number into a block before the program ran. A program can also ask for a number while it runs, so whoever runs it decides.
The ask block (Boxes palette) shows a question, waits for a number, and puts the number in box1. It is a variable you did not have to store yourself.
Try it: ask for the sides
The frame below asks its question as soon as it starts. Type a number and press Enter. (Press Run to be asked again.)
Before you type, work out:
- You type 5. How many sides will it draw, and how big is each turn?
- What will be in box1 afterwards? Look at the yellow strip.
- You type 0. What will it draw, and why?
Now try 5, 3, 12 and 0.
Check your answers
- Five sides. Each turn is 360 ÷ 5, which is 72.
-
- The answer goes in box1, and it stays there until something puts a new number in.
- Nothing. The
repeatruns 0 times.
If you press Enter on a word, or on nothing, the box says what to type and stays open. Cancel (or Escape, or Stop) ends the program.
Always box1
The answer always goes in box1. If you ask twice, the second answer replaces the first, just like storing a new number. To keep the first answer, store it in another box before you ask again:
ask: How long is a side?
store in box2: box1
ask: How many sides?
repeat box1
forward box2
right 360 ÷ box1
Trace it on paper. You type 50, then 5.
- What are box1 and box2 when the
repeatstarts? - What shape does it draw, and how long is each side?
- Take out the
store in box2block. What does the program draw now, and why?
Check your answers
- box1 is 5 (the second answer replaced the first) and box2 is 50 (stored before the second question).
- A pentagon, with sides of 50.
- Nothing. Without the store, box2 is still 0, so
forward box2goes forward 0.
Build one of your own
Open TurtleLab in a new tab and build there. The frame above does not keep your work.
Make a program that asks how long a side is, then draws a square that long. You need ask and box1 (Boxes palette), and repeat, forward and right. Run it and type 80. Run it again and type 40.
Save your work
Run your program and answer its question first. Then press the PDF button: the PDF keeps the drawing you just made.
If you press PDF before you have run your program, TurtleLab runs it for you. Nobody is there to answer, so it uses 0, and the drawing is empty.
Done early?
Ask twice. Ask for the length of a side and for the number of sides, as in the trace above, and draw any polygon of any size. Then try a third question for the pen thickness (use store in box3).
A flower. Use your block from Step 8. Ask how many petals there are, and turn 360 ÷ the answer between petals.
Unit 3: Listen to the person
Click to draw
Until now your program ran from its first block to its last, and then it stopped. A program can also keep listening for what a person does. In TurtleLab it can listen for clicks on the paper.
The when paper clicked block (Flow palette) looks like start, but Run never starts from it. Put blocks under it, and they run every time you click the paper.
Right under it in the palette are two round blocks, mouseX and mouseY. They say where you clicked, in the same x and y numbers that set xy uses. They are built in: you never store anything in them.
Try it: stamp a square
Open the frame and click the paper a few times. Look at the Output box at the top right.
The program’s own blocks (start and clean) ran once, at once. But the program did not end: it is listening. Press Stop to end it, and the Run button comes back. Press Run to listen again.
Before you click, work out:
- You click the middle of the paper. What will mouseX and mouseY say?
- You click near the top right corner. Will mouseX be positive or negative? And mouseY?
- The square starts at your click. Which corner of the square is where you clicked, and why does the square go up and to the right?
Now click in the middle, then near each corner, and check.
Check your answers
- About 0 and 0.
- Both are positive. Right of the middle is a positive x, and above the middle is a positive y. (The paper goes from -350 to 350 across, and from -280 to 280 up and down.)
- The bottom left corner. The turtle goes to your click facing up, so the first side goes up, and the turns go to the right.
What each part does
- The blocks under
startrun once, as always. - A program that has a
when paper clickedthen keeps listening, until you press Stop. - Each click runs the blocks under
when paper clicked, from the top to the bottom, with mouseX and mouseY set to that click. - Sliding the paper with the mouse is not a click. Neither is clicking on a block.
Build one of your own
Open TurtleLab in a new tab and build there. The frame above does not keep your work.
Make a dot painter: every click draws a fat dot where you click. You need when paper clicked and mouseX and mouseY (Flow palette), and pen up, set xy, pen down, set pen size and forward. A very short line drawn with a fat pen looks like a dot, as in Step 3.
Press Run, then click. If nothing happens, check that the message says it is listening, and click the paper, not a block.
Save your work
Press Run, click the paper to make your drawing, and then press the PDF button. The PDF keeps what is on the paper.
Done early?
A color for every click. Put a set color before the dot, with a random block in each of its three slots, as in Step 3.
A stamp of your own. Make a block of your own that draws a flower (Step 8), and use it under when paper clicked, so every click plants a flower.
Unit 3: Listen to the person
Capstone 3: ask and click
In Capstone 2 you used a box, a block of your own and a loop. Now you add the two ways a program can listen to a person: ask and when paper clicked. Build one picture that the person using it helps to make.
Nobody will make the same one, because you choose what the program asks, what a click does, and what your block draws.
What you will build
A program with these in it:
- An ask block, and an answer that your program really uses.
- A when paper clicked that draws something where you click, using mouseX and mouseY.
- A block of your own with an input, that your program uses more than once.
- A loop that repeats.
- At least two colors.
- A sticky note on the page that says what it all does, in your own words.
Pick a theme
Choose one, or invent your own.
- Stamp shop. Ask how big the stamp is. Every click stamps a flower (or a star, or a house) that size where you click.
- Polygon painter. Ask how many sides. Every click draws a polygon with that many sides, starting where you click.
- Dot painter. Ask how fat the dots are. Every click draws a dot of that size, and the color changes with where you click.
Plan it
Sketch on paper what the picture looks like after a few clicks.
Then finish these three sentences:
- My program asks __, and uses the answer to __.
- My block is called __. It draws __, and its input is __.
- Each click __ at the spot that was clicked, using mouseX and mouseY.
If you can fill in all three, you are ready to build. If you cannot, go back to your sketch.
The shape of your program
to (the name of your block) (an input)
(blocks that draw one thing, using the input)
start
clean
ask: (your question)
(blocks to set the color, or the pen size)
when paper clicked
pen up
set xy mouseX mouseY
pen down
(use your block, giving it box1 or a number)
Build it
Build in small pieces and run after each one:
- First make your block work on its own. Use it once with a plain number, and check the drawing.
- Then put it under
when paper clicked, withpen up,set xy mouseX mouseYandpen downbefore it. Press Run, click the paper, and check that it draws where you click. - Last, add the
askblock understart, and putbox1where the plain number was.
Press Run, answer the question first, and then click.
Check yourself
Before you save, check that:
- Your program asks a question, and the answer changes the picture.
- A click draws something where you clicked, not somewhere else.
- Your picture has a block of your own with an input, and your program uses it more than once.
- Your picture has a
repeat. - Your picture has at least two colors.
- Your sticky note has your name. It says, in your own words, what your question, your click and your block each do.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
Then press Run, answer the question, and click the paper a few times to make your picture. Then press the PDF button. Your blocks, your note and your drawing are on the first page. The last page lists what your program is made of, with a count of each kind of block: variables, loops, blocks you made, ask and click, and more. Look at it. Does it match what you meant to build? Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
Two questions. Ask for a size and a number of petals. Keep the first answer in box2 before you ask again, as in Step 13.
Click to change. Make the color depend on where you click, by using mouseX and mouseY in the numbers of set color.
Unit 4: Make a game
Catch the square
In Step 14 a click drew something. A game has to do more than that: it has to decide whether the click hit something. That is a yes or no question, and the if block is how a program asks one.
One question is often not enough. A click is inside a stripe only if it is more than the left edge and less than the right edge. That is two questions, joined together. Three blocks in the Math palette join them: and, or and not.
Try it: inside the stripe
The frame has a red stripe, and numbers along the paper that say x. Click inside it, then click outside it. A ring opens where you click, so you can see where the program heard you. The Output box at the top right prints where you clicked (the x), and then 1 if the click is inside the stripe or 0 if it is not.
Before you click, work out:
- You click at x = 160. Is
mouseX > 100yes or no? IsmouseX < 220yes or no? What doesandsay, and what gets printed? - You click at x = 300. Which of the two questions is no? What does
andsay? - You click at x = 50. Which question is no this time? What gets printed?
Now click in the middle of the stripe, to the left of it and to the right of it, and check.
Check your answers
- Both are yes, so
andsays yes, and the program prints 1. mouseX < 220is no, because 300 is not less than 220. One no is enough to makeandsay no, so it prints 0.mouseX > 100is no, because 50 is not more than 100. It prints 0.
and, or and not
- and says yes only when both of its questions are yes.
- or says yes when at least one of its questions is yes (or both).
- not flips one answer: yes becomes no, and no becomes yes.
| first question | second question | and | or |
|---|---|---|---|
| yes | yes | yes | yes |
| yes | no | no | yes |
| no | yes | no | yes |
| no | no | no | no |
Their holes are pointed, like the one in if. A yes or no block fits (less than, greater than, equals, or another and, or or not) and a number block does not. So an and can hold another and when you need three or four questions.
Try it: outside the stripe
This frame has the same stripe, but it asks the question the other way round, with or: a click is outside the stripe if it is less than 100 or more than 220. This time it prints 1 for a click outside the stripe, and 0 for a click inside.
Before you click, work out:
- You click at x = 160. Is
mouseX < 100yes or no? IsmouseX > 220yes or no? What doesorsay? - You click at x = 300. What does
orsay now, and why? - A
notaround theandfrom the first frame gives the opposite answer. For the click at x = 160, what doesandsay, and what doesnotof it say?
Check your answers
- Both are no, so
orsays no, and the program prints 0. mouseX > 220is yes. One yes is enough to makeorsay yes, so it prints 1.andsays yes, sonotof it says no.
Catch the square
Here is a whole game. A red square sits at a random place. Click inside it to score a point, and it jumps somewhere else. Your score is in box1, and the yellow strip at the bottom right shows it.
The blocks are hidden in the frame to leave room for the game. Use Open full size, under the frame, to see them.
How does it know the click hit the square?
A square is two stripes that cross: one standing up (the columns it covers) and one lying down (the rows it covers). A click is inside the square only if it is inside both stripes.
Say the square’s bottom left corner is at x = box2 and y = box3, and the square is 60 wide and 60 tall.
mouseX - box2says how far right of the corner the click is. The click is in the square’s columns when that is more than 0 and less than 60.mouseY - box3says how far above the corner the click is. The click is in the square’s rows when that is more than 0 and less than 60.
Check it on paper. The corner is at x = 50 and y = -30.
- You click at x = 80 and y = 10. Is the click in the square?
- You click at x = 80 and y = 50. Is the click in the square? Which question says no?
- You click at x = 40 and y = 10. Which question says no?
Check your answers
- Yes. The click is 30 right of the corner and 40 above it, and both are more than 0 and less than 60.
- No. It is 30 right of the corner, which is fine, but 80 above it, which is not less than 60. The question about mouseY says no.
- The question about mouseX. The click is 10 left of the corner, so
mouseX - box2is -10, which is not more than 0.
Build it
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
The square needs a block of its own, so it can be drawn again every time it jumps:
to target
clean
store in box2: random -300 240
store in box3: random -220 130
pen up
set xy box2 box3
pen down
start fill
repeat 4
forward 60
right 90
end fill
clean wipes the old square off the paper. The two random blocks pick where the new one goes, and box2 and box3 remember it so the click can be checked against it.
The rest is the shape of your program. The words in brackets are for you to build:
start
clean
store in box1: 0
target
when paper clicked
store in box4: (how far right of the corner is the click?)
if (is that more than 0 AND less than 60?)
store in box4: (how far above the corner is the click?)
if (is that more than 0 AND less than 60?)
(add 1 to the score in box1)
(jump the square)
Build in small pieces and run after each one:
- First make
targetwork on its own. Put it understart, press Run, then press Run again. Does the square move each time? - Add
when paper clicked, with the firststore in box4and aprint box4under it. Press Run and click. Does the Output box say how far right of the corner you clicked? - Add the outer
ifwithand, and aprint 1inside it. Click in the square’s columns, above and below the square. Does it print 1? - Add the second
storeand the innerif. Then replace theprintwith the score and the jump.
Press Run, and click the square.
Check yourself
Before you save, check that:
- The square jumps to a new place after every hit, and not after a miss.
- Your score in box1 goes up by 1 for each hit.
- Your
ifusesandto ask two questions at once. - You made a block of your own, and your program uses it twice.
- Your picture has a
repeat. - You added a sticky note with your name that says, in your own words, what
anddoes in your game.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
Then press Run, catch the square a few times, and press the PDF button. Your blocks, your note and the paper are on the first page. The last page lists what your program is made of, with a count of each kind of block. Look at it. Does it match what you meant to build? Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
One question, not two. Use one if with an and that holds two more and blocks, so all four questions are asked at once. Use mouseX and mouseY directly, like mouseX > box2. Then you do not need box4 at all.
Lose a point for a miss. With the one if above, change it to if with an else, and take 1 off box1 when the click misses.
Change the size in one place. Keep the size of the square in box4 and use it in the forward block and in every question. Now you can make the game harder by changing one number.
The same question with not. Start with an if that asks if the click hit the square. Then ask the opposite with a not around it, and draw a small dot wherever a click misses.
Unit 4: Make a game
Steer the snake
In Step 14 a click drew something. Games listen to more than the mouse: they listen to the keyboard, and they keep moving by themselves while you think. Two more blocks in the Flow palette do that:
- when [key] pressed runs the blocks under it every time you press one key.
- every [ ] runs the blocks under it again and again, on a timer.
With them, and what you already know, you can make a snake.
Try it: steer with the arrow keys
Click the paper once, so the frame knows the keys are for it. Then press the up, left and right arrow keys.
Before you press anything, work out:
- You press the up arrow three times. How long is the line?
- Then you press the left arrow once. Does the turtle move? What does it do?
- Then you press the up arrow twice. Which way does the turtle walk, and how far?
Now try it and check.
Check your answers
-
- Each press walks 20, and 3 × 20 is 60.
- It does not move. It turns a quarter turn to the left, on the spot, because the left arrow only has a
left 90block under it. - Left, which is the way the turtle was turned, and 40 far: two presses of 20.
The two blocks
when [key] pressed. Tap the box on it and choose the key: the four arrows, space, enter, any letter or any digit. The blocks under it run every time you press that key. If you hold the key down it keeps running, like typing. You can have as many as you like, one for each key.
A program with one of these does not end after its last block. It waits for keys until you press Stop.
every [ ]. The number is in tenths of a second, like wait: 10 is one second, 2 is a fifth of a second. The timer starts after the last block under start has run, and it keeps going until you press Stop.
Try it: a program that moves by itself
Nothing to press here. Watch the spiral draw itself.
Before you look closely, work out:
- How many seconds are there between one tick and the next?
- The first tick walks 8, and box1 grows by 8 each time. How far does the third tick walk, and what is in box1 after it?
- What two changes would make the spiral grow faster?
Check your answers
- 0.3 of a second, because the number is 3 and it is in tenths.
- The first tick walks 8 and makes box1 16. The second walks 16 and makes it 24. The third walks 24 and makes box1 32.
- Make the number on every smaller, so the ticks come sooner. Or make the number added to box1 bigger, so each side is longer than the last by more.
The blocks under every take time to run, like any blocks. At the slow speeds each block waits before the next one. For a game, put a speed block set to 4 (instant) at the top of start, and the blocks under every run all at once.
Catch the square, with a snake
Here is a whole game. The snake moves by itself. Steer it with the arrow keys. Eat the red square and the snake gets fatter and a new red square appears somewhere else. Go off the edge and it starts again.
Click the paper once so the frame hears your keys.
The blocks are hidden in the frame to leave room for the game. Use Open full size, under the frame, to see them.
A real snake game remembers where every piece of its tail is. You only have four boxes, which is not enough for that, so this snake gets fatter instead of longer. For the same reason it cannot rub out a square it has eaten: it only remembers where the newest square is. The old ones stay on the paper.
How does it work?
The snake is the turtle. The every 2 block runs a forward 20 five times a second. The four arrow keys each have a block that sets the heading: 0 is up, 90 is right, 180 is down and 270 is left.
Remember where you were. The red square is drawn by a block called food. To draw it the turtle has to walk to a new place, so it first puts where it is in two boxes (x into box3 and y into box4), and when the square is done it walks back with set xy box3 box4. box1 and box2 keep where the square is. That is all four boxes.
Is the snake on the square? It is the question from Step 16, asked every tick. The square’s corner is at x = box1 and y = box2. The snake is on the square when it is more than -30 and less than 60 away from the corner in x, and the same in y. The numbers are a little bigger than the square, because the snake moves 20 at a time and a thick line could otherwise jump right over it.
Is the snake off the paper? That is two if blocks, each with two questions joined with or: x is more than 330, or x is less than -330. Then, in the next if: y is more than 260, or y is less than -260. One yes in an or is enough.
Check it on paper. The square’s corner is at x = 50 and y = -30.
- The snake is at x = 70 and y = 10. Is it on the square?
- The snake is at x = 70 and y = 50. Is it on the square? Which question says no?
- The snake is at x = 10 and y = 10. Which question says no?
- The snake is at x = 340 and y = 0. Which of the edge questions says yes?
Check your answers
- Yes. It is 20 right of the corner and 40 above it, and both are more than -30 and less than 60.
- No. It is 20 right of the corner, which is fine, but 80 above it, which is not less than 60. The question about y says no.
- The question about x. It is 40 left of the corner, so x minus box1 is -40, which is not more than -30.
x > 330. 340 is more than 330, so theorsays yes and the snake starts again.
Build it
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Two blocks of your own do the heavy work. The first draws a red square somewhere new and comes back:
to food
store in box3: x
store in box4: y
pen up
set xy (random -300 240) (random -220 130)
store in box1: x
store in box2: y
set color 255 0 0
start fill
repeat 4
forward 40
right 90
end fill
set color 0 150 0
set xy box3 box4
pen down
The second starts the game again from the middle:
to restart
clean
set pen size 8
pen up
set xy 0 0
set heading 90
pen down
food
The rest is the shape of your program. The words in brackets are for you to build:
start
speed 4
clean
restart
when up arrow key pressed
set heading 0
(the same for right arrow with 90, down arrow with 180, left arrow with 270)
every 2
forward 20
if (is x past an edge? x > 330 OR x < -330)
restart
if (is y past an edge? y > 260 OR y < -260)
restart
store in box3: (how far right of the square's corner is the snake?)
store in box4: (how far above the square's corner is the snake?)
if (is box3 more than -30 AND less than 60?)
if (is box4 more than -30 AND less than 60?)
set pen size (pen size + 4)
food
Build in small pieces and run after each one:
- Start with only
start,set heading 90and anevery 2withforward 20under it. Press Run. Does the turtle walk by itself? - Add the four
when key pressedblocks. Press Run, click the paper once, and steer. - Make
foodwork on its own: put it understart, press Run, and press Run again. Does the square move each time? - Add the two
ifblocks with theorfor the edges, and makerestart. - Add the two
storeblocks and the twoifblocks that eat the square.
Press Run, click the paper, and play.
Check yourself
Before you save, check that:
- The snake moves by itself, and the arrow keys turn it.
- Eating the red square makes the snake fatter and a new square appears.
- Going off the paper starts the game again.
- Your program uses a
when key pressedblock, aneveryblock, anandand anor. - You made two blocks of your own, and your program uses them.
- You added a sticky note with your name that says, in your own words, what
everydoes in your game.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
Then press Run, click the paper, steer the snake and eat a few squares, and press the PDF button. Your blocks, your note and the paper are on the first page. The last page lists what your program is made of, with a count of each kind of block. Look at it. Does it match what you meant to build? Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
Different keys. Choose w, a, s and d in the key boxes, so you steer with your left hand.
Turn around. Add a when space key pressed block with one block under it that turns the snake around, right 180.
Rub out the old square. Draw a white square, with set color 255 255 255, on top of the old one before you draw the new one. What happens to the snake’s line? Why?
Wrap around. Instead of starting over at the edge, send the snake to the other side. Put pen up before the set xy and pen down after it, so it does not draw a line right across the paper.
A bigger meal. Make the snake fatter by more than 4, or make it change color each time it eats: set color 0 (pen size × 3) 0.
Your own game. Use when key pressed and every for something that is not a snake: a ball that bounces, a turtle that runs away from a square, a drawing you steer.
Unit 4: Make a game
Capstone 4: make a game
You can now make a program listen three ways: a click (when paper clicked), a key (when [key] pressed) and a timer (every). And you can make it decide with if, and, or and not. Put them together and build a small game or toy that a person plays.
Nobody will make the same one. You choose what the person controls, what the program does by itself, and how it decides what happens.
Look at some first
Open each one, press Run if it is not already running, and play with it. Then look at the blocks to see how it works. They all start from what you already know.
- Keyboard painter listens for keys: the arrows steer and r, g and b change the color.
- The bouncing turtle moves by itself on a timer, and an or says when it is near an edge.
- Click flowers listens for clicks, and uses a block of its own and random.
- Catch the square is the game from Step 16, and The fat snake is the game from Step 17.
What you will build
A program with these in it:
- Two kinds of listening, at least: pick two of
when paper clicked,when [key] pressedandevery. - A box that changes while the program runs: a score, a count, or where something is.
- An if that uses and or or (or both) to decide something.
- A block of your own that your program uses more than once.
- A loop that repeats.
- At least two colors.
- A sticky note on the page that says what your game is and how to play it, in your own words.
Pick a theme
Choose one, or invent your own.
- Click race. A target sits at a random place. Every few seconds it jumps somewhere else by itself (
every). Click inside it before it jumps to score. Is the click inside? That is Step 16. - Steer a painter. A turtle walks by itself and leaves a trail (
every). The arrow keys turn it, and letter keys change its color or its pen size. - Catch the falling dot. A dot falls down the paper by itself (
every). Two keys slide a bar along the bottom. Score when the bar is under the dot when it lands. To move something without leaving a trail, usecleanand draw everything again at its new place on every tick. - Your own snake. Start from what you built in Step 17 and make it yours: a different food, a different rule for growing, wrap around the edges, different keys. It must still do something new that Step 17 did not.
Plan it
Sketch on paper what the game looks like when you play it, and mark where the score or the count shows.
Then finish these three sentences:
- The person plays by __ (pressing which keys, or clicking where).
- By itself, my game __ every __ (what the timer does, and how often).
- My game keeps __ in box __, and uses an if with and or or to decide __.
If you can fill in all three, you are ready to build. If you cannot, go back to your sketch.
The shape of your program
to (the name of your block) (maybe an input)
(blocks that do one thing, like draw something)
start
speed 4
clean
(set up your boxes)
when (a key) pressed
(what that key does)
every (a number of tenths)
(what the game does by itself)
if (a question joined with AND or OR)
(what happens)
Build it
Build in small pieces and run after each one:
- First make your block work on its own. Use it once under
start, press Run, and check the drawing. - Add the first thing the person does, a
when [key] pressedor awhen paper clicked. Press Run, try it, and check that it does what you meant. - Add the
everyblock with one block under it. Press Run and watch it go by itself. - Add the box and the
ifwith and or or. Use aprintblock to see what a box holds while you play. - Last, add the score, the colors and anything fun.
Put speed 4 under start so the blocks under every run all at once. Press Run, click the paper once, and play.
Check yourself
Before you save, check that:
- Your program listens in at least two of the three ways (a click, a key, a timer), and it works when you play it.
- A box changes while it runs, and you can say what it holds.
- Your program has an
ifthat usesandoror. - You made a block of your own, and your program uses it more than once.
- Your program has a
repeat. - Your picture has at least two colors.
- Your sticky note has your name. It says what your game is and how to play it, in your own words.
Make your PDF
Add the sticky note: open the Notes palette, drag out a note, tap it and type. A sticky note holds about 300 letters, so keep it short.
Then press Run, click the paper once, and play for a little while so there is something on the paper. Then press the PDF button. Your blocks, your note and your drawing are on the first page. The last page lists what your program is made of, with a count of each kind of block: variables, loops, blocks you made, the ways it listens, and more. Look at it. Does it match what you meant to build? Keep your PDF. If you share this computer, press New when you are done so the next person starts fresh.
Done early?
How fast? Use ask at the start to ask the person how fast they want to play. The answer goes in box1, so use box1 in the number on every. The timer reads it when it starts, so ask first, before the last block.
Two players. Give a second player their own keys, like w, a, s and d.
Game over. Make something happen at the end of a round: the screen clears, the color changes, or the turtle goes home.
Show the score. Use a print block so the score shows in the Output box every time it changes.