谁能告诉我如何 return 开始游戏?
Can someone tell me how to return to start of game?
这是我编写的游戏。
这是一个猜字游戏。
from random import randint
from tkinter import *
def umm():
continue
def quit():
master.destroy()
answer = randint(0,16)
wrong_answer = randint(0,16)
wrong_answer1 = randint(0,16)
s_list = ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
secret_word = s_list[answer]
hint = f'Hint:{hint_list[answer]}'
incorrect = s_list[wrong_answer]
incorrect1 = s_list[wrong_answer1]
master = Tk()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')
def random1():
label["text"] = "Player 1 won!"
def random2():
label["text"] = "Player 2 won!"
def random3():
label["text"] = "Player 2 won!"
def random4():
label["text"] = "Player 1 won!"
label = Label(master, text="2 Player Guessing Game!", font = "Arial 14")
label2 = Label(master, text="<- P1 P2 ->", font = "Arial 14")
button = Button(master, text=secret_word, font = "Arial 14", command=random1)
button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, text=hint, font = "Arial 14")
button7 = Button(master, text="Again?", font = "Arial 14", command = umm)
button8 = Button(master, text="Quit", font = "Arial 14", command = quit)
label.pack()
label2.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)
button7.pack()
button8.pack()
master.mainloop()
目前,我的代码显示,如果玩家 1 单击他或她这一边的正确按钮,它将打印玩家 1 获胜,反之亦然。现在,我添加了额外的按钮,"Again?" 和 "Quit"。
我还添加了 "continue"。但是,它现在说 continue 没有正确地循环。我认为因为它在 mainloop() 中,这就是我可以使用它的原因。但它现在显示消息。为什么?
两条建议:
- 你可以通过
random.choice()
得到这些词,它可能更容易。
- 不建议使用
from tkinter import *
。
just like reset the whole code.
在您的应用程序中,将 means:change the Player x won!
重置为 2 Player Guessing Game!
。并使用 random.choice()
生成一个新字符串并将其显示在按钮中。
代码:
from random import choice
from tkinter import *
def generateRandomWord(): # at first I don't know you need to use index.
secret_word.set(choice(s_list))
hint.set(final_dict[secret_word.get()]) # use dict to get the value.
incorrect.set(choice(s_list))
incorrect1.set(choice(s_list))
def umm():
label['text'] = "2 Player Guessing Game!"
generateRandomWord()
def quit():
master.destroy()
master = Tk()
secret_word = StringVar() # use StringVar
incorrect = StringVar()
incorrect1 = StringVar()
hint = StringVar()
s_list = ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
final_dict = dict(zip(s_list,hint_list)) # generate a dict.
generateRandomWord()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')
def random1():
label["text"] = "Player 1 won!"
def random2():
label["text"] = "Player 2 won!"
def random3():
label["text"] = "Player 2 won!"
def random4():
label["text"] = "Player 1 won!"
label = Label(master, text="2 Player Guessing Game!", font = "Arial 14")
label2 = Label(master, text="<- P1 P2 ->", font = "Arial 14")
button = Button(master, textvariable=secret_word, font = "Arial 14", command=random1) # bind textvariable
button2 = Button(master, textvariable=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, textvariable=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, textvariable=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, textvariable=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, textvariable=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, textvariable=hint, font = "Arial 14")
button7 = Button(master, text="Again?", font = "Arial 14", command = umm)
button8 = Button(master, text="Quit", font = "Arial 14", command = quit)
label.pack()
label2.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)
button7.pack()
button8.pack()
master.mainloop()
您也可以使用 list
来保存这些变量,按钮和 labels.That 将减少代码量。
这是我编写的游戏。 这是一个猜字游戏。
from random import randint
from tkinter import *
def umm():
continue
def quit():
master.destroy()
answer = randint(0,16)
wrong_answer = randint(0,16)
wrong_answer1 = randint(0,16)
s_list = ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
secret_word = s_list[answer]
hint = f'Hint:{hint_list[answer]}'
incorrect = s_list[wrong_answer]
incorrect1 = s_list[wrong_answer1]
master = Tk()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')
def random1():
label["text"] = "Player 1 won!"
def random2():
label["text"] = "Player 2 won!"
def random3():
label["text"] = "Player 2 won!"
def random4():
label["text"] = "Player 1 won!"
label = Label(master, text="2 Player Guessing Game!", font = "Arial 14")
label2 = Label(master, text="<- P1 P2 ->", font = "Arial 14")
button = Button(master, text=secret_word, font = "Arial 14", command=random1)
button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, text=hint, font = "Arial 14")
button7 = Button(master, text="Again?", font = "Arial 14", command = umm)
button8 = Button(master, text="Quit", font = "Arial 14", command = quit)
label.pack()
label2.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)
button7.pack()
button8.pack()
master.mainloop()
目前,我的代码显示,如果玩家 1 单击他或她这一边的正确按钮,它将打印玩家 1 获胜,反之亦然。现在,我添加了额外的按钮,"Again?" 和 "Quit"。 我还添加了 "continue"。但是,它现在说 continue 没有正确地循环。我认为因为它在 mainloop() 中,这就是我可以使用它的原因。但它现在显示消息。为什么?
两条建议:
- 你可以通过
random.choice()
得到这些词,它可能更容易。 - 不建议使用
from tkinter import *
。
just like reset the whole code.
在您的应用程序中,将 means:change the Player x won!
重置为 2 Player Guessing Game!
。并使用 random.choice()
生成一个新字符串并将其显示在按钮中。
代码:
from random import choice
from tkinter import *
def generateRandomWord(): # at first I don't know you need to use index.
secret_word.set(choice(s_list))
hint.set(final_dict[secret_word.get()]) # use dict to get the value.
incorrect.set(choice(s_list))
incorrect1.set(choice(s_list))
def umm():
label['text'] = "2 Player Guessing Game!"
generateRandomWord()
def quit():
master.destroy()
master = Tk()
secret_word = StringVar() # use StringVar
incorrect = StringVar()
incorrect1 = StringVar()
hint = StringVar()
s_list = ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
final_dict = dict(zip(s_list,hint_list)) # generate a dict.
generateRandomWord()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')
def random1():
label["text"] = "Player 1 won!"
def random2():
label["text"] = "Player 2 won!"
def random3():
label["text"] = "Player 2 won!"
def random4():
label["text"] = "Player 1 won!"
label = Label(master, text="2 Player Guessing Game!", font = "Arial 14")
label2 = Label(master, text="<- P1 P2 ->", font = "Arial 14")
button = Button(master, textvariable=secret_word, font = "Arial 14", command=random1) # bind textvariable
button2 = Button(master, textvariable=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, textvariable=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, textvariable=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, textvariable=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, textvariable=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, textvariable=hint, font = "Arial 14")
button7 = Button(master, text="Again?", font = "Arial 14", command = umm)
button8 = Button(master, text="Quit", font = "Arial 14", command = quit)
label.pack()
label2.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)
button7.pack()
button8.pack()
master.mainloop()
您也可以使用 list
来保存这些变量,按钮和 labels.That 将减少代码量。