programming in python using graphics.py.(from graphics import*) create the follo
ID: 3568287 • Letter: P
Question
programming in python using graphics.py.(from graphics import*) create the following graphics dice program. The user will be "throwing" 5 dice. Then they will "throw" each of the 5 dice by clicking on their respective outlined areas. When clicked/thrown the dice will appear in the outlined area. You will need to randomly generatePreview the documentView in a new window a number between 1-6 for each click/throw. After each click/throw the new dice total will appear in the bottom left of the window. After all the dice have been clicked/thrown you will display a message below the dice total remarking on how lucky they are. Have an Exit button the user can click to exit the program.
Notes:
You can assume that the dice are clicked/thrown in order of 1 to 5.
If the user clicks outside of the dice outlines nothing should happen, no dice display, no error message.
You need to use a function that is called with every dice click/throw:
Explanation / Answer
def dice_graph():
#import random library
import random
# this generates a random integer between and including 1 and 6
intNum = random.randint(1, 6)
return intNum
def dot():
intNum = dice_graph()
if intNum == 1:
#draw the main dot in the center of the die
circDot4 = Circle(Point(75, 75), 5).setFill('black')
circDot4.draw(win)
if intNum == 2:
circDot2 = Circle(Point(50, 75), 5).setFill('black')
circDot2.draw(win)
circDot6 = dot().clone()
circDot6.move(50, 0)
circDot6.draw
if intNum == 3:
circDot3 = Circle(Point(50, 100), 5).setFill('black')
circDot3.draw(win)
circDot4 = Circle(Point(75, 75), 5).setFill('black')
circDot4.draw(win)
circDot5 = Circle(Point(100, 50), 5).setFill('black')
circDot5.draw(win)
if intNum == 4:
circDot1 = Circle(Point(50, 50), 5).setFill('black')
circDot1.draw(win)
circDot3 = Circle(Point(50, 100), 5).setFill('black')
circDot3.draw(win)
circDot5 = Circle(Point(100, 50), 5).setFill('black')
circDot5.draw(win)
circDot7 = Circle(Point(100, 100), 5).setFill('black')
circDot7.draw(win)
if intNum == 5:
circDot1 = Circle(Point(50, 50), 5).setFill('black')
circDot1.draw(win)
circDot3 = Circle(Point(50, 100), 5).setFill('black')
circDot3.draw(win)
circDot5 = Circle(Point(100, 50), 5).setFill('black')
circDot5.draw(win)
circDot7 = Circle(Point(100, 100), 5).setFill('black')
circDot7.draw(win)
circDot4 = Circle(Point(75, 75), 5).setFill('black')
circDot4.draw(win)
if intNum == 6:
circDot1 = Circle(Point(50, 50), 5).setFill('black')
circDot1.draw(win)
circDot3 = Circle(Point(50, 100), 5).setFill('black')
circDot3.draw(win)
circDot5 = Circle(Point(100, 50), 5).setFill('black')
circDot5.draw(win)
circDot7 = Circle(Point(100, 100), 5).setFill('black')
circDot7.draw(win)
circDot2 = Circle(Point(50, 75), 5).setFill('black')
circDot2.draw(win)
circDot6 = dot().clone()
circDot6.move(50, 0)
circDot6.draw
def dice(win):
#make a square to represent the die and draw it
sqrDice = Rectangle(Point(25, 25), Point(125,125))
sqrDice.setFill('snow')
sqrDice.setOutline('snow')
sqrDice.draw(win)
dot()
def main():
win = GraphWin('Dice', 650, 400)
#papayawhip
win.setBackground('papayawhip')
blnRun = True
Box1 = Rectangle(Point(25, 25), Point(125, 125))
Box1.setFill('ivory')
Box1.draw(win)
Box2 = Rectangle(Point(150, 25), Point(250, 125))
Box2.setFill('ivory')
Box2.draw(win)
Box3 = Rectangle(Point(275, 25), Point(375, 125))
Box3.setFill('ivory')
Box3.draw(win)
Box4 = Rectangle(Point(400, 25), Point(500, 125))
Box4.setFill('ivory')
Box4.draw(win)
Box5 = Rectangle(Point(525, 25), Point(625, 125))
Box5.setFill('ivory')
Box5.draw(win)
while blnRun == True:
#get the x and y coordinates of a click
ClickXY = win.getMouse()
xPoint = ClickXY.getX()
yPoint = ClickXY.getY()
#if they clicked within the button, call the dice function
if xPoint > 25 and xPoint < 125 and yPoint > 25 and yPoint < 125:
dice(win)
elif xPoint > 150 and xPoint < 250 and yPoint > 25 and yPoint < 125:
dice(win)
elif xPoint > 275 and xPoint < 375 and yPoint > 25 and yPoint < 125:
dice(win)
elif xPoint > 400 and xPoint < 500 and yPoint > 25 and yPoint < 125:
dice(win)
elif xPoint > 525 and xPoint < 625 and yPoint > 25 and yPoint < 125:
dice(win)
else:
Text(Point(200,325),"Click on a die").draw(win)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.