简体   繁体   English

填充颜色不适用于我的 function(Python 海龟图形)

[英]Fill color is not working for my function (Python turtle graphics)

The fillcolor() is not working at all in this function -- I can not figure out why. fillcolor()在这个 function 中根本不起作用——我不知道为什么。 It worked in all of my other functions:它适用于我的所有其他功能:

from turtle import Turtle

from random import randint

t = Turtle()

def rocks():
    for i in range(5):
        t.penup()
        t.goto(randint(-300,0), randint(-200,0))

        for x in range(40):
            t.pendown()
            t.fillcolor("gray")
            t.fillcolor()
            t.begin_fill()
            t.forward(5)
            t.left(25)
            t.right(27)
            t.forward(5)
            t.right(20)
            t.end_fill()

    t.speed("fastest")

rocks()

The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments.问题是您的循环begin_fill()end_fill() ,这意味着它们正在尝试填充短线段。 You need them around your loop to fill the entire shape:你需要它们围绕你的循环来填充整个形状:

from turtle import Turtle, Screen
from random import randint

def rocks(t):
    t.fillcolor('gray')

    for _ in range(5):
        t.penup()
        t.goto(randint(-300, 0), randint(-200, 0))

        t.begin_fill()
        for _ in range(15):
            t.pendown()
            t.forward(5)
            t.right(2)
            t.forward(5)
            t.right(22)
        t.end_fill()

screen = Screen()

turtle = Turtle()
turtle.speed('fastest')

rocks(turtle)

turtle.hideturtle()
screen.exitonclick()

在此处输入图像描述

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

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