Learn Python by drawing (turtle graphics)

What is turtle graphics?

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.

environment

Touch turtle graphics (interactive shell)

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.

image.png

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)

image.png

In this state, enter turtle.forward (100) again to proceed down.

>>> turtle.forward(100)

image.png

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')

image.png

You can also change the color of this turtle with color ().

>>> turtle.color('red')

image.png

If you want to hide this turtle, use hideturtle ().

>>> turtle.hideturtle()

image.png

Type show turtle () if you want the hidden turtle to appear again.

>>> turtle.showturtle()

image.png

You can draw a circle as well as a straight line.

>>> turtle.circle(100)

image.png

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)

image.png

You can also go backwards and use backward ().

>>> turtle.backward(200)

image.png

Use home () if you want to return to the very first position.

>>> turtle.home()

image.png

Use clear () if you want to clear everything.

>>> turtle.clear()

image.png

Type done () at the end to exit.

>>> turtle.done()

Create a file and run it

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()

image.png

You can see that it is displayed firmly.

Rectangle

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.

image.png

star shape

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()

image.png

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.

image.png

triangle

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.

image.png

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.

image.png

end

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)

image.png

Recommended Posts

Learn Python by drawing (turtle graphics)
[Python] Chapter 03-02 turtle graphics (turtle behavior)
[Python] Chapter 03-01 turtle graphics (creating a turtle)
Reintroduction to Python Decorators ~ Learn Decorators by Type ~
[Python] Drawing a swirl pattern with turtle
[python] Streamline drawing
Learn python gestures
Primality test by Python
Visualization memo by Python
Communication processing by Python
Graph drawing in python
Beamformer response by python
Drawing with Python Tinker
Learn Python asynchronous processing / coroutines by comparing with Node.js
Speech recognition by Python MFCC
Drawing candle charts in python
Newcomer training program by Python
Parameter setting by python configparser
Pin python managed by conda
Learn cumulative sum in Python
Paiza Python Primer 1 Learn Programming
Keyword extraction by MeCab (python)
Separate numbers by 3 digits (python)
Markov switching model by Python
Image processing by python (Pillow)
Python started by C programmers
Learn exploration in Python # 1 Full exploration
Platform (OS) determination by Python
Sort by date in python
Straight line drawing by matrix-Inventor's original research of Python image processing-