在蟒蛇龟中填充颜色的形状

时间:2017-11-13 15:22:15

标签: python-3.x turtle-graphics

我正在尝试使用颜色填充形状,但是当我运行它时,它没有显示。 我不应该为此使用课程吗?我不熟悉python-3,仍然学习如何使用类

import turtle

t=turtle.Turtle()
t.speed(0)


class Star(turtle.Turtle):
    def __init__(self, x=0, y=0):
        turtle.Turtle.__init__(self)
        self.shape("")
        self.color("")
#Creates the star shape    
    def shape(self, x=0, y=0):
        self.fillcolor("red")
        for i in range(9):
        self.begin_fill()
        self.left(90)
        self.forward(90)
        self.right(130)
        self.forward(90)
        self.end_fill()
#I was hoping this would fill the inside        
    def octagon(self, x=0.0, y=0.0):
        turtle.Turtle.__init__(self)

    def octa(self): 
        self.fillcolor("green")
        self.begin_fill()
        self.left(25)
        for x in range(9):
            self.forward(77)
            self.right(40)

#doesn't run with out this
a=Star()

1 个答案:

答案 0 :(得分:1)

你的程序问题:你创建和设置你实际上没有使用的乌龟的速度; turtle.py已经有shape()方法,所以不要将它覆盖为其他东西,选择一个新名称;你不希望循环中有begin_fill()end_fill(),而是围绕循环;用无效的参数调用自己的shape()方法。

以下代码修改解决了上述问题:

from turtle import Turtle, Screen

class Star(Turtle):
    def __init__(self, x=0, y=0):
        super().__init__(visible=False)
        self.speed('fastest')
        self.draw_star(x, y)

    def draw_star(self, x=0, y=0):
        """ Creates the star shape """

        self.penup()
        self.setposition(x, y)
        self.pendown()

        self.fillcolor("red")

        self.begin_fill()

        for _ in range(9):
            self.left(90)
            self.forward(90)
            self.right(130)
            self.forward(90)

        self.end_fill()

t = Star()

screen = Screen()
screen.exitonclick()

enter image description here