python - 谁能告诉我如何回到游戏开始?
问题描述
这是我编写的游戏。这是一个猜词游戏。
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 获胜,反之亦然。现在,我添加了额外的按钮,“再来一次?” 和“退出”。我还添加了“继续”。但是,它现在说 continue 没有正确循环。我认为因为它在 mainloop() 中,这就是我可以使用它的原因。但它现在显示了消息。为什么?
解决方案
两个建议:
- 你可以得到那些词
random.choice()
,它可能会更容易。 - 不建议使用
from tkinter import *
。
就像重置整个代码一样。
在您的应用程序中,重置意味着:更改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()
您也可以使用 alist
来保存这些变量、按钮和标签。这样可以减少代码量。
推荐阅读
- node.js - 如何使用 Nodejs、mongodb 和 express 在对象数组数组中使用计数查询?
- node.js - 会话未存储在护照中
- wordpress - 最大的内容绘画:现场数据和工具之间的差异
- security - 如何为私有 Web 应用实施最小访问控制?
- javascript - 带有chart.js的刻度线和X轴之间的空间
- ios - 如何快速制作嵌套的可折叠表格视图?
- google-cloud-speech - 谷歌云语音到文本不返回标点符号
- php - PHP/JSON-OOCSS:单击任何元素后如何永久修改 CSS 文件?
- react-native - React Native Navigation Library - 隐藏底部选项卡
- discord.js - discord.js中间隔使用的时间单位是什么?