This article mainly introduces the examples of turtle drawing in Python, and shares several small examples of turtle drawing, which has certain reference value. Friends in need can learn about it
In Python, turtles can not only draw simple black lines, but also draw more complex geometric shapes with different colors, and even fill shapes with colors.
1, Start with 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(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 optimize it with the for loop:
>>> t.reset() >>> for x in range(1,5): t.forward(50) t.left(90)
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): ##Cycle eight times t.forward(100) ##Forward 100 pixels t.left(225) ##Rotate 225 degrees to the left
However, we can further improve it. For example, rotate 175 degrees each time and cycle 37 times. The code is as follows:
>>> t.reset() >>> for x in range(1,38): t.forward(100) t.left(175)
We can also draw spiral stars. The code is as follows:
>>> t.reset() >>> for x in range(1,20): t.forward(100) t.left(95)
Now let's use the if statement to control the turtle's turn to draw different stars. Let the turtle turn one angle first, and then turn a different angle next time.
Here, we first create a cycle that runs 18 times (range(1,19)), and then let the turtle move forward 100 pixels (t.forward(100)). The following 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 even, we make the turtle turn left 175 degrees (t.left(175)), otherwise (else) we make it turn left 225 degrees. Finally, if your time is not very tight and you want to improve python quickly, the most important thing is that you are not afraid of hardship. I suggest you can pay the price (homonym): 762459510. That's really good. Many people make rapid progress and need you to be not afraid of hardship! You can add it and have a look~
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)
3, Draw a car
Try to draw a car and set a small goal for yourself. Maybe it will be achieved one day.
(color, begin_fill, end_fill, circle and setheading functions are added to 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
Here are some new functions:
1. Color is used to change the brush color.
2,begin_fill and end_fill is used to fill an area on the canvas.
3. Circle is used to draw a circle of specified size.
4. setheading makes the turtle face the specified direction.
Summary:
This time, more than last time, Python's turtle module is used to draw several basic geometric figures, as well as for loops and if statements to control the turtle's actions on the screen. At the same time, the color of the turtle's pen is changed and the shape it draws is colored.