PencilCoder

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:

  • coding snippets
  • The Turtle constructor's color parameter is optional. The default color is green.
  • Sprites passed to sync must finish all instructions preceding the call to sync before any of them can move on to instructions appearing after sync.
  • 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 the Turtle 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 turtle t forward and s.rt 90 makes turtle s turn right. turtle.fd 20 works the same as fd 20.
  • Coding ExampleEach Sprite has its own animation queue, or list of instructions to follow. Pencil Code processes all animation queues at the same time, so the code at the right will create two turtles that begin moving simultaneously.

Activities:

Save all programs for this lesson in a folder called Turtles.

  • RandomTurtles: make a program with several differently-colored turtles that move about randomly. Have the turtles draw lines, dots, or other patterns along the way.
  • Copy the code above to a program SyncTest, and run it. Then insert the statement sync r,b before the statement b.rt 360,100. You should get the following, illustrating the effect of sync:
  • 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).