我正在尝试使用项目的Zelle graphics模块在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)
答案 0 :(得分:0)
我切断了部分代码并创建了工作示例
from graphics import *
from random import randint
import graphics
# --- functions ---
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 makeText(text_position, text_string, win):
text = Text(text_position, text_string)
text.setSize(10)
text.draw(win)
return text
def makeButtons(buttonsSetup, win):
buttons = list()
x = 810
y = 350
for answer, color, text_string in buttonsSetup:
# create button - rect & text
rect = makeColoredRect(Point(x, y), 80, 30, color, win)
text = makeText(Point(x+40, y-15), text_string, win)
buttons.append( (answer, rect, text) )
y = y - 40
return buttons
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 getChoice(buttons, default, win):
point = win.getMouse()
for answer, rect, text in buttons:
if isInside(point, rect):
print('selected:', answer, text.getText())
return answer
return default
def checkAnswer(numFaces, answer, points):
print('points before:', points)
if numFaces == answer:
correctAnswer = True
points = points + 10
else:
correctAnswer = False
points = points - 10
print('correct answer:', correctAnswer)
print('points after:', points)
return points, correctAnswer
# --- main ------------------------------------------------
points = 0
correctAnswer = None
buttonsSetup = [
# (answer, color, text_string)
(1, 'red', "One Face"),
(2, 'green', "Two Faces"),
(3, 'blue', "Three Faces"),
(4, 'purple', "Four Faces"),
(5, 'orange', "Five Faces"),
(6, 'yellow', "Six Faces"),
('exit', 'white', "EXIT"),
]
# - start -
win = graphics.GraphWin(width=1000, height=600)
# - objects -
# it create list with elements `(answer, rect, text)`
buttons = makeButtons(buttonsSetup, win)
# select new number of faces
numFaces = randint(1, 6)
print('----')
print('numFaces:', numFaces)
# - mainloop -
while True:
print('----')
# check which button was clicked
answer = getChoice(buttons, '', win)
# exit `while` loop
if answer == 'exit':
break
if answer != '':
# compare selected answer with correct answer and change points
points, correctAnswer = checkAnswer(numFaces, answer, points)
if correctAnswer:
# select new number of faces
numFaces = randint(1, 6)
print('----')
print('numFaces:', numFaces)
# - exit -
win.close()