HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
multipleTurtle
Deutsch   English   

17. SEVERAL TURTLE OBJECTS

 

 

YOU LEARN HERE...

 

that you can create several Turtles in one programme and gain a first insight into object-oriented programming.

 

 

EXAMPLES

  Example 1: Creating multiple Turtles
In the previous examples, you used a global Turtle that does not need a name and can directly use all commands from the gturtle library. If you want to use several Turtles, you must give each Turtle a name.
With the instruction john = Turtle() you create a Turtle with the name john. With the instruction lia = Turtle() you create a Turtle with the name lia. You can control the named turtles with the commands you know, but you must always say which of the turtles you mean. To do this, prefix the command with the turtle name separated by a dot, e.g.. john.forward(50).  

Program:

from gturtle import *

john = Turtle()
john.forward(50)
john.right(90)
john.forward(50)
lia = Turtle()
lia.setColor("red")
lia.setPenColor("red")
lia.back(50)
lia.left(90)
lia.forward(50)
► Copy to clipboard

 

Example 2: Two Turtles draw stars
You can also have the Turtles draw alternately. Here john and lia each draw a star figure.
 

Program:

from gturtle import *

john = Turtle()
lia = Turtle()
john.left(90)
john.setPenColor("green")
lia.right(90)
lia.setPenColor("red")

repeat 9:
    john.forward(150)
    lia.forward(150)
    john.left(160)
    lia.right(160)
► Copy to clipboard

 

Example 3: Turtle name as parameter
Three Turtles are to draw towers consisting of 5 small squares. To do this, you define a function square(t) with the parameter t. When square(joe) is called, the Turtle joe draws a square, analogue square(luka) and square(mia).
 

Program:

from gturtle import *

def square(t):
    repeat 4:
        t.forward(40)
        t.right(90)
    t.forward(40)    

joe = Turtle()
luka = Turtle()
mia = Turtle()
joe.setPenColor("red")
luka.setPenColor("green")
mia.setPenColor("blue")
joe.setPos(-100, -100)
luka.setPos(0,-100)
mia.setPos(100, -100)

repeat 5:
    square(joe)
    square(luka)
    square(mia)
► Copy to clipboard

 

Example 4: Creating turtles with a mouse click
Each time the mouse is clicked, a turtle is created at the current mouse position and draws a star. With the next mouse click, however, you have to wait until the star has been drawn.
To draw a star, use the star(t). function as in example 3. Each turtle is created with the callback function onMousePressed(), which is called when the mouse is clicked.

 

Program:

from gturtle import *

def onMousePressed(x, y):
    t = Turtle()
    t.setPos(x, y)
    star(t)

def star(t):
    t.setFillColor("red")
    t.startPath()
    repeat 4:
        t.forward(50)
        t.left(144)
    t.fillPath()

makeTurtle(mousePressed = onMousePressed)
hideTurtle()
► Copy to clipboard

 

 

REMEMBER...

 

With the instructions joe = Turtle(), luka = Turtle() etc. you can create several Turtles and have them drawn in the same graphics window. You use commands from the gturtle library, but each command must be preceded by the turtle name separated by a dot. This way of calling commands is characteristic of object-orientated programming (OOP). This programming technique is used in some professional programming languages such as Java or C++.

 

 

TO SOLVE BY YOURSELF

 
1.

Two Turtles alternately draw lines of a spiral. The first Turtle draws red and turns 90° to the right after each line drawn, the second Turtle draws green and turns 90° to the left after each line drawn.


 

2.

A red and a black turtle alternately draw squares. The red one starts at the bottom, the black one on the right.

 

 

3.

Two Turtles alternately draw filled squares.

 

 

 

 

 



4.

Turtles are created by clicking the mouse. Each turtle then moves forwards a short distance and draws a filled circle (dot).