0

プロジェクトに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)
4

1 に答える 1