好吧,我正在课堂上编写一个文本冒险游戏,但我在编写按钮机制时遇到了麻烦。我想按下3个选项中的一个按钮,这将导致我进入另一个屏幕,还有3个选择,但我无法做到这一点。 这是我的代码:(抱歉格式化)
from tkinter import *
root = Tk()
class GAME():
def __init__(self, master=None):
self.scene201_button = Button(master, text = "Go to your University", bg = "Blue",fg = "Orange", height = "10", width = "25", command=self.scene201)
self.scene201_button.pack()
self.scene202_button = Button(master, text = "Stay at home and defend yourself", bg = "Red", fg = "Black", height = "10", width = "25", command=self.scene202)
self.scene202_button.pack()
self.scene203_button = Button(master, text = "Scream and run around like an idiot", bg = "Green", fg = "Red", height = "10", width = "25", command=self.scene203)
self.scene203_button.pack()
def intro():
print ("Welcome to Nov Yot! This is a decision based adventure game about an alien attack on the planet Earth in 2050")
def scene100():
print ("It all started one day while you were sitting on your couch watching tv after a long day of work as a professor at an esteemed university. An emergency government broadcast interrupts your favorite episode of Rick and Morty to inform the public that the world is under attack from aliens that will arrive in an estimated 12 hours. You get out your father's old rusty shotgun.")
print ("Would you like to 1. Go to your university, 2. Stay at home and prepare to defend yourself, or 3. Scream and run around like an idiot.")
def scene201(self):
print ('When you arrive at the university the staff of your department has collected in your lab. They look to you as you arrive and another professor says "You are in-charge". You pace around the room of your experimental science lab and, after a few moments, address your friends and colleagues. You all agree that in order to defeat the aliens you must destroy whatever is "in charge". In order to do this by getting to the alien mothership you must destroy the three support ships and most of the drones and shuttle ships. In order to do this you and your new team must gather weapons.')
print ("Do you 1.Decide to use your experiments to create larger weapons and then go to a gun store and take whatever else you need, 2.Decide to go to a military base, take it over, and defend the base with the weapons inside it, or 3. Get as close to the mothership as possible and try to find a peaceful solution to the conflict which satisfies everyone?")
root.destroy()
self.scene311_button = Button(self, text = "lol what up", bg = "Blue",fg = "Orange", height = "10", width = "25", command=self.scene311)
self.scene311_button.pack()
self.scene312_button = Button(self, text = "Stay at home and defend yourself", bg = "Red", fg = "Black", height = "10", width = "25", command=self.scene312)
self.scene312_button.pack()
self.scene313_button = Button(self, text = "Scream and run around like an idiot", bg = "Green", fg = "Red", height = "10", width = "25", command=self.scene313)
self.scene313_button.pack()
def scene202(self):
print ("You use wood you find outside in your shed to board up your doors and windows. You get guns from your safe and are prepared if anything comes.")
print ("Do you 1. Take the fight to the aliens, 2. Wait to be attacked or 3.Randomly fire off shots into your roof and attract a lot of attention to yourself because you dumb")
def scene203(self):
print ("After an hour of being stared at by your neighbors like a crazy person, you calm down enough to realize you must either:")
print ("1. Go to your university or 2.Stay at home and prepare to defend yourself")
def scene311():
print
def scene312():
print ("You are unprepared to attack a surprisingly well defended military base and are killed by the government.")
print ("Game Over")
def scene313():
print ("The aliens kill you before you even get close to them.")
print ("Game Over")
intro()
scene100()
root = Tk()
wait = GAME()
root.mainloop()
答案 0 :(得分:0)
在下面的示例中,有1 + 1 * 2 = 3'场景'每个选择2个,总共2 * 2 = 4个结果。它的结构可能会好得多,但它应该给你一个主要的想法:
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.choices = list()
self.scene = Scene0(self)
self.scene.pack()
def choice_pick(self, buttonID):
self.choices.append(buttonID)
self.scene.destroy()
if len(self.choices) == 1:
if self.choices[0] == 1:
self.scene = Scene1(self)
elif self.choices[0] == 2:
self.scene = Scene2(self)
elif len(self.choices) == 2:
if self.choices[0] == 1:
if self.choices[1] == 1:
self.scene = EndScene1(self)
elif self.choices[1] == 2:
self.scene = EndScene2(self)
elif self.choices[0] == 2:
if self.choices[1] == 1:
self.scene = EndScene3(self)
elif self.choices[1] == 2:
self.scene = EndScene4(self)
self.scene.pack()
class Scene0(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="Dialogue0: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
tk.Button(self, text="Choice 0.1", command=lambda : master.choice_pick(1)).grid(row=1, column=0, sticky="nsew")
tk.Button(self, text="Choice 0.2", command=lambda : master.choice_pick(2)).grid(row=1, column=1, sticky="nsew")
class Scene1(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="Dialogue1: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
tk.Button(self, text="Choice 1.1", command=lambda : master.choice_pick(1)).grid(row=1, column=0, sticky="nsew")
tk.Button(self, text="Choice 1.2", command=lambda : master.choice_pick(2)).grid(row=1, column=1, sticky="nsew")
class Scene2(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="Dialogue2: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
tk.Button(self, text="Choice 2.1", command=lambda : master.choice_pick(1)).grid(row=1, column=0, sticky="nsew")
tk.Button(self, text="Choice 2.2", command=lambda : master.choice_pick(2)).grid(row=1, column=1, sticky="nsew")
class EndScene1(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="EndScene1: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
class EndScene2(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="EndScene2: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
class EndScene3(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="EndScene3: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
class EndScene4(tk.Frame):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="EndScene4: Lorem Ipsum").grid(row=0, column=0, columnspan=2, sticky="nsew")
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()