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
t.speed(5)t.pensize(1)t.screensize(500, 500)t.bgcolor('white')

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 .

#  Head def head():    t.up()    t.circle(150, 45)    t.down()    t.fillcolor(head_color)    t.begin_fill()    t.circle(150, 270)    t.end_fill()

# The scarf def scarf():    t.fillcolor(scarf_color)    t.begin_fill()    t.seth(0)    t.fd(216)    t.circle(-5, 90)    t.fd(10)    t.circle(-5, 90)    t.fd(220)    t.circle(-5, 90)    t.fd(10)    t.circle(-5, 90)    t.end_fill()

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 .

def face():    t.fd(186)    t.lt(45)    t.fillcolor(color_white)    t.begin_fill()    t.circle(120, 100)    t.seth(180)    t.fd(120)    t.seth(215)    t.circle(120, 100)    t.end_fill()
def draw_eyes():    t.fillcolor(color_white)    t.begin_fill()    a = 2.5    for i in range(120):        if 0 <= i < 30 or 60 <= i < 90:            a -= 0.05        else:            a += 0.05        t.lt(3)        t.fd(a)    t.end_fill()
def eyes():    go_to(0, 227)    t.seth(90)    draw_eyes()    go_to(0, 227)    t.seth(270)    draw_eyes()

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 .

# nose def nose():    go_to(-13, 166)    t.seth(315)    t.fillcolor(nose_color)    t.begin_fill()    t.circle(20)    t.end_fill()

# mouth def mouth():    go_to(0, 156)    t.seth(270)    t.fd(100)    pos = t.pos()    t.seth(0)    t.circle(110, 60)    go_to(pos[0], pos[1])    t.seth(180)    t.circle(-110, 60)


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 .

# beard def mustache():    h = 70    go_to(30, 140)    t.seth(15)    t.fd(h)
   go_to(30, 130)    t.seth(0)    t.fd(h)
   go_to(30, 120)    t.seth(-15)    t.fd(h)
   go_to(-30, 140)    t.seth(150)    t.fd(h)
   go_to(-30, 130)    t.seth(180)    t.fd(h)
   go_to(-30, 120)    t.seth(195)    t.fd(h)
# small bell def bell():    # Great circle    go_to(0, 33)    t.pensize(1)    t.fillcolor("#FCE341")    t.begin_fill()    t.circle(25)    t.end_fill()
   # Horizontal stripe    go_to(-15, 22)    t.seth(0)    t.forward(42)    go_to(-18, 17)    t.seth(0)    t.forward(47)
   # Small circle    go_to(5, 0)    t.pensize(1)    t.color("black", '#79675D')    t.begin_fill()    t.circle(5)    t.end_fill()    t.seth(270)    t.pensize(1)    t.forward(15)

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 .