我正在尝试使用Zelle 图形模块在 python 中制作游戏。(它必须是 Zelle 图形)。该程序绘制随机数量的面孔,我绘制了带有文本的按钮。我是一个完整的初学者,如果这真的很明显,我很抱歉。
我希望用户能够选择一个按钮并让该按钮成为他们的答案,但我不太清楚如何将按钮与一个数字相关联,然后让该数字成为分配分数的正确答案。没有错误,但按钮无法按预期工作。如何使按钮与数字答案相关联?
def isBetween(x, end1, end2):
return end1 <= x <= end2 or end2 <= x <= end1
def isInside(point, rect):
pt1 = rect.getP1()
pt2 = rect.getP2()
return isBetween(point.getX(), pt1.getX(), pt2.getX()) and \
isBetween(point.getY(), pt1.getY(), pt2.getY())
def makeColoredRect(corner, width, height, color, win):
corner2 = corner.clone()
corner2.move(width, -height)
rect = Rectangle(corner, corner2)
rect.setFill(color)
rect.draw(win)
return rect
def getChoice(choicePairs, default, win):
point = win.getMouse()
for (rectangle, choice) in choicePairs:
if isInside(point, rectangle):
return choice
return default
def makeButtonSetup(colors):
buttons = list()
x = 810
y = 350
for color in colors:
buttons.append((x, y, color))
y = y - 40
return buttons
choicePairs = list()
buttonSetup = makeButtonSetup(['red', 'green', 'blue', 'purple', 'orange', 'yellow'])
for (x, y, color) in buttonSetup:
button = makeColoredRect(Point(x, y), 80, 30, color, win)
choicePairs.append((button, color))
answerPairs = [(1, 'red'), (2, 'green'), (3, 'blue'), (4, 'purple'), (5, 'orange'), (6, 'yellow')]
redText = Text(Point(850, 335), "One Face")
redText.setSize(10)
redText.draw(win)
greenText = Text(Point(850, 295), "Two Faces")
greenText.setSize(10)
greenText.draw(win)
blueText = Text(Point(850, 255), "Three Faces")
blueText.setSize(10)
blueText.draw(win)
purpleText = Text(Point(850, 215), "Four Faces")
purpleText.setSize(10)
purpleText.draw(win)
orangeText = Text(Point(850, 175), "Five Faces")
orangeText.setSize(10)
orangeText.draw(win)
yellowText = Text(Point(850, 135), "Six Faces")
yellowText.setSize(10)
yellowText.draw(win)
numFaces = randint(1, 6)
print(numFaces)
points = 0
correctAnswer = True
def correct(points):
if correctAnswer == True:
points = points+10
print(points)
if numFaces == 1:
1 == True
if numFaces == 2:
2 == True
if numFaces == 3:
3 == True
if numFaces == 4:
4 == True
if numFaces == 5:
5 == True
if numFaces == 6:
6 == True
else:
correctAnswer == False
def incorrectAnswer(points):
correctAnswer = False
if correctAnswer == False:
points = 0
print(points)
correct(points)
robots = []
for n in range(numFaces):
robots.append(makeRobotFace())
i = 0
for robotList in robots:
for robotPart in robotList:
robotPart.draw(win)
robotPart.move(i * 100, 0)
i = i + 1
time.sleep(5)
i = 0
for robotList in robots:
for robotPart in robotList:
robotPart.undraw()
i = i + 1
shapePairs = [(1, 'redText'), (2, 'greenText'), (3, 'blueText'), (4, 'purpleText'), (5, 'orangeText'), (6, 'yellowText')]
msg = Text(Point(700, 400),'')
msg.draw(win)
for (shape, description) in shapePairs:
time.sleep(.2)
prompt = 'Click to choose an answer'
msg.setText(prompt)
answer = getChoice(choicePairs, shapePairs, win)