简体   繁体   English

Python龟图形填充问题

[英]Python turtle graphics filling issue

I'm supposed to draw a building for my Python class, and the fillcolor() function does its job for two of the shapes, but for the last shape, even though I finish the circuit so to speak, it won't fill it with the color I need: 我应该为我的Python类绘制一个建筑物,并且fillcolor()函数针对两种形状完成其工作,但是对于最后一个形状,即使我完成了电路,可以说也无法填充我需要的颜色:

import turtle

def main():
    turtle.setup(900, 900)
    cityscape(-300, -400, 300, 'gray')
    cityscape(0, -400, 300, 'gray')
    building(-300, -100, 'gray')
    turtle.speed(0)

def cityscape(x, y, width, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pencolor('gray')
    turtle.fillcolor(color)
    turtle.pendown()
    turtle.begin_fill()
    for count in range(4):
        turtle.forward(width)
        turtle.left(90)
    turtle.end_fill()

def building(x, y, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.fillcolor(color)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.right(180)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(200)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(250)
    turtle.left(90)
    turtle.forward(70)
    turtle.left(90)
    turtle.forward(175)
    turtle.right(90)
    turtle.forward(80)
    turtle.right(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(70)
    turtle.right(90)
    turtle.forward(425)
    turtle.right(90)
    turtle.forward(425)


main()

How can I resolve this? 我该如何解决?

You left turtle.end_fill() off the end of your building() function. 您将turtle.end_fill()building()函数的末尾移开了。 I'd also write main() as follows: 我还将编写main()如下:

def main():
    turtle.setup(900, 900)
    turtle.speed('fastest')
    cityscape(-300, -400, 300, 'gray')
    cityscape(0, -400, 300, 'gray')
    building(-300, -100, 'gray')
    turtle.mainloop()

Complete code listing with the above fix and some style changes: 使用上述修复程序和一些样式更改的完整代码清单:

from turtle import Screen, Turtle

def main():
    screen = Screen()
    screen.setup(900, 900)

    yertle = Turtle(visible=False)
    yertle.speed('fastest')

    cityscape(yertle, -300, -400, 300, 'gray')
    cityscape(yertle, 0, -400, 300, 'gray')
    building(yertle, -300, -100, 'gray')

    screen.mainloop()

def cityscape(turtle, x, y, width, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    turtle.color(color)

    turtle.begin_fill()
    for _ in range(4):
        turtle.forward(width)
        turtle.left(90)
    turtle.end_fill()

def building(turtle, x, y, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    turtle.color(color)

    turtle.begin_fill()
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(200)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(250)
    turtle.left(90)
    turtle.forward(70)
    turtle.left(90)
    turtle.forward(175)
    turtle.right(90)
    turtle.forward(80)
    turtle.right(90)
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(70)
    turtle.right(90)
    turtle.forward(425)
    turtle.right(90)
    turtle.forward(425)
    turtle.end_fill()

main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM