Add More Turtles!
Goals:
- Work with sprites, computer graphics that may be moved and otherwise manipulated as individual entities.
- Begin to develop an understanding of why the order of statements in our code does not always agree with the order that things actually happen when we run that code.
Coding Snippets:
- The
Turtle
constructor's color parameter is optional. The default color is green. Usewear
to change an existing turtle's color. - Sprites passed to
sync
must finish all instructions preceding that call tosync
before any of them can move on to instructions appearing aftersync
. cs
(clear screen) will delete all graphics as well as all sprites other than the default.cg
(clear graphics) clears only what is drawn on the canvas.
Math and Computer Concepts:
- Use a special function called a constructor to create additional sprites. Constructors have names that begin with capital letters, and are invoked with the
new
operator. In this lesson we will use theTurtle
constructor to make additional turtles. - Additional sprites need to be named, such as
r
in the coding snippet above.turtle
is the name for the green, default sprite.
- Use dot notation to refer to a specific sprite. For example,
t.fd 50
moves turtlet
forward ands.rt 90
makes turtles
turn right.turtle.fd 20
works the same asfd 20
.
Activities:
Save all programs for this lesson in a folder called Turtles.
- Copy the code above to a program SyncTest, and run it. Then insert the statement
sync r,b
before the statementb.rt 360,100
. You should get the following, illustrating the effect ofsync:
- RandomTurtles: make a program with several differently-colored turtles that move about randomly (use iteration, and use the
random
function to determine how far each turtle moves and/or turns). Have your turtles draw lines or dots along the way. - TeamTurtles: write a program with multiple turtles that work in unison to make a creative, symmetric design. The turtles should be synced so they move in unison.
- TurtleRace: set up a race between two or more turtles. Use a loop and the random function to control their forward movement, e.g.,
turtle.fd random(10)
.