Greenfoot - Turtle Graphics

Turtles?

Turtle graphics are based on the concept that an actor (a turtle) moves slowly by choosing a direction and walking, then changing directions and walking some more.  In this example world, the turtles (and hippo) leave a trail as they move around.

The simple commands to control the turtles are summarized below:

Square

        move(50); 
        turn(90);

 

Spiral
   
move(size);
   turn(90);
   size = size + 4;

Circle

  move(5); // 5 pixels
  turn(2);   // 2 degrees

Star

   move(100);
   turn(150);

Each of these shapes is based on the concept that the turtlle (or hippo) will move, then turn, then move again and turn again.  The spiral is slightly different, because the amount it moves each time (size) is changing.  That means that each line is 4 pixels longer than the previous line.

The StarHippo is particularly interesting.  By turning by a number of degrees over 90, the line segments are no longer just horizontal and vertical, but a variety of angles.

Discussion Questions

  1. The circle turtle is turning 2 degrees each time.  How many times must it turn before it has completed a full circle?
  2. How will the the circle change if the CircleTurtle moves only 3 pixels each time?
  3. What happens to the star if the hippo turns 120 degrees instead of 150?  Make a prediction before running the program to check.
  4. What will happen if the SquareTurtle turns 60 degrees instead of 90?
  5. What will change if the SpiralTurtle starts with size=50 and then changes size by doing size = size - 2 ?
  6. What will happen to the spiral if it turns 60 degrees instead of 90?
  7. What command could make the CircleTurtle turn to the left instead of turning to the right?
  8. What will happen if the SquareTurtle turns 180 degrees instead of 90?

Changing the Program

Get a copy of Greenfoot (hopefully alread installed) and a copy of the TurtleGraphics world here :download the TurtleGraphics world here 

Compile - the simulation will only run after clicking the [Compile All] button.  That will change the striped object names to normal text.  If there are errors in the code for any of the objects (actors), the editor will open and show you where the error is.  After correcting the error, close the editor box and click [Compile All] again - repeat until there are no errors.

Running the Simulation - click the [> Run] button to start the simulation and watch the actors moving around.  After that, click the [Pause] to stop the simulation.

Changing the Actions - double-click on an actor (object) to open the editor.  You will see Java code that looks like this :

This has two methods - a CONSTRUCTOR and an ACTIONS method.

The CONSTRUCTOR has exactly the same name as the object - StarHippo.   It will execute once, at the very beginning when the Hippo is instantiated (created).  This constructor says that the drawing color should be magenta, and that the pen should be down (touching the paper).

The ACTIONS method is named act().  This executes each to the clock "ticks" - over and over again.  It says that the Hippo should move 100 pixels, and then turn 150 degrees.

You can change these commands, but be careful that you do not remove or change the headers (the class command, the StarHippo command and the act command).  Of course your changes must be written using correct Java syntax - e.g. with brackets in the right place and a semicolon at the end of each command.

Java Commands

All Java commands will work in Greenfoot, but they won't necessarily do anything useful with the Turtle objects.  At first, stick to turn and move.  Later you can add other types of commands.

New Objects (actors)

You can add more actors - more turtles.  There are two ways to do this:

  1. Right-click on a class name on the right side and choose new, for example new StarHippo.  Then drag the new object into the display frame and drop it somewhere.  This will work, and the object will start moving when you click [> Run], but this object will disappear whenever you click [Reset].
     

     
  2. The more permanent technique is to open [TurtleWorld] in the editor and add another command - for example  addObject(new StarHippo, 300, 200);
    This will place a new StarHippo at location 300,200.  When you click [Reset], this will keep the hippo and move it back to this starting position.


     

New Types (classes)

You can create a new type of actor, just like the 4 that already exist.   For example, create a Snake.   Right-click on the Turtle class and select New subclass.  Name the new class Snake, and select a corresponding icon for the picture.

  

Now this new Snake actor won't actually do anything until you type commands into it's act() method.  Double click on the class to open the editor, then add some commands.  For example:

You will need to add the Constructor by typing all the code.  The act() method will already be there, but you also need to add the degrees variable declaration.

** Can you guess how this snake will move? **

Of course, you will still need to create an actual snake, using either the temporary or permanent techniques outlined above.l

Practice

Get a copy of Greenfoot and a copy of the TurtleGraphics world.  Then work through the following experiments:

  1. Make all the changes suggested in the Discussion Questions above.  Check that you can make the changes successfully and that the results match your predictions.
  2. Create the new Snake actor as outlined above.  Check how the snake moves.
  3. Make SmallSquareTurtle that creates a smaller square than the original.  Place 4 of these SmallSquareTurtles in the correct place so that they draw 4 squares that fit exactly inside the large square.
  4. Create a SmallCircleTurtle that draws smaller circles than the original.  Place 3 SmallCircleTurtles inside the the normal Circle so that it exactly fits 3 small circles into the original larger circle.
  5. Change the SquareTurtle so that it draws a stop-sign (octagon) instead of a square.
  6. Create a triangular spiral instead of a square spiral.
  7. Try running 2 spirals that overlap each other to make a dizzying pattern.
  8. Make a circle turtle that starts out moving in large steps, but these steps get smaller and smaller.  It must start out with something like 100 pixels.  The result should be that it "spirals inward".  What happens after it reaches the center?
  9. Make a hexagon turtle.  Use lots of these fitting exactly together to tile the screen in a honeycomb pattern, like this:

  10. Make up some of your own experiments.