deutsch     english    français     Print

 

2.1 MOVING THE TURTLE

 

 

INTRODUCTION

 

Programming means giving a machine commands in order to control it. The first such machine that you will control is a small turtle on the screen, which we simply call turtle. What can this turtle do and what do you have to know in order to control it?

Turtle commands are written in English and are always followed by a pair of parentheses, which may contain further details about the respective command. Even if no further information is
required, an empty pair of parentheses must follow. The case of the letters (either upper case or lower case) must always stay consistent.

The turtle can move within its window and draw a trail, but before it can get going, you must first instruct the computer to create such a turtle. You can do this with the command
makeTurtle(). In order to then move the turtle, you can use three commands: forward(distance), left(angle), and right(angle).

PROGRAMMING CONCEPTS: Edit source, run program, sequence

 

 

YOUR FIRST PROGRAM

 

This is how your first program with the turtle looks. Click on Mark program code, copy it and paste it into the TigerJython-Editor. Execute it by clicking on the green start button. The turtle will draw a right triangle. The turtle commands are all stored in a file (a so-called module) named gturtle.

With the import command you tell the computer that it should make certain commands in a module available. The command makeTurtle() creates a window with a turtle that you can control. The following lines of the code consist of commands (also called statements) for the turtle itself.


 
from gturtle import *

makeTurtle()

forward(141)
left(135)
forward(100)
left(90)
forward(100)
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

At the beginning of each turtle program you must first load the turtle module, and then create a new turtle:

from gturtle import *
makeTurtle()

Afterwards, you can give any amount of commands to the turtle. The three commands that the turtle surely understands are:

 
 

forward(s)
left(w)
right(w)

Move forward by distance s (in pixels).
Rotate left by angle w (in degrees).
Rotate right by angle w (in degrees).
 

 

 

YOUR OWN TURTLE IMAGE

 

You can also specify your own file while calling makeTurtle(), which is then used as the turtle picture. This way, you can give the program your own personal touch. Here you can use the file beetle.gif from the directory sprites of the TigerJython distribution. Please note that you must put the file name in quotation marks.

With the following code, the turtle will draw a cross with filled circles at the end points.


 

from gturtle import *

makeTurtle("sprites/beetle.gif")

forward(100)
dot(20)
back(100)
right(90)

forward(100)
dot(20)
back(100)
right(90)

forward(100)
dot(20)
back(100)
right(90)

forward(100)
dot(20)
back(100)
right(90)
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

If you want to use a different image for the turtle, as seen in the above example, you must first create an icon with an image editor. Normally, turtle images have a size of 32x32 pixels and a transparent background and are typically in GIF or PNG format. The image file should be stored in the subfolder sprites of the same directory in which your program is located.

In the above program you are using the new command back(), with which the turtle moves backwards, as well as dot(), with which the turtle draws a filled circle, the radius of which you can specify (in pixels).

 

 

EXERCISES


 
1.
Draw two nested squares with the turtle.
 


2.

Using the command dot(), try to draw the following figure:

 


3.
The House of Saint Nick is a drawing game for kids. The goal is to draw the house using exactly 8 lines, without passing through the same route twice. Draw the House of Saint Nick using the turtle.  


4*.

Create your own turtle icon with an image editor and draw the adjacent picture with it. The side length of the squares is 100. It does not matter where the turtle begins or ends.