[Python] How to fill a specific area (sock) with pink color in turtle graphics?

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Python's turtle module to draw a character illustration. Most parts of the drawing are correctly colored using begin_fill() and end_fill(). However, I'm not sure how to fill the sock area with pink, as indicated by the arrow in the attached image:

    import turtle as t
    t.setup(1000,750)
    def hair():
    t.begin_fill()
    t.color("black")
    t.pd()
    t.left(50)
    t.circle(-80,40)
    t.left(5)
    t.circle(-220,40)
    t.circle(-100,40)
    t.circle(-200,15)
    t.pos()
    t.right(70)
    t.circle(100,12)
    t.right(130)
    t.circle(250,15)
    t.circle(25,60)
    t.left(120)
    t.circle(-10,150)
    t.circle(-33,20)
    t.circle(25,120)
    t.right(10)
    t.circle(-190,2)
    t.circle(-10,180)
    t.circle(-180,10)
    t.left(133)
    t.circle(220,12)
    t.circle(300,9)
    t.left(144)
    t.circle(-80,23)
    t.circle(-10,180)
    t.circle(-100,6)
    t.circle(20,120)
    t.fd(10)
    t.circle(-10,100)
    t.circle(-30,30)
    t.end_fill()
    def head():
    t.seth(-130)
    t.begin_fill()
    t.color("black","navajo white")
    t.circle(90,50)
    t.right(90)
    t.circle(80,50)
    t.circle(60,50)
    t.circle(90,40)
    t.circle(130,40)
    t.circle(550,20)
    t.right(55)
    t.circle(400,4)
    t.circle(35,130)
    t.circle(150,15)
    t.circle(20,80)
    t.circle(30,50)
    t.goto(150,120)
    t.seth(90)
    t.circle(100,90)
    t.circle(600,10)
    t.end_fill()
    t.seth(0)
    def braw():
    t.seth(90)
    t.circle(-40,180)
    t.pu()
    t.goto(25,140)
    t.seth(75)
    t.pd()
    t.circle(-45,160)
    def eyes():
    t.seth(0)
    t.begin_fill()
    t.color("black","black")
    t.circle(36)
    t.end_fill()
    t.seth(90)
    t.fd(22)
    t.seth(0)
    t.begin_fill()
    t.color("white","white")
    t.circle(13)
    t.end_fill()
    def mouse():
    t.pencolor("black")
    t.seth(20)
    t.fd(10)
    def close():
    t.seth(-110)
    t.begin_fill()
    t.color("black","red")
    t.fd(17)
    t.circle(10,80)
    t.fd(50)
    t.left(105)
    t.fd(10)
    t.bk(80)
    t.left(180)
    t.circle(20,90)
    for i in range(5):
    t.circle(50,30)
    t.circle(-50,30)
    t.seth(90)
    t.fd(75)
    t.seth(-160)
    t.fd(45)
    t.right(100)
    t.fd(30)
    t.bk(30)
    t.right(80)
    t.fd(80)
    t.circle(10,100)
    t.fd(30)
    t.circle(-50,30)
    t.fd(10)
    t.circle(10,90)
    for i in range(3):
    t.circle(20,20)
    t.circle(-20,20)
    t.end_fill()
    def hand1():
    t.begin_fill()
    t.color("black","navajo white")
    t.seth(-100)
    t.circle(30,25)
    t.circle(-60,40)
    t.circle(15,60)
    t.circle(-15,120)
    t.circle(-50,15)
    t.circle(-10,120)
    t.left(150)
    t.circle(-7,100)
    t.circle(-15,40)
    t.right(20)
    t.circle(-5,70)
    t.circle(7,150)
    t.circle(30,10)
    t.circle(-50,20)
    t.circle(30,20)
    t.circle(-40,10)
    t.end_fill()
    def hand2():
    t.seth(125)
    t.begin_fill()
    t.color("black","navajo white")
    t.circle(-65,35)
    t.left(90)
    t.fd(10)
    t.left(15)
    t.circle(30,20)
    t.circle(-20,30)
    t.circle(-3,140)
    t.circle(-100,10)
    t.left(150)
    t.circle(-100,15)
    t.circle(-5,150)
    t.circle(-100,10)
    t.left(140)
    t.circle(-100,13)
    t.circle(-6,150)
    t.circle(-100,10)
    t.circle(3,120)
    t.circle(-100,10)
    t.circle(-3,140)
    t.fd(25)
    t.left(100)
    t.fd(15)
    t.circle(-5,150)
    t.fd(20)
    t.circle(20,90)
    t.end_fill()
    def trou():
    t.seth(-100)
    t.begin_fill()
    t.color("black","khaki1")
    t.fd(50)
    t.circle(5,100)
    t.fd(110)
    t.circle(10,160)
    t.left(180)
    t.circle(-10,65)
    t.circle(10,85)
    t.fd(120)
    t.circle(10,95)
    t.fd(40)
    t.end_fill()
    def legs1():
    t.seth(-90)
    t.begin_fill()
    t.color("black","navajo white")
    t.circle(100,25)
    t.left(50)
    t.circle(50,38)
    t.left(70)
    t.circle(-100,23)
    t.end_fill()
    t.left(180)
    t.circle(100,35)
    t.right(95)
    t.begin_fill()
    t.color("black","lightgoldenrod")
    t.fd(28)
    t.right(70)
    t.circle(-100,15)
    t.left(180)
    t.circle(100,6)
    t.right(90)
    t.circle(10,55)
    t.circle(-30,40)
    t.circle(-10,20)
    t.circle(-100,13)
    t.circle(5,179)
    t.fd(78)
    t.circle(5,120)
    t.fd(17)
    t.end_fill()
    def legs2():
    t.seth(-60)
    t.begin_fill()
    t.color("black","navajo white")
    t.circle(-75,30)
    t.left(75)
    t.circle(50,43)
    t.left(70)
    t.fd(35)
    t.end_fill()
    t.left(180)
    t.fd(35)
    t.right(5)
    t.begin_fill()
    t.color("black","lightgoldenrod")
    t.fd(20)
    t.right(20)
    t.circle(-20,125)
    t.right(20)
    t.circle(-25,10)
    t.fd(20)
    t.left(180)
    t.fd(20)
    t.circle(25,10)
    t.right(55)
    t.fd(20)
    t.circle(5,125)
    t.fd(76)
    t.circle(5,180)
    t.circle(-35,40)
    t.circle(-15,40)
    t.circle(5,90)
    t.end_fill()
    def snow(size,n):
    import turtle as t
    t.pencolor("skyblue")
    t.pensize(2)
    if n==1:
    t.fd(size)
    t.left(60)
    t.fd(size)
    t.right(120)
    t.fd(size)
    t.left(60)
    t.fd(size)
    else:
    snow(size/3,n-1)
    t.left(60)
    snow(size/3,n-1)
    t.right(120)
    snow(size/3,n-1)
    t.left(60)
    snow(size/3,n-1)

    t.speed(10)
    t.pensize(5)
    t.pu()
    t.goto(-125,-145)
    t.pd()
    trou()
    t.pu()
    t.goto(-100,-203)
    t.pd()
    legs1()
    t.pu()
    t.goto(45,-200)
    t.pd()
    legs2()
    t.pu()
    t.goto(-160,0)
    t.pd()
    close()
    t.pu()
    t.goto(155,-67)
    t.pd()
    hand1()
    t.pu()
    t.goto(-185,60)
    t.pd()
    hand2()
    t.pu()
    t.goto(-100,200)
    t.pd()
    head()
    t.pu()
    t.goto(-100,200)
    t.pd()
    hair()
    t.pu()
    t.goto(-98,130)
    t.pd()
    braw()
    t.pu()
    t.goto(-53,70)
    t.pd()
    eyes()
    t.pu()
    t.goto(50,72)
    eyes()
    t.pu()
    t.goto(-60,-10)
    t.pd()
    mouse()
    t.speed(10000)
    for i in range(10):
    import random as ran
    x=ran.uniform(-400,-200)
    y=ran.uniform(-250,250)
    t.pu()
    t.goto(x,y)
    t.pd()
    for j in range(3):
    snow(12,3)
    t.right(120)
    t.pu()
    x = ran.uniform(200, 400)
    y = ran.uniform(-250, 250)
    t.pu()
    t.goto(x, y)
    t.pd()
    for j in range(3):
    snow(12, 3)
    t.right(120)
    t.pu()

    #
    t.pu()
    # t.goto(200,-200)
    # t.pd()
    # t.write("***",font=(18))
    t.done()


    I want to add a pink section between the leg and the shoe to represent a sock. I’m not sure where to place the new fill and how to draw the shape accurately within the current turtle path flow.

    How should I modify the code to include this pink sock section?

    [​IMG]

    Continue reading...

Compartilhe esta Página