I'm trying to fill a shape with a color but when I run it, it does not show. Am I not supposed to use classes for this? I am not proficient with python-3 and still learning how to use classes
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()
Issues with your program: you create and set the speed of a turtle that you don't actually use; turtle.py already has a shape() method so don't override it to mean something else, pick a new name; you don't want the begin_fill() and end_fill() inside the loop but rather surrounding the loop; you call your own shape() method with invalid arguments.
The following rework of your code addresses the above issues:
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()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With