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);
|
Spiral |
Circle move(5); // 5 pixels |
Star move(100); |
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.
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.
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.
You can add more actors - more turtles. There are two ways to do this:
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
Get a copy of Greenfoot and a copy of the TurtleGraphics world. Then work through the following experiments: