隐藏钻石游戏 - 将一颗钻石隐藏在 10 个按钮之一后面

问题描述 投票:0回答:1

我目前正在制作一款名为“隐藏钻石”的游戏,它由 10 个按钮组成,一旦按下隐藏钻石按钮,钻石就会被放置在其中一个随机数字按钮后面。我以为我已经到了游戏运行的地步,但是,有些东西导致它无法打开。我已将其范围缩小到代码底部的事件处理程序的激活。如果我将所有这些代码都标记出来,那么程序会打开,但显然游戏不会运行。有任何想法吗 ?谢谢你。

# Find the diamond game / Importing the correct imports
from tkinter import *
import tkinter.messagebox as msgBox
import sys
import random

diamond = 0 
guesses = 0

window=Tk()
window.resizable(0,0)
window.title("Find The Diamond")
window.configure(bg = "light sea green")

# Creating the button widgets and changing the appearnace and state.

Box1 = Button(window, text="1", width=10, height=3, bg="red", fg="white", state=DISABLED)
Box1.pack()
Box2 = Button(window, text="2", width=10, height=3, bg="blue", fg="white", state=DISABLED)
Box2.pack()
Box3 = Button(window, text="3", width=10, height=3, bg="gold", fg="white", state=DISABLED)
Box3.pack()
Box4 = Button(window, text="4", width=10, height=3, bg="dark green", fg="white", state=DISABLED)
Box4.pack()
Box5 = Button(window, text="5", width=10, height=3, bg="dark orange", fg="white", state=DISABLED)
Box5.pack()
Box6 = Button(window, text="6", width=10, height=3, bg="dark turquoise", fg="white", state=DISABLED)
Box6.pack()
Box7 = Button(window, text="7", width=10, height=3, bg="brown", fg="white", state=DISABLED)
Box7.pack()
Box8 = Button(window, text="8", width=10, height=3, bg="magenta", fg="white", state=DISABLED)
Box8.pack()
Box9 = Button(window, text="9", width=10, height=3, bg="medium purple", fg="white", state=DISABLED)
Box9.pack()
Box10 = Button(window, text="10", width=10, height=3, bg="lawn green", fg="white", state=DISABLED)
Box10.pack()

DiamondBut = Button(window, text="Hide The Diamond", width=15, height=3, bg="coral", fg="white")
DiamondBut.pack()

InstructionsLab = Label(window, text="Click the Hide The Diamond button to start the game. Then, click on the box where you think the diamond Is\
                        hidden. You have three guesses to find it.", 
                        wraplength=300, justify=LEFT, anchor=W, bg = "light sea green")


# Positioning the Buttons on the grid.

Box1.grid(row=0, column=0, padx=10, pady=20)
Box2.grid(row=0, column=1, padx=10)
Box3.grid(row=0, column=2, padx=10)
Box4.grid(row=0, column=3, padx=10)
Box5.grid(row=0, column=4, padx=10)
Box6.grid(row=1, column=0)
Box7.grid(row=1, column=1)
Box8.grid(row=1, column=2)
Box9.grid(row=1, column=3)
Box10.grid(row=1, column=4)

DiamondBut.grid(row=2, column=0, columnspan=2, sticky=W, padx=10, pady=20)

InstructionsLab.grid(row=2, column=2, columnspan=3, sticky=W, padx=10)


# Create the event Handlers.
def oneC():
    boxNumber = 1
    checkGuess(boxNumber)
def twoC():
    boxNumber = 2
    checkGuess(boxNumber)
def threeC():
    boxNumber = 3
    checkGuess(boxNumber)
def fourC():
    boxNumber = 4
    checkGuess(boxNumber)
def fiveC():
    boxNumber = 5
    checkGuess(boxNumber)
def sixC():
    boxNumber = 6
    checkGuess(boxNumber)
def sevenC():
    boxNumber = 7
    checkGuess(boxNumber)
def eightC():
    boxNumber = 8
    CheckGuess(boxNumber)
def nineC():
    boxNumber = 9
    checkGuess(boxNumber)
def tenC():
    boxNumber = 10
    checkGuess(boxNumber)

# The Check Guess Function.
def checkGuess(boxNumber):
    if boxNumber == diamond:
        yesNo = msgBox.askyesno("You did it! Congratulations", "Would you like to play again?")
        if yesNo == 1:
            hideDiamond()
        else:
            sys.exit()
    else:
        msgBox.showinfo("It's not here", "Sorry, try again.")
        global guesses
        guesses += 1
        if guesses == 3:
            msgBox.showinfo("No more guesses..", \
            "You ran out of guesses.\nThe diamond was in box number " \
            + str(diamond) + ".")
            yesNo = msgBox.askyesno("Play again?", "Would you like to play again?")
            if yesNo == 1:
                hideDiamond()
            else:
                sys.exit()
            
def hideDiamond():
    global guesses
    guesses = 0
    global diamond
    diamond = int(random.randint(1, 10))
    msgBox.showinfo("The Diamond has been hidden!.. Good Luck.")
    Box1.configure(state=NORMAL)
    Box2.configure(state=NORMAL)
    Box3.configure(state=NORMAL)
    Box4.configure(state=NORMAL)
    Box5.configure(state=NORMAL)
    Box6.configure(state=NORMAL)
    Box7.configure(state=NORMAL)
    Box8.configure(state=NORMAL)
    Box9.configure(state=NORMAL)
    Box10.configure(state=NORMAL)
    DiamondBut.configure(state=DISABLED)

# Activate the Event Handlers
Box1.configure(command=onec)
Box2.configure(command=twoC)
Box3.configure(command=threeC)
Box4.configure(command=fourC)
Box5.configure(command=fiveC)
Box6.configure(command=sixC)
Box7.configure(command=sevenC)
Box8.configure(command=eightC)
Box9.configure(command=nineC)
Box10.configure(command=tenC)
DiamondBut.configure(command=hideDiamond)

window.mainloop()
python python-3.x windows user-interface button
1个回答
0
投票

我找到了我的程序无法运行的原因..它应该是command=oneC,但我有command=onec。 c 不是大写字母。我看这段代码很久了 lmao

© www.soinside.com 2019 - 2024. All rights reserved.