Basic Procedures

For the following two problems create a procedure called set-up that draws the required pattern when called. (You can also provide a button that automatically calls the set-up procedure.)

Spiraling Turtles

Here's a snap shot of two turtles. The green one is executing the spiral-out procedure. The pink one is executing the spiral-in procedure.

 

These procedures repeatedly move the turtle forward one step, turn the turtle right by some angle, then either increment or decrement the angle.

Flowering Turtles

Here's a snapshot of 10 turtles after each one has drawn a flower:

The flower procedure takes as input the number of petals as input, then calls the polygon procedure that many times:

to flower [petals]
   ???
end

To initialize the turtles, call the create-ordered (cro) procedure. This creates turtles with their headings evenly spread out. For example, the command:

cro 10 [fd 5]

Creates 10 outward-facing turtles in a perfect circle:

Now ask each turtle to draw a flower:

Racing

Here's a screen shot of a simple racing game:

Here's the 3D view (got by clicking on the 3D buttom in the upper left corner of the world view.)

When clicked, the init-cars button calls the init-cars procedure:

to init-cars

end

This procedure does the following:

1. clears everything

2. draws a white finish line

3. creates 10 turtles

4. sets the shape of each turtle to "car" and the label to who

5. places the turtles evenly across the bottom of the screen and heading north

Hints:

1. The pre-defined constants max-pxcor, min-pxcor, max-pycor, min-pycor should always be used to determine the coordinates of the corners of the world. Doing so means your program will always work, even if the user changes the size of the world (which is easily done by clicking on the "settings" button.)

Clearly each car (turtle) should have its ycor set to min-pycor:

set xcor min-pycor

But what about its xcor? This will be different for each turtle. Also, the problem requires that the turtles be evenly spaced. If the width of the world was W, then the spacing between 10 turtles would be W/10, with turtle 0 placed at the lower left corner of the world.

2. Each turtle has a unique ID number, starting with 0. A turtle can access this number through its attribute named "who". So for example, the xcor of a turtle should be

who * (W / 10) – min-pxcor

3. The crt command can have two inputs: the number of turtles to create as well as a block of commands each turtle should execute when it is born:

crt 10 [ commands to initialize a turtle go here ]

When clicked, the race-cars button repeatedly calls the race-cars procedure. This procedure picks out a random turtle and moves it forward a 0 – 3 steps.

Hint:

To generate a random number less than 10 try this:

random 10