所以我有多个文件用于执行游戏的不同功能。我遇到问题的文件是我创建的向游戏添加按钮的功能。我将此文件导入另一个需要使用按钮功能的文件,但是当从功能文件中将其明确定义为功能时,该程序崩溃,提示“按钮未定义”。有人知道如何解决这个问题吗?
这是按钮代码:
def Button(Text, X, Y, Width, Height, Inactive_Colour, Active_Colour, Action = None):
Cursor = pg.mouse.get_pos()
Click = pg.mouse.get_pressed()
if X + Width > Cursor[0] > X and Y + Height > Cursor[1] > Y:
pg.draw.rect(Display, Active_Colour, (X, Y, Width, Height))
if Click[0] == 1 and Action != None:
if Action == "Quit":
pg.quit()
quit()
elif Action == "Controls":
Controls()
elif Action == "Play":
Game_Loop()
elif Action == "Main Menu":
Main_Menu()
elif Action == "Objective":
Objective()
else:
pg.draw.rect(Display, Inactive_Colour, (X, Y, Width, Height))
Text_To_Button(Text, Black, X, Y, Width, Height)
这是使用按钮文件中按钮功能的主菜单代码:
import pygame as pg
from Settings import *
from Buttons import *
from Text import *
def Main_Menu():
Main_Menu = True
while Main_Menu:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
Main_Menu = False
elif event.key == pg.K_ESCAPE:
pg.quit()
quit()
Display.fill(White)
Message_To_Screen("RPG", Light_Green, -180, Size = "Large")
# Message_To_Screen("Press Return to play, P to pause, or Escape to quit.", BLACK, 180, Size = "Small")
Button("Play", 25, 500, 150, 50, Dark_Green, Light_Green, Action = "Play")
Button("Controls", 225, 500, 150, 50, Dark_Cyan, Light_Cyan, Action = "Controls")
Button("Objective", 425, 500, 150, 50, Dark_Yellow, Light_Yellow, Action = "Objective")
Button("Quit", 625, 500, 150, 50, Dark_Red, Light_Red, Action = "Quit")
pg.display.update()
Clock.tick(15)
Main_Menu()
当按钮功能位于同一文件中时,该按钮功能有效,但是当我从其他文件导入该按钮功能时,该按钮功能无效,就像当它存在时一样。