Home (learn.porttack.com) TurtleLab TurtleLab Lessons Python in 3D Studio Python in 3D Lessons MicroPython on Pi Pico and Projects Pi Pico Projects Electronics 101 Working in Python CS50 Problems CS Unplugged Student Movies ROV Robotics OpenSCAD Logo AP CSP Reference Standards Licensing

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.

An L drawn slowly on a grid. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-l

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.

Build in a fresh tab

Open TurtleLab in a new tab and keep this page next to it. That tab remembers your blocks if you refresh. Its Notes palette has no write block, on purpose: you draw the letters 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.)

-300 -200 -100 100 200 300 -200 -100 100 200 0 x y

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.

An L and an I, with the four blocks that walk the turtle from one letter to the next. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-travel

Work out:

  1. Which four blocks walk the turtle from the L to the I?
  2. The turtle ends the L facing right. Why does the I still draw downward?
  3. Change the 180 in the set heading just above the I to 90, and press Run. What happens to the I, and why? (Start over puts it back.)
Check your answers
  1. pen up, set xy, set heading and pen down. The pen goes up so nothing is drawn on the way. set xy picks the place. set heading makes the turtle face the way the letter starts. The pen goes down to draw.
  2. set heading 180 turns 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.
  3. 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 write block.

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.

A square: one forward and one right, repeated 4 times. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=square

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.

A triangle: a forward and a right of 120, repeated 3 times. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-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.

A pentagon: a forward and a right of 72, repeated 5 times. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-pentagon

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.

A program that asks how many sides, then draws a shape with that many. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-sides-any

  1. Type 3, then 5, then 8. What shapes do you get?
  2. Type 40, then 100. What does the shape look like, and why?
  3. Type 2. What does the turtle draw, and why?
Check your answers
  1. A triangle, a pentagon and an octagon.
  2. 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.
  3. 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 repeat for 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.

Color burst: 50 rays, each in a random color. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=color-burst

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:

  1. What is the smallest number random 0 255 can give? The largest?
  2. The sample draws 50 rays and turns 7.2 each time. 50 × 7.2 is what? Why does that number make sense?
  3. 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:

  1. The line length is 200. Drag a random block 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.
  2. Change the number of rays and the turn so the rays still go all the way around.

How a block plugs in

A round block like random plugs into a round number slot. When you drop it on a slot, the slot grows to hold it. Drag the block back out to take it off, and a plain number comes back.

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.

A start on confetti: sixty red dots in a thin band across the middle. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-confetti-start

Two things are wrong, and you can fix both.

  1. The band. The dots land in a thin band. Look at the numbers in the random block in the y slot. The paper runs from -280 to 280 up and down. Change the numbers so the dots cover the whole paper.
  2. The color. Every dot is red, because set color has three plain numbers. Plug a random block into each of the three slots, the same way as the color burst, and choose the numbers a color can be.
Check your answers
  1. The y block needs random -280 280. The dots were picked between -20 and 20, which is a thin band.
  2. Each slot of set color gets random 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 random block into set 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.

Wandering turtle: a coin flip decides which way each turn goes. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=wandering-turtle

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:

  1. When the random number is 1, which way does the turtle turn? Which way does it turn in the else?
  2. Can the turtle ever do both in one pass? Can it do neither?
  3. 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.

A full-size sample tab will not remember changes

A sample tab never saves anything, so a refresh puts the sample back. Press Ctrl+S to keep a copy of something you want to keep.

Do these one at a time and run after each:

  1. Change the turns so heads turns right 60 and tails turns left 120. What happens to the path?
  2. Make the color tell you which road was taken. Put a set color inside the top mouth and a different set color inside the else mouth.

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

A red outline on the if block?

The pointed slot needs a yes-or-no block, like =, < or > from the Math palette. A plain number will not fit there. Read the message under the drawing.

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.

Build in a fresh tab

Open TurtleLab in a new tab and build there. Do not build in a sample tab, or in a frame inside a lesson. Neither saves your work, so a refresh would put the sample back and lose your blocks.

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:

  1. First, a repeat that draws one thing, with no random yet.
  2. Then plug in random where you want chance.
  3. Last, add the if and else, 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.

When something is not working

  • A red outline means that block has a problem. Read the message under the drawing.
  • Nothing happens? Check that your blocks are snapped under start.
  • Part of your picture off the edge of the paper? Press the Fit button on the toolbar (four corners round a square). It zooms until all of it is on the paper, and the PDF button does that for you too.
  • Not what you pictured? Change one number at a time and run it again. The Stop button always works, and Ctrl+Z undoes a mistake.

Check yourself

Before you save, check that:

  • Your picture has a repeat that draws part of it.
  • You can see random at work in the picture. Run it twice. Does it change?
  • Your if and else changes 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 box1 is a command, like forward. It has a number slot on its right, and the number in that slot is what goes into the box.
  • box1 is 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.

A slow program that stores numbers in box1 and prints it, one block at a time. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-box-steps

First look at the blocks, and predict before you watch:

  1. The first store in box1 has a 40 in its slot. What is in box1 after it runs?
  2. The next two store in box1 blocks each have a + block in their slot: box1 plus 10. What is in box1 after both have run?
  3. The two print blocks come next. What does each one print? Does printing change what is in the box?
  4. The next store in box1 has a 100 in its slot. What happens to the number that was in the box before?
  5. The last block is forward with the box1 block 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
  1. 40.
    1. 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.
  2. Each prints 60. Printing only looks inside the box. It does not change it.
  3. The 60 is gone. A variable holds one value, so storing a new one replaces the old one. The box now holds 100.
  4. 100, because box1 stands for the number in the box, and that is 100 now. (The Output box shows 60, 60 and 100, from the three print blocks.)

That is everything a variable does:

  • It holds one value.
  • Storing a value replaces whatever was there.
  • Looking at it (using the box1 block) 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.

A house drawn from one variable, box1. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-box-house

Work through these, one at a time:

  1. Find the store in box1 block. What number is in its slot? The yellow strip shows what is in each box.
  2. Count the forward blocks that have the yellow box1 block in their slot. How many are there?
  3. Change the 80 in store in box1 to 120 and press Run. How long is every side of the house now?
  4. Change it to 50 and press Run. How many numbers did you have to change?
Check your answers
  1. 80.
  2. Three: the forward in the walls, the forward that climbs to the roof, and the forward in the roof.
    1. The walls and the roof are all 120 long, because they all ask the same variable.
  3. 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.

Any polygon, with its number of sides in the variable box1. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=polygon

  1. Change the 5 in store in box1 to 3, then 6, then 12. Press Run each time.
  2. Find the two blocks that use box1. What does each one do with it?
  3. Why does one number in one variable make the shape close up every time?
Check your answers
  1. The repeat uses it as the number of sides. The turn uses it inside the ÷ block, as 360 ÷ box1.
  2. 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:

  1. The + block looks inside box1 and adds 1.
  2. store in box1 puts that answer back in box1.

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.

Is that a mistake?

In math, a number can never equal itself plus 1. A program is different. The + block is always worked out first, using the number that is in the box now, and only then does the new number go in.

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.

A box that grows by 10 on every lap and prints itself. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-box-trace

Before you change anything, work out:

  1. The box starts at 0. What does the first lap print? What does the last lap print?
  2. Change the 10 in box1 + 10 to a 5 and press Run. What does the last lap print now?
  3. Change it back to 10, then change the 8 on the repeat to a 12. What does the last lap print?
Check your answers
  1. The first lap prints 10, because the box goes from 0 to 10 before it is printed. The last lap prints 80.
    1. Eight laps of 5 is 40.
    1. 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
  1. What does it print, lap by lap?
  2. What would the last number be if the repeat said 5 instead of 3?
Check your answers
  1. It prints 4, then 8, then 16. The box starts at 2, and each lap doubles it.
    1. The five laps print 4, 8, 16, 32 and 64.

Counting down

A box can count down too. This sample counts with box1 - 1.

Countdown: a box that counts down from 3. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=countdown

  1. What is in the box when the circle starts to draw?
  2. Why does the circle wait until the numbers have finished?
  3. Change the 3 in store in box1 to a 5, but leave the repeat at 3. What does it print? Then change the repeat to 5 as well.
Check your answers
    1. It started at 3 and lost 1 on each of three laps.
  1. The repeat has to finish all of its laps before the blocks below it run.
  2. With only 3 laps it prints 5, 4 and 3, then stops early. With a repeat of 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 box1 that makes the box bigger inside the repeat. 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 petal now 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 petal block gets a place to type a number.
  • A size block appears in My Blocks. Plug it in wherever the number belongs, like the number in arc.

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.

A block called square, used three times with three sizes. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=own-block

  1. Which blocks are under the to block? What do they draw?
  2. What is size the first time square is used? The second time? The third time?
  3. How many lines does the turtle draw in all?
  4. Change one of the three sizes and press Run. What changes in the drawing, and what stays the same?
Check your answers
  1. A repeat of four forward and right blocks, which draws one square. The forward uses the size block for its length.
  2. 40, then 80, then 120. They are the numbers typed into the three square blocks.
    1. Three squares, four sides each.
  3. 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.

A flower drawn by using a block called petal eight times. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-own-flower

  1. How many times does the main program use petal?
  2. Change the 60 in the main program to 30. What changes?
  3. After each petal the turtle turns 45. Why 45?
  4. If you change the repeat from 8 to 6, what turn closes the flower?
  5. Now find the to petal block and change the 120 inside it to 90. Press Run. How many petals changed?
Check your answers
  1. Eight times, because the repeat around it says 8.
  2. All eight petals get smaller, because they all use the same size.
  3. 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.
  4. 60, because 6 times 60 is 360.
  5. 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.

A B drawn with straight lines and a B drawn with arcs, each a block of its own. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-b-two-ways

  1. How many blocks are inside each B? Which one took fewer?
  2. Four blocks come before each B in the main program. Which four, and what do they do?
  3. Change the 0 in the set heading just above the curved B to 90, and press Run. What happens to the B, and why? (Start over puts it back.)
Check your answers
  1. The straight B has 15 blocks and the curved B has 9. An arc draws a whole round bowl in one block.
  2. pen up, set xy, set heading and pen down, the same four you used between letters in Step 1. The pen goes up so nothing is drawn on the way. set xy picks the place. set heading makes the turtle face up. The pen goes down to draw.
  3. 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 heading the 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.

  1. 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.
  2. 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 xy inside a letter block. A letter that always jumped to the same place would land on top of the last one.
  3. In the main program, write the name: for each letter, the four blocks that travel, then the letter.
  4. Give every letter a color of its own. Put a set color block (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 xy inside it.
  • Every letter has a color of its own.
  • You built every letter yourself, with no write block.
  • 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.

An E and a B made of angled lines, with the turtle walking between them. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-b-diagonal

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.

Twelve squares: a repeat of 4 inside a repeat of 12. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-nested-count

Before you change anything, work out:

  1. How many forward blocks run in all? (Think: how many sides does one square have, and how many squares are there?)
  2. After each square the turtle turns 30. Why 30?
  3. You change the outside repeat to 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
    1. The inside loop runs 4 sides, and the outside loop runs it 12 times, so 12 times 4 is 48.
  1. 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.
  2. 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.

A flower: a repeat of 6 inside a repeat of 6. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=hexagon-flower

  1. How many lines does the turtle draw in all?
  2. Change the inside repeat from 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 outside repeat and the outside turn be?
  3. You change both repeat blocks to 20. How many lines is that? Work it out before you try it.
Check your answers
    1. Six hexagons with 6 sides each.
  1. Pentagons. For a flower of five, the outside repeat is 5 and the outside turn is 72, because 5 times 72 is 360.
    1. 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.

A thick bar drawn with the three numbers of set color. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-color-mix

Predict the color of the bar for each set of numbers. Then change the three numbers in set color, press Run, and check:

  1. 0 255 0
  2. 0 0 255
  3. 255 255 0 (two lights at full)
  4. 255 255 255
  5. 128 128 128
  6. 0 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
  1. Green.
  2. Blue.
  3. Yellow. Red light and green light together look yellow.
  4. White, all three lights at full.
  5. Gray. Any color where all three numbers are equal is a gray: a low number is dark gray, a high one is light gray.
  6. 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.

  1. Type 10 into the red number. What binary does the picker show? Does it match your answer above?
  2. Which number gives the binary 0100 0000?
  3. 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?
  4. Eight bits make how many different numbers? Three lights make how many different colors?
Check your answers
  1. 0000 1010.
  2. 64, because only the 64 switch is on.
  3. #FFFF00 is yellow: red FF and green FF, blue 00. #00FF00 is green.
  4. 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.

Squares whose color is made from a variable that grows each lap. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=spinning-squares

  1. The first square is drawn after box1 becomes 7. About what are its three numbers, and which light is strongest?
  2. The last square is drawn when box1 is 252. About what are its three numbers?
  3. Why does 255 - box1 make blue fall exactly as red rises?
Check your answers
  1. Red 7, green 60, blue 248. Blue is the strongest, so the first square looks blue with a little purple.
  2. Red 252, green 60, blue 3. Red is the strongest, so the last square looks red.
  3. 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:

  1. Run it and look at what you got.
  2. Say what you expected. What should it have done?
  3. Find the difference, and the block that could cause it.
  4. 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

A program that is meant to draw a triangle. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-bug-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

A program that is meant to draw a square 100 long using box1. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-bug-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

A program that is meant to wander left and right by a coin flip. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-bug-if

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

A program that is meant to draw a house with a triangle roof. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-bug-pen

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.

Build in a fresh tab

Open TurtleLab in a new tab and build there. The frames in the other steps do not keep your work, so do not build in one of them.

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 flower or petal block with a size input. 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 size input. 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:

  1. First make your block work on its own. Use it once with a plain number, and check the drawing is what you meant.
  2. Then put it inside a repeat, still with a plain number.
  3. Last, put box1 where the plain number was, and add the block that changes box1 every lap. Remember it goes inside the repeat.

If your picture is slow to draw, add a speed block (Flow palette) near the top. Speed 4 is instant.

When something is not working

  • A red outline means that block has a problem. Read the message under the drawing.
  • Watch the yellow strip at the bottom right of the drawing. It shows what is in each box. Is the box changing the way you thought it would?
  • Nothing happens? Check that your blocks are snapped under start, and that you used your block, not only made it.
  • Part of your picture is off the edge of the paper? Press the Fit button on the toolbar (four corners round a square).
  • Not what you pictured? Use the four steps from Step 11: run it, say what you expected, find the difference, then change one thing.

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 repeat that 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.)

A program that asks how many sides, then draws a shape with that many. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-ask-sides

Before you type, work out:

  1. You type 5. How many sides will it draw, and how big is each turn?
  2. What will be in box1 afterwards? Look at the yellow strip.
  3. You type 0. What will it draw, and why?

Now try 5, 3, 12 and 0.

Check your answers
  1. Five sides. Each turn is 360 ÷ 5, which is 72.
    1. The answer goes in box1, and it stays there until something puts a new number in.
  2. Nothing. The repeat runs 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.

  1. What are box1 and box2 when the repeat starts?
  2. What shape does it draw, and how long is each side?
  3. Take out the store in box2 block. What does the program draw now, and why?
Check your answers
  1. box1 is 5 (the second answer replaced the first) and box2 is 50 (stored before the second question).
  2. A pentagon, with sides of 50.
  3. Nothing. Without the store, box2 is still 0, so forward box2 goes 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.

A program that stamps a square wherever you click the paper. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-click-stamp

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:

  1. You click the middle of the paper. What will mouseX and mouseY say?
  2. You click near the top right corner. Will mouseX be positive or negative? And mouseY?
  3. 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
  1. About 0 and 0.
  2. 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.)
  3. 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 start run once, as always.
  • A program that has a when paper clicked then 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.

Build in a fresh tab

Open TurtleLab in a new tab and build there. The frames in the other steps do not keep your work, so do not build in one of them.

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:

  1. First make your block work on its own. Use it once with a plain number, and check the drawing.
  2. Then put it under when paper clicked, with pen up, set xy mouseX mouseY and pen down before it. Press Run, click the paper, and check that it draws where you click.
  3. Last, add the ask block under start, and put box1 where the plain number was.

Press Run, answer the question first, and then click.

When something is not working

  • Nothing happens when you click? Check that you pressed Run, that the message says the program is listening, and that you clicked the paper and not a block.
  • The program asks nothing? Check that the ask block is snapped under start.
  • A red outline means that block has a problem. Read the message under the drawing.
  • Watch the yellow strip at the bottom right of the drawing. Is box1 what you typed?
  • Not what you pictured? Use the four steps from Step 11: run it, say what you expected, find the difference, then change one thing.

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.

A red stripe, and a program that prints 1 when a click is inside it and 0 when it is not. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-click-and

Before you click, work out:

  1. You click at x = 160. Is mouseX > 100 yes or no? Is mouseX < 220 yes or no? What does and say, and what gets printed?
  2. You click at x = 300. Which of the two questions is no? What does and say?
  3. 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
  1. Both are yes, so and says yes, and the program prints 1.
  2. mouseX < 220 is no, because 300 is not less than 220. One no is enough to make and say no, so it prints 0.
  3. mouseX > 100 is 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.

The same red stripe, with a program that prints 1 when a click is outside it. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-click-or

Before you click, work out:

  1. You click at x = 160. Is mouseX < 100 yes or no? Is mouseX > 220 yes or no? What does or say?
  2. You click at x = 300. What does or say now, and why?
  3. A not around the and from the first frame gives the opposite answer. For the click at x = 160, what does and say, and what does not of it say?
Check your answers
  1. Both are no, so or says no, and the program prints 0.
  2. mouseX > 220 is yes. One yes is enough to make or say yes, so it prints 1.
  3. and says yes, so not of 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.

A game: click inside the red square to score, and it jumps to a new place. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-click-game

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 - box2 says 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 - box3 says 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.

  1. You click at x = 80 and y = 10. Is the click in the square?
  2. You click at x = 80 and y = 50. Is the click in the square? Which question says no?
  3. You click at x = 40 and y = 10. Which question says no?
Check your answers
  1. Yes. The click is 30 right of the corner and 40 above it, and both are more than 0 and less than 60.
  2. 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.
  3. The question about mouseX. The click is 10 left of the corner, so mouseX - box2 is -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:

  1. First make target work on its own. Put it under start, press Run, then press Run again. Does the square move each time?
  2. Add when paper clicked, with the first store in box4 and a print box4 under it. Press Run and click. Does the Output box say how far right of the corner you clicked?
  3. Add the outer if with and, and a print 1 inside it. Click in the square’s columns, above and below the square. Does it print 1?
  4. Add the second store and the inner if. Then replace the print with the score and the jump.

Press Run, and click the square.

When something is not working

  • Nothing happens when you click? Check that you pressed Run, that the message says the program is listening, and that you clicked the paper and not a block.
  • A red outline means that block has a problem. If it is an and with an empty hole, plug a yes or no block into every hole.
  • The square jumps but the score does not go up? Look at the yellow strip. Is the store in box1 inside the inner if, and does it add 1 to box1?
  • Clicks near the edge do not count, because the test says more than 0 and less than 60, not exactly 0 or 60. That is fine.
  • Not what you pictured? Use the four steps from Step 11: run it, say what you expected, find the difference, then change one thing.

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 if uses and to 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 and does 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.

Three key blocks: the up arrow walks the turtle and the left and right arrows turn it. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-key-steer

Before you press anything, work out:

  1. You press the up arrow three times. How long is the line?
  2. Then you press the left arrow once. Does the turtle move? What does it do?
  3. Then you press the up arrow twice. Which way does the turtle walk, and how far?

Now try it and check.

Check your answers
    1. Each press walks 20, and 3 × 20 is 60.
  1. It does not move. It turns a quarter turn to the left, on the spot, because the left arrow only has a left 90 block under it.
  2. 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.

An every block that draws a growing spiral by itself. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-every-spiral

Before you look closely, work out:

  1. How many seconds are there between one tick and the next?
  2. 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?
  3. What two changes would make the spiral grow faster?
Check your answers
  1. 0.3 of a second, because the number is 3 and it is in tenths.
  2. 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.
  3. 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.

A game: steer the snake with the arrow keys and eat the red squares. Open full size

Try it in TurtleLab: https://learn.porttack.com/turtlelab/?sample=lesson-snake

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.

  1. The snake is at x = 70 and y = 10. Is it on the square?
  2. The snake is at x = 70 and y = 50. Is it on the square? Which question says no?
  3. The snake is at x = 10 and y = 10. Which question says no?
  4. The snake is at x = 340 and y = 0. Which of the edge questions says yes?
Check your answers
  1. Yes. It is 20 right of the corner and 40 above it, and both are more than -30 and less than 60.
  2. 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.
  3. The question about x. It is 40 left of the corner, so x minus box1 is -40, which is not more than -30.
  4. x > 330. 340 is more than 330, so the or says 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:

  1. Start with only start, set heading 90 and an every 2 with forward 20 under it. Press Run. Does the turtle walk by itself?
  2. Add the four when key pressed blocks. Press Run, click the paper once, and steer.
  3. Make food work on its own: put it under start, press Run, and press Run again. Does the square move each time?
  4. Add the two if blocks with the or for the edges, and make restart.
  5. Add the two store blocks and the two if blocks that eat the square.

Press Run, click the paper, and play.

When something is not working

  • Nothing happens when you press a key? Check that you pressed Run, that the message says the program is listening, and that you clicked the paper once so the page is listening to the keyboard.
  • The snake does not move by itself? An every block does nothing until the last block under start has run. Check for a forever or a wait that never ends.
  • The snake crawls? Put speed 4 under start.
  • A red outline means that block has a problem. An every with an empty box, or a number smaller than 0.5, stops the program and says so.
  • Clicking a when block or an every block only tells you what it is for. Press Run and the keys do the rest.
  • Not what you pictured? Use the four steps from Step 11: run it, say what you expected, find the difference, then change one thing.

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 pressed block, an every block, an and and an or.
  • 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 every does 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.

Build in a fresh tab

Open TurtleLab in a new tab and build there. The frames in the other steps do not keep your work, so do not build in one of them.

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] pressed and every.
  • 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, use clean and 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:

  1. First make your block work on its own. Use it once under start, press Run, and check the drawing.
  2. Add the first thing the person does, a when [key] pressed or a when paper clicked. Press Run, try it, and check that it does what you meant.
  3. Add the every block with one block under it. Press Run and watch it go by itself.
  4. Add the box and the if with and or or. Use a print block to see what a box holds while you play.
  5. 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.

When something is not working

  • A key does nothing? Check that you pressed Run, that the message says the program is listening, and that you clicked the paper once so the page is listening to the keyboard.
  • Nothing moves by itself? An every block starts after the last block under start has run. Check for a forever or a wait that never ends. And a number smaller than 0.5 on every is too fast, so it stops and says so.
  • It runs slowly? Put speed 4 under start.
  • Clicking a when block or an every block only tells you what it is for. Press Run and the keys, clicks and timer do the rest.
  • A red outline means that block has a problem. Read the message under the drawing.
  • Watch the yellow strip at the bottom right of the drawing. Is the box what you thought?
  • You only have four boxes, box1 to box4. Plan which one holds what before you start.
  • Not what you pictured? Use the four steps from Step 11: run it, say what you expected, find the difference, then change one thing.

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 if that uses and or or.
  • 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.