Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python program: Construct a base class named shape that will maintain common \"s

ID: 3689453 • Letter: P

Question

Python program:

Construct a base class named shape that will maintain common "shape" information such as location, fill color, etc. The shape class should have instance variables for the x and y location of the shape (type int), the fill color (type str) and a boolean indicating if the shape should be filled or not. The location should default to 0,0. The fill color should default to "" (Null string) and filled should default to False. Provide the following shape methods:

• setFillcolor(str) : Mutator to set the fill color to the value of the string argument

• setFilled(bool) : Mutator to set the filled boolean to the value of the boolean argument

• isFilled() : Accessor that will return the value of the filled instance variable

Explanation / Answer

RandomColors.py

## Stretch
import turtle
import random

myt = circle.draw()
scr = turtle.Screen()
scr.delay(0)
myt.speed(0)
myt.hideturtle()
colors = ["red", "yellow", "green", "blue", "purple", "orange"]

def randColor():
    idx = random.randint(0, 5)
    return colors[idx]

class shape():
    def __init__(self, x=0, y=0, istr = "", ibool = False):
        self.__xloc = x
        self.__yloc = y
        self.__Fillcolor = istr
        self.__Filled = ibool
      
    def setFillcolor(self, istr):
        self.__Fillcolor = istr

    def setFilled(self, ibool):
        self.__Filled = ibool

    def isFilled(self):
        return str(self.__Filled)


class circle(shape):
    def __init__(self, x=0, y=0, r=1):
        super().__init__()
        self.__radius = r

    def draw(turtle):
        super().setFilled(randColor)
        turtle.penup()
        turtle.goto(super().__init__(x, y))
        turtle.pendown()
        if turtle.isFilled(self) == 'True':
            turtle.fillcolor(super().setFillcolor())
            turtle.begin_fill()
            turtle.circle(self.__radius)
            turtle.end_fill()
        else:
            turtle.circle(self.__radius)


      
workout.py
## Workout

import turtle
import random
import math

colors = ["red", "yellow", "green", "blue", "purple", "orange", "navy"]

def randColor():
    idx = random.randint(0, 6)
    return colors[idx]

class shape():
    def __init__(self, x=0, y=0, istr = "", ibool = False):
        self.xloc = x
        self.yloc = y
        self.Fillcolor = istr
        self.Filled = ibool
      
    def setFillcolor(self, istr):
        self.Fillcolor = istr

    def setFilled(self, ibool):
        self.Filled = ibool

    def isFilled(self):
        return self.Filled

class circle(shape):
    def __init__(self, x=0, y=0, r=1):
        super().__init__(x, y)
        self.radius = r

    def draw(self, myt):
        myt.penup()
        myt.goto(self.xloc, self.yloc)
        myt.pendown()
        if self.isFilled():
            myt.fillcolor(self.Fillcolor)
            myt.begin_fill()
            myt.circle(self.radius)
            myt.end_fill()
        else:
            myt.circle(self.radius)

##    def isIN(self, x, y):
##        self.xloc
##        self.yloc
      

class rectangle(shape):
    def __init__(self, x=0, y=0, w=1, h=1):
        super().__init__(x, y)
        self.width = w
        self.height = h

    def draw(self, myt):
        myt.penup()
        myt.goto(self.xloc, self.yloc)
        myt.pendown()
        if self.isFilled():
            myt.fillcolor(self.Fillcolor)
            myt.begin_fill()
            for i in range(2):
                myt.forward(self.width)
                myt.left(90)
                myt.forward(self.height)
                myt.left(90)
            myt.end_fill()
        else:
            for i in range(2):
                myt.forward(self.width)
                myt.left(90)
                myt.forward(self.height)
                myt.left(90)

##    def isIN(self, x, y):
      

class Display():
    def __init__(self, myt=turtle.Turtle(), scr=turtle.Screen()):
        self.element = []
        self.myt = myt
        self.scr = scr
        self.scr.delay(0)
        self.myt.speed(0)
        self.myt.hideturtle()
        self.scr.onclick(self.mouseEvent)
        self.scr.listen()

    def mouseEvent(self, x, y):
        lst = self.element
        print(lst)
        deter = random.randint(0,100)
        if deter > 50:
            a = circle(x, y, random.randint(10, 100))
        else:
            a = rectangle(x, y, random.randint(50, 100), random.randint(50, 100))
        a.setFillcolor(randColor())
        a.setFilled(True)
        lst += [a]
##        for i in self.element:
##            if a.isIN == True:
##                lst.remove(a)
##            else:
##                lst.add(a)
##        for i in self.element:
##            i.draw(self.myt)
        a.draw(self.myt)
      
      
    def add(self, shape):
        self.element.append(self.shape)

    def remove(self, shape):
        self.element.remove(self.shape)
      
      
Display()