Based on the educational programming language "LOGO", the turtle on the screen is operated by programming and drawn as a figure. Developed in the 1960s, it is sometimes used for children's programming learning because the results of the program can be visually confirmed immediately.
First, let's take a quick look at turtle graphics using an interactive shell.
Open the PyCharm terminal screen and type Python
.
$ Python
Now that you have an interactive shell, import turtle
.
>>> import turtle
Then type turtle.forward (100)
.
>>> turtle.forward(100)
Then the graphic will start up as shown below.
This means that the arrow has advanced 100 times forward.
If you want the arrow to point down, bend it 90 degrees to the right as turtle.right (90)
.
>>> turtle.right(90)
In this state, enter turtle.forward (100)
again to proceed down.
>>> turtle.forward(100)
If you feel that the shape of the leading arrow is dull, you can change it with shape. ()
.
This time I would like to change it to a turtle.
>>> turtle.shape('turtle')
You can also change the color of this turtle with color ()
.
>>> turtle.color('red')
If you want to hide this turtle, use hideturtle ()
.
>>> turtle.hideturtle()
Type show turtle ()
if you want the hidden turtle to appear again.
>>> turtle.showturtle()
You can draw a circle as well as a straight line.
>>> turtle.circle(100)
Now it is drawn with a red line, but if you want to change the color of this line
Use pencolor ()
.
At the same time, let's change the angle a little and proceed.
>>> turtle.pencolor('blue')
>>> turtle.right(45)
>>> turtle.forward(100)
You can also go backwards and use backward ()
.
>>> turtle.backward(200)
Use home ()
if you want to return to the very first position.
>>> turtle.home()
Use clear ()
if you want to clear everything.
>>> turtle.clear()
Type done ()
at the end to exit.
>>> turtle.done()
In the above, it was executed in an interactive shell from the terminal, but from here Create a Python file with a suitable name and run it from the file.
Let's write and execute the following simple process on the file.
import turtle
turtle.forward(100)
Then you can see that it is displayed only for a moment and then disappears immediately.
To keep it displayed, put done ()
on the last line to indicate that all processing is completed.
I will describe it.
import turtle
turtle.forward(100)
turtle.done()
You can see that it is displayed firmly.
Let's output a rectangle using Python's for loop.
The idea is to repeat turtle.forward (100)
forward four times,
Once the advance is complete, a process of turning 90 degrees is added.
import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
for _ in range(4):
turtle.forward(100)
turtle.right(90)
turtle.end_fill()
turtle.done()
In the file format, the content of the process is turtle.begin_fill ()
and turtle.end_fill ()
I'll put it together.
When you execute it, you will see a rectangle with a red line and a yellow inside.
Although it is a star-shaped program, it has almost the same composition as a rectangular program.
The changes are that the number of forward movements is 5 and the angle is 360/5 * 2
.
import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
for _ in range(5):
turtle.forward(100)
turtle.right(360 / 5 * 2)
turtle.end_fill()
turtle.done()
Create a star three times, and gradually increase the distance to move forward each time.
import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
for i in range(5 * 3):
turtle.forward(100 + i * 10)
turtle.right(360 / 5 * 2)
turtle.end_fill()
turtle.done()
Then, a beautiful figure is created as shown below. If you give various loop processing in this way, it will be in a different shape and you can enjoy it.
Similarly, the triangle moves forward three times, and after moving forward once, the angle is changed.
import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
for i in range(3):
turtle.forward(100)
turtle.left(360 / 3)
turtle.end_fill()
turtle.done()
When you run it, you will see a triangle.
Let's see what happens if we shift the angle a little after moving forward once, as we did with the star shape.
import turtle
turtle.begin_fill()
for i in range(200):
turtle.forward(200)
turtle.left(360 / 3 + 10)
turtle.end_fill()
turtle.done()
In this way, the figure will be as beautiful as the star shape.
It was a rough sketch, but the above is the explanation of how to handle it.
By using this turtle graphics, you can program yourself Since it can be easily visualized, I think that not only children but also adults can enjoy learning programming.
Finally, you can also create a tree like the one below.
import turtle as t
foo = t.Turtle()
foo.left(90)
foo.speed(10)
def draw(l):
if(l<10):
return
else:
foo.forward(l)
foo.left(30)
draw(3*l/4)
foo.right(60)
draw(3*l/4)
foo.left(30)
foo.backward(l)
draw(100)
Recommended Posts