This is full working source code for turtle t and z. Run this source and than we will explain:
import turtle
t = turtle.Turtle()
t.color("red")
t.begin_fill()
t.goto( 300, 300)
t.goto(-300, 300)
t.goto(-300, -300)
t.goto( 300, -300)
t.goto( 300, 300)
t.end_fill()
t.hideturtle()
z = turtle.Turtle()
z.color("yellow")
z.begin_fill()
z.goto( 200, 200)
z.goto(-200, 200)
z.goto(-200, -200)
z.goto( 200, -200)
z.goto( 200, 200)
z.end_fill()
z.hideturtle()
Creating new turtle is extremely easy, but of course, first we must import turtle module to have access to all methods.
In this simple example we will create 2 turtles, t and z. You can experiment with only one turtle t, if it's easier for you, like this:
import turtle
t = turtle.Turtle()
Then we will set color to be red using color() method:
t.color("red")
We must fill shape with that color when turtle object is done with movement. To do that begin_fill() method is used:
t.begin_fill()
In this tutorial it's all about coordinate system. Numbers you see passed to goto() functions are x and y. A turtle must travel to reach those coordinates from center of coordinate system, which is x = 0 and y = 0.
By telling t.goto(300, 300) turtle will reach upper right position and stop there waiting for next goto() command:
t.goto( 300, 300)
You can experiment a little bit, but to create red box we see in a picture, these steps are needed:
t.goto(-300, 300)
t.goto(-300, -300)
t.goto( 300, -300)
t.goto( 300, 300)
After we are done with movement, it's time to use end_fill() method, that will "finish" painting:
t.end_fill()
And if there's need to hide turtle object t, we can use hideturtle() method:
t.hideturtle()
To create z turtle (object) we will use these lines:
z = turtle.Turtle()
z.color("yellow")
z.begin_fill()
z.goto( 200, 200)
z.goto(-200, 200)
z.goto(-200, -200)
z.goto( 200, -200)
z.goto( 200, 200)
z.end_fill()
z.hideturtle()
Yellow shape created with z movement is, of course, smaller because coordinates are smaller too.
Please note, important thing here is that we are dealing with turtle coordinates, and not with angles and paths where a turtle must be pushed.
That will be a topic for next tutorial.
No comments:
Post a Comment