leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python turtle module combat: drawing the head of the robot cat

Estimated reading: 6 minutes 62 views

We have analyzed the drawing of the robot cat before, let’s draw the head of the robot cat

head() function

Here, we define a function to draw the big head of the robot cat – head(). First, it draws a big blue-filled circle, representing the Doraemon’s head. Then, within the blue circle, draw a slightly smaller white filled circle to represent the Doraemon’s face. The code for the head() function is shown below.

def head(): 
    #big circle
  
    color("blue","blue")
    penup()
    goto(0,100)
    drawRound(75,True)
    #small circle
    color("white","white")
    penup()
    goto(0,72)
    drawRound(60,True)

First call the color() function, set the color of the brush to blue, and set the color of the fill to blue. Then call the penup() function to lift the pen without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x-coordinate is 0 and the y-coordinate is 100. Call the custom function drawRound() we created earlier to draw a circle with a radius of 75 pixels and filled with blue.

Then call the color() function again, set the color of the brush to white, and set the color of the fill to white as well. Then call the penup() function to lift the pen without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x-coordinate is 0 and the y-coordinate is 72. Call the drawRound() function to draw a circle with a radius of 60 pixels filled with white.

Call this head() function to see the execution effect, as shown in Figure 1.

head() function
figure 1

eyes() function

Next, we define the eyes() function to draw the eyes of the robot cat. First, we draw two large circles with black borders and filled with white, representing the two eyes of Doraemon. Then inside each large circle draw a circle filled with black, and within this black circle draw a smaller circle filled with white, which represent the Doraemon’s eyeballs.

def eyes():
   #left eye socket   
    color("black","white")   
    penup()
    goto(-15,80)
    drawRound(17,True)
    #right eye socket
    color("black","white")   
    penup()
    goto(19,80)
    drawRound(17,True)
   #left eyeball
    color("black","black")
    penup()
    goto(-8,70)
    drawRound(6,True)   
    color("white","white")
    penup()
    goto(-8,66)
    drawRound(2,True)
    #right eyeball
    color("black","black")
    penup()
    goto(12,70)
    drawRound(6,True)   
    color("white","white")
    penup()
    goto(12,66)
    drawRound(2,True)

First draw the left eye socket of the robot cat. Call the color() function to set the color of the brush to black and the color of the fill to white. Then call the penup() function to lift the pen without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x-coordinate is −15 and the y-coordinate is 80. Call the custom function drawRound() to draw a circle with a radius of 17 pixels and filled with white.

Then draw the right eye socket. This code is basically the same as the code for drawing the left eye, but the parameters passed to the goto() function are different. Move the brush to the position where the x coordinate is 19 and the y coordinate is 80.

Next draw the left and right eyeballs. Or call the color() function, set the color of the brush and fill, move the brush, and draw the circle.

Call the eyes() function to see the effect, as shown in Figure 2.


figure 2

nose() function

Next, we define the nose() function to draw the nose. The nose is very simple, just a red circle. The code for the nose function is shown below.

def nose():    
    color("red","red")
    penup()
    goto(0,40)
    drawRound(7,True)

Call the color function to set the brush and fill color to red. Then call the penup() function to lift the brush without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x-coordinate is 0 and the y-coordinate is 40. Call the custom function drawRound() to draw a circle with a radius of 7 pixels and filled with red.

Call this nose() function to see the drawing effect, as shown in Figure 3.

nose() function
image 3

mouth() function

Next, we define a mouth() function to draw the Doraemon’s mouth. It will first draw an arc to represent the mouth shape, and then draw a vertical line to represent the “human center” of the robot cat. The code for the mouth() function is shown below.

def mouth():   
    #嘴
    color("black","black")
    penup()
    goto(-30,-20)
    pendown()
    setheading (-27)
    circle(70,55)
    #人中
    penup()
    goto(0,26)    
    pendown()
    goto(0,-25)

Call the color function to set both the brush and fill colors to black. Then call the penup() function to lift the brush without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x coordinate is −30 and the y coordinate is −20. Then call the pendown() function to drop the pen. Call setheading(-27) to set the direction of movement of the turtle when it starts. Call circle(70,55) to draw an arc representing the Doraemon’s mouth.

Next, call the penup() function to lift the pen without leaving handwriting on the canvas. Call the goto() function to move the pen to the position where the x coordinate is 0 and the y coordinate is 26. Then call the pendown() function to drop the pen. Call goto(0,-25) to draw a straight line representing the “people” of the robot cat.

Call the mouth() function to see the drawing effect, as shown in Figure 4.

mouth() function
Figure 4

whiskers() function

Next, we define a whiskers() function to draw the mustache. It draws 3 straight lines on both sides of the “people” of the robot cat, representing whiskers. The code for the whiskers() function is shown below.

def whiskers():
    color("black","black")
    #left middle beard
    penup()
    goto(10,5)
    pendown()
    goto(-40,5)
    #right middle beard
    penup()
    goto(10,5)
    pendown()
    goto(40,5)
    #top left beard
    penup()
    goto(-10,15)
    pendown()
    goto(-40,20)
    #top right beard
    penup()
    goto(10,15)
    pendown()
    goto(40,20)
    #lower left beard
    penup()
    goto(-10,-5)
    pendown()
    goto(-40,-10)
    #lower right beard
    penup()
    goto(10,-5)
    pendown()
    goto(40,-10)

Or call the color() function first to set both the brush and fill colors to black. Then call the penup() function to lift the brush without leaving handwriting on the canvas. Call the goto() function to move the pen to the specified position. Then call the pendown() function to drop the pen. Call the goto() function to draw a straight line representing the first beard on the left.

Then repeat a similar action to draw the remaining 5 beards. The code of this part is basically the same, but the coordinate position moved to is different, so I won’t repeat it here. Call the whiskers() function to see the drawing effect, as shown in Figure 5.

whiskers() function

Leave a Comment

CONTENTS