OOP Workshop

Random Color Button

Adding random color button

class RandomColorButton(CircleButton):
    def getColor(self): 
# Overriding the default behavior of the getColor method in the Button base class
        global colors
        return colors[random.randint(0,len(colors)-1)]

Adding color buttons:

class RandomColorButton(CircleButton):
    def getColor(self): # Overriding the default behaviour of the getColor method in the Button base class
        global colors
        return colors[random.randint(0,len(colors)-1)]

colorButtons = []
selectedColorButton = RandomColorButton("White", -(len(colors) +1) * 30 /2 , 170, 20, 20)
colorButtons.append(selectedColorButton)
for i in range(len(colors)):
    colorButtons.append(CircleButton(colors[i], -(len(colors) +1) * 30 /2 + (i+1)*30, 170, 20, 20))

def updateColorButtons():
    for i in range(len(colorButtons)):
        colorButtons[i].drawTheButton(buttonTurtle)

def anyColorSelected(x,y):
    global selectedColorButton
    for i in range(len(colorButtons)):
        colorB = colorButtons[i]

        if(colorB.isClicked(x,y)):
            print(colorB.buttonColor, "was clicked")
            if(colorB is selectedColorButton):
                return False
            selectedColorButton = colorB
            return True
 
    return False