填充蟒蛇中不寻常的形状

发布于 2025-01-11 09:03:58 字数 1489 浏览 0 评论 0原文

我正在尝试用海龟制作迪士尼介绍,但是城堡的这一部分不想正确填充,我不明白为什么。

这是代码:

import turtle

t1 = turtle.Turtle()
t2 = turtle.Turtle()

background_color = "#1b3f9f"
castle_color = "#78b3d3"

def goto_168_64():
    t1.penup()
    t2.penup()
    t1.goto(168,64)
    t2.goto(-168,64)
    t1.pendown()
    t2.pendown()

def background():
    t1.hideturtle()
    t2.hideturtle()
    turtle.bgcolor(background_color)

def draw():
    goto_168_64()
    t1.fillcolor(castle_color)
    t2.fillcolor(castle_color)
    t1.begin_fill()
    t2.begin_fill()
    t1.goto(40,64)
    t2.goto(-40,64)
    t1.setheading(90)
    t2.setheading(90)
    for x in range(23):
        t1.forward(2)
        t2.forward(2)
        t1.left(4)
        t2.right(4)
    t1.setheading(180)
    t2.setheading(0)
    t1.forward(25)
    t2.forward(25)
    goto_168_64()
    t1.setheading(180)
    t2.setheading(0)
    for x in range(23):
        t1.forward(2)
        t2.forward(2)
        t1.right(4)
        t2.left(4)
    t1.setheading(90)
    t2.setheading(90)
    t1.forward(10)
    t2.forward(10)
    t1.setheading(180)
    t2.setheading(0)
    t1.forward(140)
    t2.forward(140)
    t1.penup()
    t2.penup()
    t1.goto(0,103)
    t2.goto(0,103)
    t1.pendown()
    t2.pendown()
    t1.goto(0,93)
    t2.goto(0,93)
    t1.end_fill()
    t2.end_fill()

background()
draw()
turtle.mainloop()

这是输出的图片:

output

I am trying to make the Disney intro in turtle, but this part of the castle doesn't want to fill properly and I don't understand why.

Here is the code:

import turtle

t1 = turtle.Turtle()
t2 = turtle.Turtle()

background_color = "#1b3f9f"
castle_color = "#78b3d3"

def goto_168_64():
    t1.penup()
    t2.penup()
    t1.goto(168,64)
    t2.goto(-168,64)
    t1.pendown()
    t2.pendown()

def background():
    t1.hideturtle()
    t2.hideturtle()
    turtle.bgcolor(background_color)

def draw():
    goto_168_64()
    t1.fillcolor(castle_color)
    t2.fillcolor(castle_color)
    t1.begin_fill()
    t2.begin_fill()
    t1.goto(40,64)
    t2.goto(-40,64)
    t1.setheading(90)
    t2.setheading(90)
    for x in range(23):
        t1.forward(2)
        t2.forward(2)
        t1.left(4)
        t2.right(4)
    t1.setheading(180)
    t2.setheading(0)
    t1.forward(25)
    t2.forward(25)
    goto_168_64()
    t1.setheading(180)
    t2.setheading(0)
    for x in range(23):
        t1.forward(2)
        t2.forward(2)
        t1.right(4)
        t2.left(4)
    t1.setheading(90)
    t2.setheading(90)
    t1.forward(10)
    t2.forward(10)
    t1.setheading(180)
    t2.setheading(0)
    t1.forward(140)
    t2.forward(140)
    t1.penup()
    t2.penup()
    t1.goto(0,103)
    t2.goto(0,103)
    t1.pendown()
    t2.pendown()
    t1.goto(0,93)
    t2.goto(0,93)
    t1.end_fill()
    t2.end_fill()

background()
draw()
turtle.mainloop()

Here is a picture of the output:

output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笛声青案梦长安 2025-01-18 09:03:58

如果我们将其视为单个多边形,您可能可以用更少的一只乌龟和十几行代码来获得您想要的东西:

from turtle import Screen, Turtle

background_color = "#1b3f9f"
castle_color = "#78b3d3"

def draw():
    t1.penup()
    t1.goto(168, 64)
    t1.pendown()

    t1.begin_fill()

    t1.setheading(180)
    t1.forward(128)
    t1.setheading(90)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(180)
    t1.forward(11)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(180)
    t1.forward(128)
    t1.setheading(0)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(90)
    t1.forward(10)
    t1.setheading(0)
    t1.forward(264)
    t1.setheading(270)
    t1.forward(10)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.end_fill()

screen = Screen()
screen.bgcolor(background_color)

t1 = Turtle()
t1.hideturtle()
t1.fillcolor(castle_color)

draw()

screen.mainloop()

output

您可以通过使用变量 (或变量)用于相关绘图方向并在循环中重复一半代码。

You might be able to get what you want with one less turtle and a dozen fewer lines of code if we think of this as a single polygon:

from turtle import Screen, Turtle

background_color = "#1b3f9f"
castle_color = "#78b3d3"

def draw():
    t1.penup()
    t1.goto(168, 64)
    t1.pendown()

    t1.begin_fill()

    t1.setheading(180)
    t1.forward(128)
    t1.setheading(90)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(180)
    t1.forward(11)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(180)
    t1.forward(128)
    t1.setheading(0)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.setheading(90)
    t1.forward(10)
    t1.setheading(0)
    t1.forward(264)
    t1.setheading(270)
    t1.forward(10)

    for _ in range(23):
        t1.forward(2)
        t1.left(4)

    t1.end_fill()

screen = Screen()
screen.bgcolor(background_color)

t1 = Turtle()
t1.hideturtle()
t1.fillcolor(castle_color)

draw()

screen.mainloop()

output

You might be able to reduce the above code further by using a variable (or variables) for the relative drawing direction and repeating half the code in loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文