我已经创建了一个游戏。它的工作完美,所以我想我会添加一个屏幕来改变'子弹'的颜色。我做了窗口我分配变量我globaled我需要的变量,但是我不断收到一个错误说变量没有被定义,但该变量下定义和来自同一个'商店'功能下的其他变量是定义。如果我摆脱了正常,并且只有FireGun和LaserGun FireGun没有定义。很难解释。所以看看我已经非常糟糕的代码,看看你能否挑出任何错误。全局变量不通过,说它不被识别?
from tkinter import *
def MainMenu():
self = Tk()
self.btnShops = Button(text ="Shop")
self.btnShops.pack()
def startShops(event):
self.destroy()
Shop()
self.btnShops.bind('<Button>',startShops)
self.geometry("830x700")
self.title("Space Assault")
self.configure(bg="black")
def Shop():
self = Tk()
normal = 1
fire = 0
laser = 0
def FireGun(event):
global fire
global normal
global laser
fire = 1
normal = 0
laser = 0
def LaserGun(event):
global laser
global fire
global normal
laser = 1
fire = 0
normal = 0
self.btnfireGun = Button(text="Fire Gun")
self.btnfireGun.pack()
self.btnlaser = Button(text = "Laser Gun")
self.btnlaser.pack()
self.btnMainMenu = Button(text="Main Menu")
self.btnMainMenu.pack()
#Bind
self.btnfireGun.bind('<Button>',FireGun)
self.btnlaser.bind('<Button>', LaserGun)
self.btnMainMenu.bind('<Button>',StartMainMenu)
#Frame settings
self.geometry("830x700")
self.title("Space Assault Shop")
self.configure(bg="black")
def singleplayergame():
def Shoot(event):
global normal
global fire
global laser
self.shot = Canvas(bg="black",highlightthickness=0,width=10,height=10)
self.shot.place(x=shotXpos,y=shotYpos)
if normal == 1:
self.shot.create_oval(0,0,10,10,fill=random.choice(COLORS))
elif fire == 1:
self.shot.create_oval(0,0,10,10,fill='red')
elif laser == 1:
self.shot.create_oval(0,0,10,10,fill='blue')
MainMenu()
如果我点击FireGun按钮子弹工作正常,他们出来像红色一样正常。但是,如果我想它'正常'它甚至不会发射任何东西。希望这是有道理的。我认为它与全球减速有关,但我真的坚持这一点。