writing | doug
source ：Python technology 「ID: pythonall」
I believe you all saw Dora in your childhood A dream , His pocket is absolutely omnipotent , It's full of all kinds of magical props . I used to fantasize that if I had such a pocket . We'll use it today Python Let's draw a Dora A dream , Miss our childhood .
Let's take a look at our final rendering .
The outline of the head and the scarf
Old rules , Let's start with the library we're going to use today turtle And make some basic settings .
import turtle as t
First , Let's draw a lot of pictures first A The outline of the dream's head , The outline of the head is mainly a semicircle , The scarf is a small rectangle .
# The scarf
Let's see how it works , Uh, uh, uh , How does it feel like a stone mound on the main road , Except for the color .
No hurry, no hurry , Let's draw the facial details again .
As the saying goes, the eye is the window of the soul , Let's draw the eyes first .
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
a += 0.05
You can't draw a circle directly by drawing eyes , That would look strange , You have to draw an ellipse , Let's see the effect .
I feel much more energetic after I draw my eyes , It's a bit too much A It's like a dream . But it still looks a little bit wrong , Yes , The eye hasn't drawn the pupil yet .
Simplicity , Just fill in two black circles .
def fill_eyes(): # Fill the eyes go_to(-15, 220) t.pensize(12) t.color('black') for i in range(30): t.forward(2) t.right(12) go_to(15, 220) for i in range(30): t.forward(2) t.left(12) t.pensize(1)
Good good , It's getting better and better . Now let's add the nose and mouth .
nose & mouth
It's not hard for the nose , Just draw a small circle under your eyes , The mouth is like an inverted one T word .
pos = t.pos()
It's the last step to success , Paint the beard and the bell and you're done .
beard & small bell
The beard is similar to that of a cat , The bell watch is more complicated , Big circle sets small circle , And decoration .
h = 70
# small bell
# Great circle
# Horizontal stripe
# Small circle
Finally, let's write an entry function , Encapsulate these functions that draw different parts of the body . The code is as follows ;
if __name__ == '__main__': head() scarf() face() eyes() fill_eyes() nose() mouth() mustache() bell() go_to() t.hideturtle() t.done()
The end result is as follows ：
Do you think it's very simple , Go to the background to get the source code run Get up . Interested readers can also add body to it .
In this article we use Python Of turtle Ku drew a picture A dream , Don't know and everyone remember Dora A Is the dream the same .
Actually turtle It's not hard to use , The main thing is to clarify the position and orientation of the tortoise , And then there's the movement pattern , Straight line motion , Or curve motion and how to change the direction of motion . The calculation of tortoise's coordinates is troublesome , Especially when you're doing curvilinear motion , You can combine pos() Function to get the coordinates of the tortoise , It will help you to sort out your drawing ideas .