Using the Advanced Turtle (Turtle) mapping method in Python

Source: Internet
Author: User
In Python, a turtle can not only draw a simple black line, but also use it to draw more complex geometry, use different colors, and even fill the shape with color.

First, starting from the 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 >>> t.left (All) >>> T.forward (+) >>> t.left (All) >>> T.forward (>>> t.left) >>> T,forward (50)

This code is too long and we can use a for loop to optimize it:

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

The effect is as follows:

Second, draw the stars

We just 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像素    t.left (225)         # #向左旋转225度

The effect is as follows:

However, we can further improve, such as rotating 175 degrees each time, loop 37 times, the code is as follows:

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

The effect is as follows:

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

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

The effect is as follows:

Now let's use the IF statement to control the turtle's steering to draw different stars. Let the turtle turn an angle first and then the next time turn a different angle.

Here, we first create a loop that runs 18 times (1,19), and then let the turtle move forward 100 pixels (T.forward (100)). Next is the IF statement (ifx%2 = = 0), which means that the remainder of x divided by 2 is equal to 0. If the number in X is even, we let the turtle turn left 175 degrees (T.left (175)), or else we let it turn left 225 degrees. The code is as follows:

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

The effect is as follows:

Three, draw a car

Try to draw a car, set yourself a small goal, and maybe one day it will happen.

(This code adds the Color,begin_fill,end_fill,circle,setheading function)

>>> Import turtle>>> t = Turtle. Pen () >>> t.color (1,0,0) >>> T.begin_fill () >>> T.forward (+) >>> t.left (All) > >> T.forward >>> t.left (All) >>> T.forward >>> t.right (All) >>> T.forward (>>> t.left) >>> t.forward >>> t.left (All) >>> T.forward (20) >>> t.right >>> t.forward >>> t.left (+) >>> T.forward (>>>) T.end_fill ()

Body

>>> T.color (0,0,0) >>> t.up () >>> T.forward (Ten) >>> T.down () >>> T.begin_ Fill () >>> t.circle (Ten) >>> T.end_fill ()

Left wheel

>>> t.setheading (0) >>> t.up () >>> T.forward (+) >>> t.right (All) >>> T.forward >>> t.setheading (0) >>> T.begin_fill () >>> t.down () >>> t.circle (10) >>> T.end_fill () Right Wheel

Right Wheel

The following results are integrated:

Here are a few new functions to focus on:

1, color is used to change the color of the brush.

2,Begin_fill and End_fill are used to fill an area of the canvas.

3,Circle is used to draw a circle of a specified size.

4, setheading Let the turtle face the specified direction.

Summarize:

This time, the Python turtle module was used to draw a few basic geometries, as well as a for loop and if statement to control the action of the turtle on the screen. It also changes the color of the turtle's pen and fills the shape it draws. The next step is to learn to fill the color.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.