我正致力于创造一个三门蒙特"程序使用graphic.py库,但我无法弄清楚我做错了导致图形窗口无法加载。有什么建议或提示吗?知道我的运气,我的错误是一个基本的错误
from random import randrange
from graphics import *
class Door:
def __init__(self,win,center,width,height,label):
#mark secret door
# 0 = not this door
# 1 = this is scret door
self.secret = 0
self.hasClick = 0
#get X,Y from center and find border of the door
x,y = center.getX(),center.getY()
self.xmin, self.xmax = x-width/2,x+width/2
self.ymin, self.ymax = y-height/2,y+height/2
#draw a door border
self.rec = Rectangle(Point(self.xmin,self.ymax),Point(self.xmax,self.ymin))
self.rec.setFill(color_rgb(250,216,21))
self.rec.setWidth(0)
self.rec.draw(win)
#draw doorknob
self.knob = Circle(Point(x+(width/3.2),y),width/12)
self.knob.setFill(color_rgb(255,255,0))
self.knob.setWidth(0)
self.knob.draw(win)
#draw label
self.label= Rectangle(Point(x-(width/2.8),y+(height/2.4)),Point(x+(width/3),y++(height/3.5)))
self.label.setFill(color_rgb(227,219,178))
self.label.setWidth(0)
self.label.draw(win)
#draw text on label
self.text = Text(Point(x,y+(height/2.8)),label)
self.text.setSize(10)
self.text.draw(win)
def setSecret(self):
#set door as the secret door
self.secret = 1
print()
def turnGreen(self):
#Turn door color green to present this is a correct door.
self.rec.setFill(color_rgb(79,232,17))
self.knob.setFill(color_rgb(51,176,0))
self.label1.setFill(color_rgb(51,176,0))
def turnRed(self):
#Turn door red if user click on wrong door.
self.rec.setFill(color_rgb(241,41,41))
self.knob.setFill(color_rgb(254,40,56))
self.label.setFill(color_rgb(254,40,56))
def clicked(self,c):
if self.xmin <= c.getX() <= self.xmax and self.ymin <= c.getY() <= self.ymax:
self.hasClick = 1
return (self.xmin <= c.getX() <= self.xmax and
self.ymin <= c.getY() <= self.ymax)
def reveal(self):
#if user click on a correct door
if self.secret == 1 and self.hasClick == 1:
self.turnGreen()
return 'win'
#if user click on wrong door turn it to red
elif self.secret == 0 and self.hasClick == 1:
self.turnRed()
#reveal secret door if necessary
elif self.secret ==1 and self.hasClick ==0:
self.turnGreen()
return 'secret'
def main():
win,msg,d1,d2,d3 = drawGUI()
secretDoor(d1,d2,d3)
getInput(win,d1,d2,d3)
result(msg,d1,d2,d3)
exit(win)
def drawGUI():
#setup windows
win = GraphWin('Three Door Monte',390,300)
win.setCoords(0,0,130,100)
win.setBackground('white')
#draw doors
d1 = Door(win,Point(25,60),30,53,'Door 1')
d2 = Door(win,Point(65,60),30,53,'Door 2')
d3 = Door(win,Point(105,60),30,53,'Door 3')
#create title of program
title = Text(Point(65,95),'Three Door Monte')
title.setSize(17)
title.draw(win)
#draw box to display messages
msgBox = Rectangle(Point(10,25),Point(120,9))
msgBox.setFill('yellow3')
msgBox.setWidth(0)
msgBox.draw(win)
#draw message to invite user
msg = Text(Point(65,17),'Guess which is the winning door')
msg.setSize(13)
msg.draw(win)
return win,msg,d1,d2,d3
def secretDoor(d1,d2,d3):
i = randrange(1,4)
if i == 1: d1.setSecret()
elif i == 2 : d2.setSecret()
else: d3.setSecret()
def getInput(win,d1,d2,d3):
#wait for user to select a door
p = win.getMouse()
while not d1.clicked(p) and not d2.clicked(p) and not d3.clicked(p):
p = win.getMouse()
def result(msg,d1,d2,d3):
#display the results
#door reveals whether green or blue
x = d1.reveal()
y = d2.reveal()
z = d3.reveal()
#display winning message
if x == 'win' or y == 'win' or z == 'win':
msg.setText('You win!\nClick anywhere to close!')
#if user did not pick the winning door
else:
if x == 'secret': wayout = '1'
elif y == 'secret': wayout = '2'
else: wayout = '3'
msg.setText('You lose!!\
\nClick anywhere to close!'.format(wayout))
def exit(win):
win.getMouse()
win.close()