Chase Other Sprites!
Goals:
- Use sprites as arguments in functions such as
jumpto
orturnto
.
Coding Snippets:
- The
turnto
,moveto
, andjumpto
functions can accept sprites as arguments. - When passing a sprite to
jumpto
ormoveto
, use a second argument to specify a maximum distance to move. - Turn off an unwanted warning by setting the built-in
nowarn
variable totrue
.
Math and Computer Concepts:
- The statement
b.jumpto r
is similar tob.jumpto r.getxy()
, but there is an important difference: the latter must be preceded by a call toawait done defer()
. That isn't necessary withb.jumpto r
because the definitions forjumpto
,moveto
, andturnto
include code which takes care of the animation queue issues for us when these functions are called with sprites as arguments. - Pencil Code is set up to issue a warning when running programs that could potentially have animation queue problems. This is not a bug, but a failsafe designed to encourage good coding behavior.
- Computer software is often configurable, meaning it has default settings which can be changed to make it behave in a way more to our liking. The coding snippet illustrates modifying a default Pencil Code warning feature by updating the
nowarn
setting.
Activities:
Save each of the following in a folder called ChaseSprites.
- FollowTheLeader: move one turtle around the screen using
fd
,rt
, andlt
with random inputs. Instruct one or more other turtles to follow a few steps behind, usingturnto
,moveto
, and/orjumpto
. - Drawing a polygon centered at a specific point can involve some complex math. Thankfully, we can sidestep those difficulties using the functions from this lesson. Create a single CenteredPolygon with
n
sides using two turtles that start in the same position. The first turtle traces out a circle in increments of360/n
degrees. Every time that turtle stops, the other turtle draws a line segment to it. When the turtles have completed the loop, you've got a polygon with the same center as the circle! - Code a pair of WatchfulEyes that follow the turtle as it moves randomly around the screen. Use a separate sprite for each eye.
TeamStringArt: Use three sprites to create string art. The trick is to have two turtles map out the start and end points of each string, with the third turtle drawing the string by using
moveto
orslide
. Once you've gotten the basic code down, you can create some fantastic shapes.