Home  >  Article  >  Backend Development  >  Using advanced turtle plotting methods in Python

Using advanced turtle plotting methods in Python

高洛峰
高洛峰Original
2017-03-15 15:12:027461browse

In Python, Turtle can not only draw simple black lines, but can also use it to draw more complex geometric shapes, use different colors, and even color the shapes.

1. Start with a basic square

Introduce the turtle module and create a Pen object:

>>> import turtle
>>> t = turtle.Pen()

The code we used to create the square is as follows:

>>> t.forward(50)
>>> t.left(90)
>>> t.forward(50)
>>> t.left(90)
>>> t.forward(50)
>>> t.left(90)
>>> t,forward(50)

This code is too long, we can use for Loop for optimization:

>>> t.reset()>>> for x in range(1,5):
    t.forward(50)
    t.left(90)

The effect is as follows:

Using advanced turtle plotting methods in Python

2. Draw stars

We only need to make some changes to the for loop. The code is as follows:

>>> t.reset()
>>> for x in range(1,9):    ##循环八次
    t.forward(100)      ##前进100像素
    t.left(225)         ##向左旋转225度

The effect is as follows:

Using advanced turtle plotting methods in Python

However, we can further improve it, such as rotating 175 degrees each time and looping 37 times. The code is as follows:

>>> t.reset()
>>> for x in range(1,38):
    t.forward(100)
    t.left(175)

The effect is as follows:

Using advanced turtle plotting methods in Python

We can also draw a spiral star, the code is as follows:

>>> t.reset()
>>> for x in range(1,20):
    t.forward(100)
    t.left(95)

The effect is as follows:

Using advanced turtle plotting methods in Python

Now let us use if# The ## statement controls the turtle's direction to draw different stars. Have the turtle turn one angle first, and then a different angle the next time.

Here, we first create a loop that runs 18 times (range(1,19)), and then let the turtle move forward 100 pixels (t.forward(100)). Next is the if statement (ifx%2 == 0), which means: whether the remainder of x divided by 2 is equal to 0. If the number in x is an even number, we let the turtle turn left 175 degrees (t.left(175 )), otherwise (

else) we make it turn left 225 degrees. The code is as follows:

>>> t.reset()
>>> for x in range(1,19):
    t.forward(100)    if x % 2 == 0:
        t.left(175)    else:
        t.left(225)

The effect is as follows:

Using advanced turtle plotting methods in Python

Three, draw the car

Try to draw a car and set a small goal for yourself. Maybe one day you will achieve it.

(This code

adds

color,begin_fill,end_fill,circle,set

headingfunctions)

>>> import turtle
>>> t = turtle.Pen()
>>> t.color(1,0,0)
>>> t.begin_fill()
>>> t.forward(100)
>>> t.left(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(20)
>>> t.right(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(60)
>>> t.left(90)
>>> t.forward(20)
>>> t.right(90)
>>> t.forward(20)
>>> t.left(90)
>>> t.forward(20)
>>> t.end_fill()
Body

>>> t.color(0,0,0)
>>> t.up()
>>> t.forward(10)
>>> t.down()
>>> t.begin_fill()
>>> t.circle(10)
>>> t.end_fill()
Left wheel

>>> t.setheading(0)
>>> t.up()
>>> t.forward(90)
>>> t.right(90)
>>> t.forward(10)
>>> t.setheading(0)
>>> t.begin_fill()
>>> t.down()
>>> t.circle(10)
>>> t.end_fill()

右车轮
Right wheel

The integrated effect is as follows:

Let’s focus on the newly added functions: Using advanced turtle plotting methods in Python

1,

color

is used Change the brush color.

2, begin_fill

and

end_fill are used to fill an area on the canvas. 3, circle

is used to draw a circle of a specified size.

4, setheading

Let the turtle face the specified direction.

Summary:

This time I used Python’s turtle module more deeply than last time to draw several basic geometric figures, as well as for loops and if statements. to control the turtle's movements on the screen. Also changes the color of the turtle's pen and fills in the shapes it draws. Next, you will start learning coloring.

The above is the detailed content of Using advanced turtle plotting methods in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn