Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.2 kB
1
Indexable
Never
import turtle 
import math
def draw_caterpillar(centre_x,centre_y,radius,head_color,body_color,num_segments):
    # Create the turtle object
    t = turtle.Turtle()
    offset_x = (num_segments-1)*radius
    # Draw the body segments
    for i in range(num_segments):
        current_pos = centre_x-1.1*radius-offset_x,centre_y-(radius*1.3)
        t.penup()
        t.goto(current_pos)
        t.pendown()
        t.fillcolor(body_color)
        t.begin_fill()
        t.circle(radius*0.8)
        t.end_fill()
        # Draw the legs of body
        t.goto(current_pos[0],current_pos[1]-0.5*radius)
        t.goto(current_pos[0]+0.40*radius,current_pos[1]-0.5*radius)
        t.penup()
        t.goto(current_pos) 
        t.setheading(300)
        t.pendown()
        t.forward(0.53*radius)
        t.setheading(30)
        t.forward(0.35*radius)
        t.setheading(0)

        offset_x-=radius
    # Draw the head
    t.fillcolor(head_color)
    t.penup() 
    t.goto(centre_x,centre_y-radius)
    t.pendown() 
    t.begin_fill()
    t.circle(radius) 
    t.end_fill()
    t.penup()
    t.goto(centre_x,centre_y)
    t.pendown()
    t.fillcolor("#000000")
    t.begin_fill()
    t.circle(radius/10)
    t.end_fill()
    t.penup()
    # Draw the eyes
    t.goto(centre_x+radius*9.3/10,centre_y)
    t.pendown()
    t.fillcolor("#000000")
    t.begin_fill()
    t.circle(radius/10)
    t.end_fill()
    t.penup()
    t.goto(centre_x-radius/10,centre_y-radius*0.3)
    t.pendown() 
    t.setheading(-35)
    t.circle(radius*0.7,70)
    t.penup() 
    t.setheading(0)
    # Draw the head organs at the top
    theta = 60*math.pi/180
    x_theta,y_theta = abs(radius*math.cos(theta)),abs(radius*math.sin(theta) )
    t.goto(centre_x+x_theta,centre_y+y_theta)
    t.setheading(110)
    t.pendown()
    t.forward(0.5*radius)
    t.setheading(55)
    t.forward(0.4*radius)
    t.penup()
    t.goto(centre_x+x_theta,centre_y+y_theta)
    t.setheading(80)
    t.pendown()
    t.forward(0.5*radius)
    t.setheading(30)
    t.forward(0.4*radius)
    t.penup()
    t.goto(centre_x+x_theta,centre_y+y_theta)
    t.setheading(180)
    t.speed(0)
    # turtle.mainloop()
    turtle.done()

draw_caterpillar(-200,100,50,"#0b3d22", "#0f756c", 5)