OOP Workshop
Circle Button
class CircleButton(Button):
def __init__(self, buttonColor, buttonX, buttonY, width, height):
super().__init__("Circle", buttonColor, buttonX, buttonY, width, height)
def drawCircle(self, aTurtle, radius, heading):
aTurtle.setheading(heading)
aTurtle.circle(radius)
def drawButtonShape(self, aTurtle):
aTurtle.pensize(2)
aTurtle.penup()
aTurtle.setheading(0)
aTurtle.forward(self.width/2)
aTurtle.pendown()
self.drawCircle(aTurtle, self.width /2,0)
def drawShape(self, x, y, aTurtle):
sign = 1 if x-aTurtle.xcor() > 0 else -1
self.drawCircle(aTurtle, \
(sign) * math.sqrt((x-aTurtle.xcor())**2 + (y-aTurtle.ycor())**2) /2 , \
math.degrees(math.atan((y-aTurtle.ycor())/(x- aTurtle.xcor()))) -90)
Adding a circle button:
buttonTurtle = turtle.Turtle()
buttonTurtle.speed(20)
selectedButton = Button("Line","Orange", -130, 220, 40, 20)
shapeButtons = []
shapeButtons.append(selectedButton)
shapeButtons.append(RectangleButton("Green", -70, 200, 100, 40))
shapeButtons.append(TriangleButton("Blue", 40, 200, 40, 40))
shapeButtons.append(CircleButton("Brown", 90, 200, 40, 40))