Advanced turtle (turtle) ing in Python, pythonturtle
In Python, turtles can not only draw simple black lines, but also draw more complex ry, use different colors, and even fill the shape.
1. Start with a basic square
IntroductionTurtle ModuleAnd createPen object:
>>> import turtle>>> t = turtle.Pen()
The code used to create a 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 segment is too long. We can use the for loop for optimization:
>>> t.reset()>>> for x in range(1,5): t.forward(50) t.left(90)
The effect is as follows:
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 (): # loop eight times t. forward (100) # forward 100 pixel t. left (225) # rotate 225 degrees left
The effect is as follows:
However, we can further improve it, such as rotating 175 degrees each time and repeating 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:
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:
Now let's use the if statement to control the redirection of turtles to draw different stars. Let the Turtles first turn an angle, and then next time turn a different angle.
Here, we first create a loop that runs 18 times (range (100), and then let the turtle move 100 pixels forward (t. forward )). The next step is the if Statement (ifx % 2 = 0), which indicates whether the remainder of x divided by 2 is equal to 0. if the number in x is an even number, let the turtle turn left to 175 degrees (t. left (175); otherwise (else), let us turn it 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:
3. Draw a car
Try to draw a car and set up a small goal for yourself. Maybe it will happen in one day.
(The color, begin_fill, end_fill, circle, and setheading functions are added in this Code)
>>> 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
After integration, the effect is as follows:
The following describes several new functions:
1,ColorIs used to change the paint brush color.
2,Begin_fillAndEnd_fillIs used to fill the area on the canvas.
3,CircleIs used to draw a circle of the specified size.
4,SetheadingLet the Turtles Face the specified direction.
Summary:
This time, I used the Python turtle module more deeply to draw several basic geometric figures, as well as for loops and if statements to control the movements of turtles on the screen. At the same time, it changes the color of the turtle pen and colors the shape it draws. Next, we will start to learn how to fill the color.
I feel more and more interesting. The harder I work, the luckier I am. Pai_^