用Python填充三角形(海龟)

用Python填充三角形(海龟),python,turtle-graphics,Python,Turtle Graphics,我可能会有很多不喜欢的东西,但我真的需要知道这一点。 我试过了turtle.begin\u fill()等等,但没有什么对我有效 我想用Python制作填充三角形/矩形,但我不知道如何在代码中实现它 当前代码: import turtle, math def theme(): turtle.speed(10) column_1() reset_column() column_2() reset_column() column_1() def r

我可能会有很多不喜欢的东西,但我真的需要知道这一点。 我试过了
turtle.begin\u fill()
等等,但没有什么对我有效

我想用Python制作填充三角形/矩形,但我不知道如何在代码中实现它

当前代码:

import turtle, math
def theme():
    turtle.speed(10)

    column_1()
    reset_column()
    column_2()
    reset_column()
    column_1()

def reset_column():
    turtle.up()
    turtle.forward(160)
    turtle.left(90)
    turtle.forward(480)
    turtle.right(90)
    turtle.down()

def reset_box():
    turtle.up()
    turtle.forward(3 * 32)
    turtle.right(90)
    turtle.forward(2 * 32)
    turtle.right(180)
    turtle.down()

def column_2():
    empty()
    reset_box()
    filled()
    reset_box()
    empty()
    reset_box()

def column_1():
    filled()
    reset_box()
    empty()
    reset_box()
    filled()
    reset_box()

def filled():
    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(135)
    turtle.forward(32 * math.sqrt(2))
    for index in range(4):
        for index in range(3):
            turtle.left(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
            turtle.right(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.right(90)
    turtle.forward(32)

    first = True
    for index in range(4):
        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)
def empty():
    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(90)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.left(90)
    turtle.left(90)
    turtle.forward(32)

    first = True
    for index in range(4):
        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)

theme()
我的结果是: 我想要的是:


有什么建议吗?真的不知道怎么开始这样的事情。这是我第一次接触Python,尤其是海龟力学

使用
turtle.begin\u fill()
turtle.end\u fill()
,也可以是
turtle.fillcolor

下面是实现它的代码以及一个。我对所有代码行都有评论

import turtle, math
def theme():
    turtle.speed(10)

    column_1()
    reset_column()
    column_2()
    reset_column()
    column_1()

def reset_column():
    turtle.up()
    turtle.forward(160)
    turtle.left(90)
    turtle.forward(480)
    turtle.right(90)
    turtle.down()

def reset_box():
    turtle.up()
    turtle.forward(3 * 32)
    turtle.right(90)
    turtle.forward(2 * 32)
    turtle.right(180)
    turtle.down()

def column_2():
    empty()
    reset_box()
    filled()
    reset_box()
    empty()
    reset_box()

def column_1():
    filled()
    reset_box()
    empty()
    reset_box()
    filled()
    reset_box()

def filled():

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(135)
    turtle.forward(32 * math.sqrt(2))
    for index in range(4):
        for index in range(3):
            turtle.left(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
            turtle.right(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.right(90)
    turtle.forward(32)

    first = True
    for index in range(4):
        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)

    turtle.end_fill()     # NEW CODE

def empty():

    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(90)
    turtle.up()
    turtle.forward(32)
    turtle.down()

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.left(90)
    turtle.left(90)
    turtle.forward(32)

    turtle.end_fill()         # NEW CODE

    turtle.fillcolor('white') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    first = True
    for index in range(4):

        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)

        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.end_fill()       # NEW CODE

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)

    turtle.end_fill()       # NEW CODE

theme()

使用
turtle.begin\u fill()
turtle.end\u fill()
也可以是
turtle.fillcolor

下面是实现它的代码以及一个。我对所有代码行都有评论

import turtle, math
def theme():
    turtle.speed(10)

    column_1()
    reset_column()
    column_2()
    reset_column()
    column_1()

def reset_column():
    turtle.up()
    turtle.forward(160)
    turtle.left(90)
    turtle.forward(480)
    turtle.right(90)
    turtle.down()

def reset_box():
    turtle.up()
    turtle.forward(3 * 32)
    turtle.right(90)
    turtle.forward(2 * 32)
    turtle.right(180)
    turtle.down()

def column_2():
    empty()
    reset_box()
    filled()
    reset_box()
    empty()
    reset_box()

def column_1():
    filled()
    reset_box()
    empty()
    reset_box()
    filled()
    reset_box()

def filled():

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(135)
    turtle.forward(32 * math.sqrt(2))
    for index in range(4):
        for index in range(3):
            turtle.left(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
            turtle.right(90)
            turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.right(90)
    turtle.forward(32)

    first = True
    for index in range(4):
        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)
        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()
    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)

    turtle.end_fill()     # NEW CODE

def empty():

    size = 480
    box = size / 3
    for index in range(4):
        turtle.forward(box)
        turtle.right(90)
    turtle.forward(32)
    turtle.right(90)
    turtle.up()
    turtle.forward(32)
    turtle.down()

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    for index in range(4):
        turtle.forward(box - (2 * (box / 5)))
        turtle.left(90)
    turtle.left(90)
    turtle.forward(32)

    turtle.end_fill()         # NEW CODE

    turtle.fillcolor('white') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    first = True
    for index in range(4):

        if (first):
            turtle.right(135)
            first = False
        else:
            turtle.left(90)

        turtle.forward(32 * math.sqrt(2))
        turtle.left(90)
        turtle.forward((32 * math.sqrt(2)) / 2)
        turtle.right(90)
        turtle.forward((32 * math.sqrt(2)) / 2)

    turtle.end_fill()       # NEW CODE

    turtle.left(135)
    turtle.up()
    turtle.forward(32)
    turtle.down()

    turtle.fillcolor('black') # NEW CODE
    turtle.begin_fill()       # NEW CODE

    for index in range(4):
        turtle.forward(box - (4 * (box / 5)))
        turtle.left(90)

    turtle.end_fill()       # NEW CODE

theme()

我相信这是另一种冲压比绘图效果更好的情况。我将其制作为一套五枚邮票,其中一枚具有可变填充颜色:

from turtle import Turtle, Screen

SIZE = 480
BOX = SIZE // 3
STAMP_SIZE = 20
STEP = 32 * 2 ** 0.5

screen = Screen()
stamps = []

stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / STAMP_SIZE)
stamp.color('black', 'white')
stamps.append(stamp)

stamp2 = Turtle()
stamp2.goto(STEP / 2, -BOX / 2 - STEP / 4)
stamp2.begin_poly()
for _ in range(4):
    for _ in range(3):
        stamp2.left(90)
        stamp2.forward(STEP / 2)
        stamp2.right(90)
        stamp2.forward(STEP / 2)
    stamp2.left(90)
    stamp2.forward(STEP)
stamp2.end_poly()
polygon = stamp2.get_poly()
screen.addshape('stamp2', polygon)

stamp2.reset()
stamp2.shape('stamp2')
stamp2.color('white')
stamp2.tilt(45)
stamps.append(stamp2)

stamp = Turtle('square', visible=False)
stamp.shapesize(3 * BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)

stamp4 = Turtle()
stamp4.setx(BOX / 5)
stamp4.right(45)
stamp4.begin_poly()
for index in range(4):
    stamp4.forward(STEP / 2)
    stamp4.right(90)
    stamp4.forward(STEP)
    stamp4.right(90)
    stamp4.forward(STEP / 2)
    stamp4.left(90)
stamp4.end_poly()
polygon = stamp4.get_poly()
screen.addshape('stamp4', polygon)

stamp4.reset()
stamp4.shape('stamp4')
stamp4.color('white')
stamps.append(stamp4)

stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)

for stamp in stamps:
    stamp.speed('fastest')
    stamp.penup()

parity = True

for x in range(-BOX, BOX + 1, BOX):
    for y in range(-BOX, BOX + 1, BOX):

        stamps[0].fillcolor(['white', 'black'][parity])

        for stamp in stamps:
            stamp.goto(x, y)
            stamp.stamp()

        parity = not parity

for stamp in stamps:
    stamp.hideturtle()

screen.exitonclick()
您会发现这使得代码更简单、更快


我认为这是另一种冲压比绘图效果更好的情况。我将其制作为一套五枚邮票,其中一枚具有可变填充颜色:

from turtle import Turtle, Screen

SIZE = 480
BOX = SIZE // 3
STAMP_SIZE = 20
STEP = 32 * 2 ** 0.5

screen = Screen()
stamps = []

stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / STAMP_SIZE)
stamp.color('black', 'white')
stamps.append(stamp)

stamp2 = Turtle()
stamp2.goto(STEP / 2, -BOX / 2 - STEP / 4)
stamp2.begin_poly()
for _ in range(4):
    for _ in range(3):
        stamp2.left(90)
        stamp2.forward(STEP / 2)
        stamp2.right(90)
        stamp2.forward(STEP / 2)
    stamp2.left(90)
    stamp2.forward(STEP)
stamp2.end_poly()
polygon = stamp2.get_poly()
screen.addshape('stamp2', polygon)

stamp2.reset()
stamp2.shape('stamp2')
stamp2.color('white')
stamp2.tilt(45)
stamps.append(stamp2)

stamp = Turtle('square', visible=False)
stamp.shapesize(3 * BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)

stamp4 = Turtle()
stamp4.setx(BOX / 5)
stamp4.right(45)
stamp4.begin_poly()
for index in range(4):
    stamp4.forward(STEP / 2)
    stamp4.right(90)
    stamp4.forward(STEP)
    stamp4.right(90)
    stamp4.forward(STEP / 2)
    stamp4.left(90)
stamp4.end_poly()
polygon = stamp4.get_poly()
screen.addshape('stamp4', polygon)

stamp4.reset()
stamp4.shape('stamp4')
stamp4.color('white')
stamps.append(stamp4)

stamp = Turtle('square', visible=False)
stamp.shapesize(BOX / 5 / STAMP_SIZE)
stamp.color('black')
stamps.append(stamp)

for stamp in stamps:
    stamp.speed('fastest')
    stamp.penup()

parity = True

for x in range(-BOX, BOX + 1, BOX):
    for y in range(-BOX, BOX + 1, BOX):

        stamps[0].fillcolor(['white', 'black'][parity])

        for stamp in stamps:
            stamp.goto(x, y)
            stamp.stamp()

        parity = not parity

for stamp in stamps:
    stamp.hideturtle()

screen.exitonclick()
您会发现这使得代码更简单、更快


非常感谢,但有一件事-,比如。。。当画笔“向上”时,它也在填充?好的,我修复了它,您只是忘记在向上/向下方法之间添加结束/开始;)非常感谢。非常感谢,但有一件事-,比如。。。当画笔“向上”时,它也在填充?好的,我修复了它,您只是忘记在向上/向下方法之间添加结束/开始;)非常感谢。