我试图给一个形状填充颜色,但运行时并没有显示出来。
我不能使用类吗?我对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()
Turtle.__init__(self, visible=False)
替换为super().__init__(visible=False)
这一行。由于您标记了Python-3.x,我使用了更新的语法。 - cdlaneif __name__ == "__main__":
语句将其隔离)。将此代码保存为star.py(或其他名称),然后在您的其他Python文件中执行:from star import Star
。然后,您可以执行t = Star()
来生成一个新的星星。 - cdlane