from random import randrange from graphics import * class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1,self.sides+1) def getValue(self): return self.value def setValue(self,value): self.value = value def main(): w = GraphWin("Playing with two dice",800,600) m = Image(Point(400,100),"two-red-dice.gif") m.draw(w) # Text message for the user T = Text(Point(400,270),"Let's play with rolling two dice\n\ When you are ready press the roll button") T.setStyle("italic") T.setTextColor("blue") T.draw(w) # roll dice button r = Rectangle(Point(365,500),Point(435,530)) r.setFill("green") rT = Text(Point(400,515),"ROLL!") #rT.setStyle("bold") r.draw(w) rT.draw(w) # Exit button Exit = Rectangle(Point(700,550),Point(770,580)) Exit.setFill("green") ExitText = Text(Point(735,565),"EXIT") #rT.setStyle("bold") Exit.draw(w) ExitText.draw(w) # creating instnces of two dice die1 = MSDie(6) # creating an object of class MSDie die2 = MSDie(7) # creating another object of class MSDie # loading up images into the list of images IMG = [] IMG.append(Image(Point(400,400),"die1.gif")) IMG.append(Image(Point(400,400),"die2.gif")) IMG.append(Image(Point(400,400),"die3.gif")) IMG.append(Image(Point(400,400),"die4.gif")) IMG.append(Image(Point(400,400),"die5.gif")) IMG.append(Image(Point(400,400),"die6.gif")) # initially both dice have face 1, let's draw them d1 = IMG[0].clone() d1.move(-55,0) d1.draw(w) d2 = IMG[0].clone() d2.move(55,0) d2.draw(w) answer = "continue" while answer != "exit": p = w.getMouse() # getting coordinates of the mouse click if ( 700 <= p.getX() <= 770 ) and ( 550 <= p.getY() <= 580 ): # if EXIT is clicked answer = "exit" elif ( 365 <= p.getX() <= 435 ) and ( 500 <= p.getY() <= 530 ): # if ROLL! is clicked die1.roll() # rolling the first die i = die1.getValue() # getting its value print("i=",i) die2.roll() # rolling the second die j = die2.getValue() # getting its value print("j=",i) d1.undraw()# removing the previous drawing of this die d1 = IMG[i-1].clone() # getting a clone of appropriate die's face d1.move(-55,0) # moving it to the left d1.draw(w) # drawing d2.undraw() d2 = IMG[j-1].clone() d2.move(55,0) d2.draw(w) else: pass w.close() main()