""" ChapterAnimation/buttonSquare.py Click button square alternate canvas colour: blue, red """ from graphics import * # if pmouse over square (p1 - corner) with side = ss def overSquare(pmouse,p1,ss): # p1 is P1 from Rectangle if (p1.getX() <= pmouse.getX() <= p1.getX() + ss and p1.getY() <= pmouse.getY() <= p1.getY() + ss): return True else: return False def main(): win = GraphWin( "Move square", 300, 300 ) side = 40 mysq = Rectangle( Point(10,70), Point(10+side,70+side) ) mysq.setFill( '#ffff00') # yellow button mysq.draw( win ) countPressed = 0 while True: mp = win.getMouse() if overSquare(mp,mysq.getP1(),side): # check if in square countPressed = countPressed + 1 if countPressed % 2 == 1: win.setBackground("red") else: win.setBackground("blue") win.close() main()