问题描述
目前正在开发一款最终支持多人游戏的西洋双陆棋游戏。 我有一个正在运行的主菜单函数,它的异常和绘制由 mainloop() 属性处理。但是,我有一个按钮,按下该按钮时,应切换到以西洋双陆棋棋盘为背景的游戏。 图像短暂出现,然后主菜单继续运行。我试图找到使用条件语句禁用菜单循环的方法,但无济于事。 这是完整的源代码:
import sys,pygame,os,pygame_menu
width,height = 1280,960
class mainBackgammonGame():
global width,height
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Bootleg Backgammon")
self.clock = pygame.time.Clock()
def updateScreen(self):
self.clock.tick(60)
pygame.display.flip()
def startGame():
surface = pygame.display.set_mode((width,height))
currPath = os.path.dirname(os.path.abspath(__file__))
boardpath = os.path.join(currPath,"images/mainboard.png")
boardImage = pygame.image.load(boardpath)
surface.blit(boardImage,(0,0))
pygame.display.flip()
def mainMenu():
surface = pygame.display.set_mode((width,height))
menu = pygame_menu.Menu(960,1280,'Welcome to Backgammon',theme = pygame_menu.themes.THEME_DARK)
menu.add_text_input("Your Name: ",default = "Joseph Smith")
menu.add_button('Play',mainBackgammonGame.startGame)
menu.add_button('Quit',pygame_menu.events.EXIT)
menu.mainloop(surface)
bg = mainBackgammonGame()
mainMenu()
while 1:
for event in pygame.event.get():
if event == pygame.QUIT:
exit()
bg.updateScreen()
解决方法
想通了。
def mainMenu():
def disable():
menu.disable()
mainBackgammonGame.startGame()
surface = pygame.display.set_mode((width,height))
menu = pygame_menu.Menu(960,1280,'Welcome to Backgammon',theme = pygame_menu.themes.THEME_DARK)
menu.add_text_input("Your Name: ",default = "Joseph Smith")
menu.add_button('Play',disable)
menu.add_button('Quit',pygame_menu.events.EXIT)
menu.mainloop(surface)
在主菜单功能中添加了一个新功能,最初禁用菜单,然后将调用加载屏幕的 startGame 函数。