Project 1
Coding the Art Toy introduces you to MicroWorlds EX primitives, which are built-in commands including setting background and turtle (object) colors; putting the turtle pen up and pen down; setting the pen size; moving the turtle forward and backward; and turning the turtle left and right. It also introduces the home and clean primitives.
In this project, you’ll code your first procedures — new commands you create by putting together primitives in new ways, many using the repeat command. As you build Art Toy, you’ll craft a user interface by making a text box title and buttons, as well as tinker with setting your buttons to execute once versus forever. The fun is in creating your own toy, and then playing with it. Now you’re coding!
All projects can be adapted to appear in new ways to the user or player. Many times, by using different graphics with the same computer code, you can create projects or games that look very different.
You can create a few different looks for Art Toy, but mostly, this project gives the end user, the person who will be playing with your art toy, a chance to make things look differently because that person uses your buttons to make their own art.
In future projects, you will have many chances to brainstorm cool new ways to change the look of your user interface. For now, get coding by following these simple steps.
Begin creating your Art Toy by starting a new project as follows:
A new project opens.
This is the next-to-the-largest project size available and provides plenty of space for creating your Art Toy. Other project size options consist of MicroWorlds Small, MicroWorlds Standard, Web Player, and Full Screen 800 x 600.
You must set the project size at the outset of a new project. You cannot resize projects later.
The region in which your toy appears is the workspace. Add a title to the workspace as follows:
The cursor becomes a white pen.
A text box called text1 appears in the workspace.
The text can now be formatted. Note that the Windows menu options for formatting text may vary slightly from Mac menu options described here.
Options consist of Plain, Bold, Italic, and Underline.
A Colors dialog box appears, as shown in Figure 1-2.
Making the text box transparent makes the text inside the box visible and the box itself invisible.
You can edit text only when the text box is opaque (not transparent). To change a text box from transparent to opaque, right-click (Windows) or Ctrl-click (Mac) the text and select Opaque from the pop-up menu.
All programming languages have ways to create objects. Objects are usually the physical things that move around the computer screen — cars, birds, paintballs, and so on. Programming languages have different name for objects. In another programming language you may have heard of, GameSalad, an object is called an actor. In MicroWorlds EX, an object is called a turtle.
In your Art Toy, your turtle will do the drawing! He is an object that can move around the workspace in many ways. Also, the turtle carries a pen that is capable of drawing in a variety of colors and pen sizes.
Create a turtle for your Art Toy as follows:
A turtle hatches in your workspace.
You can drag the turtle and move it anywhere in the workspace. You can also click its snout and turn the turtle around in a circular motion to point in any direction.
There are two types of commands in MicroWorlds EX:
You can type any command, primitive, or procedure, at any time into the Command Center for the purpose of testing out the command. The Command Center is located just below the workspace, at the bottom of the MicroWorlds EX window (see Figure 1-3).
Try out a few of these primitives at the Command Center:
Now try setbg 15. The background is still red! That’s because MicroWorlds uses both names and numbers to represent colors. The color name red is the same as the color number 15.
Try a few more color names such as yellow, green, and violet. Also test a few more color numbers using any number from 0 to 139. You can type a new line of code each time, or replace the color you typed previously and then press Enter/Return.
The opposite of moving forward is moving backward. Type bk 10 and then press Enter (Windows) or Return (Mac). This command translates to move backward 10 steps. Type some other distances and observe the change in the turtle’s position.
The MicroWorlds EX workspace is similar to a flat map of Earth. If a turtle walks off of one side of the workspace, it reappears on the opposite side of the workspace.
Try the forward and backward commands to see the turtle draw as it moves. The command pu stands for pen up, but don’t give the pen up command now; just leave the pen down.
Turtle turns are always made from the viewpoint of the turtle. If the turtle is facing north and you give the command rt 90, the turtle faces east. If you give the command rt 90 again, the turtle faces south. Give the command rt 90 a third time and the turtle now faces west.
The Command Center is a good place to test out commands as you develop a program. But users of your program don’t want to type in commands! Users want a simple way to operate your program.
You operate your television, phone, and computer through a graphical user interface (also called a GUI), which has buttons, pictures, menus, and other tools for communicating with each device. MicroWorlds EX provides ways for you to build your own GUI to make it easy for people to use your computer program.
Instead of asking a user to type in a command such as setbg "red to change the workspace of the Art Toy to the color red, you can make a button to do the work instead. To create a button, you provide two pieces of information: a label for the user, and an instruction for the computer.
Make a button to change the background color of the workspace as follows:
The completed Red Background button (shown on the left in Figure 1-6) is added to the workspace.
Be creative by using any colors, distances, and turn angles you want! On some of the buttons, try setting the Do It radio button to Forever so that the button command executes over and over (until you click the button again to halt the execution, or click the Stop All button on the toolbar). Executing the button instructions forever will produce interesting effects for forward, backward, turn, and random instructions.
Your Art Toy now looks something like Figure 1-7.
A procedure is a new command you create from primitives and other procedures. Procedures are used as shortcuts to describe a long list of steps. For example, if you want the turtle to draw a square that is 30 steps on a side (with the pen already down), you could type the following into the Command Center; press Enter/Return after each line:
fd 30 rt 90
fd 30 rt 90
fd 30 rt 90
fd 30 rt 90
But if you want to draw many squares, it would be faster to give this collection of steps a new name: square.
In the following sections, you write a square procedure and then use that procedure to write a procedure called spirosquare. You will also create two buttons.
Write a procedure to create the new command as follows:
to square
fd 30 rt 90
fd 30 rt 90
fd 30 rt 90
fd 30 rt 90
end
A new procedure starts with to followed by the name of the procedure, in this case, square. Do not use spaces in the name of the procedure. Always end the code for your new procedure with end. The end command must be on its own line, so be sure to press Return/Enter after typing it.
Procedure names may not have spaces. If you want to separate words in a procedure name, use the underscore symbol: wiggle_around. You can also use the dash symbol, but this is not recommended because the dash can be confused with a minus sign.
A common mistake among new programmers is leaving off the word to at the start of your procedure definition. If this occurs, the Command Center issues an error message like the one shown in Figure 1-9. If you see an error message, simply edit your procedure and test it again until it executes as expected.
to square
repeat 4 [fd 30 rt 90]
end
Notice that the primitive repeat is followed by a number showing how many repetitions to perform, and then the repeated phrase is placed inside square brackets: [fd 30 rt 90].
Use the clean command (or button, if you made one!) to erase drawings from the workspace if it becomes too crowded.
Now that you have written a procedure to create a square command, you can use it anywhere in the current project — you can even use it in another procedure! When one procedure is used inside another procedure, we say it is nested. Try writing the following spirosquare procedure:
to spirosquare
repeat 8 [square rt 45]
end
Figure 1-13 shows a time-lapse picture of spirosquare as it executes. Note that a new background color and turtle color are used for this drawing.
Here’s how the spirosquare procedure works: The to spirosquare command names the new procedure. Next, the turtle repeats the instructions inside the brackets a total of eight times (repeat 8). The instructions inside the brackets draw a square and then turn right 45 degrees (rt 45). The end primitive indicates the end of the procedure.
Note that it is okay to use a space in the label of a button, just not in the instruction.
The Processes pane appears, as shown in Figure 1-14. The top of the Processes pane shows three execution speeds: green (full speed), yellow (slow speed), and orange (very slow speed).
The Processes pane reports the execution of all parts of MicroWorlds EX as they happen.
You use this basic code to write the procedures:
to procedure
repeat numbersides [fd sidelength rt turnangle]
end
Replace procedure with the name of the new procedure. The repeat number should match the number of sides on the polygon. The fd command tells the length of a side. The rt command tells how much the turtle has to turn to draw the next side.
Here’s how to create an octagon:
to octagon
repeat 8 [fd 60 rt 45]
end
And here’s one way to create a spiro octagon:
to spirooctagon
repeat 12 [octagon rt 30]
end
Another shape to try drawing is a circle. A circle is not a polygon because it has no flat sides. But the turtle can draw something that looks like a circle by making 360 sides and turning one degree between each side. Following is code for a circle:
to circle
repeat 360 [fd 2 rt 1]
end
And here’s one way to create a spiro circle:
to spirocircle
repeat 20 [circle rt 18]
end
When arranging your buttons in any project, think carefully about the GUI. The user or player will expect buttons and other parts of the interface to be organized in an easy-to-understand layout. For the Art Toy, you may want to position all the buttons for setting colors and pen sizes on the left side of the workspace, and all of the buttons for drawing on the right side.
Choose File⇒Save Project from the menu bar to save your game. Your finished game should look similar to Figure 1-15.
Test each button in your toy to make sure it functions as you intend. If it doesn’t, look for error messages in the Command Center — they provide clues about how to correct your code. If you find a bug, meaning a problem with your code, look at each line of code carefully and check for misspelled primitives and missing quotes. Also check that new procedures you have written begin with to and end with end.
Once finished, play with the Art Toy and ask friends to play with it, too!