【问题标题】:Pygame screen looping backPygame 屏幕循环返回
【发布时间】:2019-10-22 18:16:07
【问题描述】:

我正在用 python 制作一个游戏,它有一个主菜单,然后是一个难度选择屏幕。当我选择玩游戏按钮时,屏幕按预期切换到难度选择屏幕,但是当我点击一个难度时,它返回到主菜单屏幕,功能齐全,让我相信该功能正在继续运行.如果我然后返回到难度选择屏幕并再次选择难度,游戏会按预期进行。没有其他按钮会导致此错误,这使我相信错误出在选择难度功能本身。

def gameIntro(): # game intro loop
    playButton=classes.Button(100,200,275,350,selectDifficulty) #classes is external file which contains a button class. params (x,y,w,h,action=None)
    leaderButton=classes.Button(700,200,275,350,leaderboard)
    quitButton=classes.Button(1000,200,250,350,quit)
    instructButton=classes.Button(425,200,275,350,instructions)
    spriteGroup = pygame.sprite.Group(playButton, leaderButton, quitButton, instructButton)
    while not(playButton.loop and leaderButton.loop and quitButton.loop and instructButton.loop): #all of button objects have loop set to false, until they are clicked
        eventLoop(spriteGroup) #runs an event handler for all of objects in spriteGroup
        mainMenuScreen = pygame.image.load('Menu.jpg')  # loads the menu Screens
        mainMenuScreen = pygame.transform.scale(mainMenuScreen, (displayWidth, displayHeight)) #adjusts image to be same size as display
        gameDisplay.blit(mainMenuScreen, (0, 0)) #displays the image
        pygame.display.update() #updates the display
        clock.tick(fps) #increments the clock by the fps amount to keep the screen changing

def selectDifficulty(): #selects the difficulty so that the appropriate AI is loaded
    import Main
    easyButton=classes.Button(70,150,300,400,Main.easyAIMaker)
    mediumButton=classes.Button(450,150,300,400)
    hardButton=classes.Button(800,150,300,400)
    spriteGroup = pygame.sprite.Group(easyButton,mediumButton,hardButton)
    while not (easyButton.loop and mediumButton.loop and hardButton.loop): #loops the screen while a choice hasnt been made
        eventLoop(spriteGroup)
        difficultyScreen = pygame.image.load('difficulty screen.jpg')
        difficultyScreen = pygame.transform.scale(difficultyScreen, (displayWidth, displayHeight))
        gameDisplay.blit(difficultyScreen, (0, 0))
        pygame.display.update()
        clock.tick(fps)

【问题讨论】:

  • 请在代码中的 cmets 中 # 之前添加空格,以便这些 cmets 使用不同的颜色,代码将更具可读性。
  • 顺便说一句:好的规则:所有import 都放在脚本的开头。这样每个人都会知道它需要哪些其他模块才能正确运行。在PEP 8 -- Style Guide for Python Code中查看更多信息
  • 对于这个问题更重要的是处理事件的代码,而不是显示屏幕的代码。也许您为按钮分配了错误的功能或按钮以错误的方式工作,当您单击它时,它会显示正确的屏幕,但随后它会自动单击返回主菜单的返回按钮 - 如果它不使用,这是 PyGame 中的常见问题pygame.BUTTONDOWN 检查点击但pygame.mouse.get_pressed()
  • 我将导入放在函数中以避免循环导入错误。我在我的事件处理程序中使用 .buttondown 。我什么时候可以发布事件处理程序的代码
  • 在开始时,您可以使用print() 查看变量中的值以及按下按钮时执行的代码部分。这可能有助于找到问题所在。称为“打印调试”。

标签: python python-2.7 button pygame


【解决方案1】:

问题是我没有意识到 python 如何处理导入的代码。在 intro.py 的最后,调用 gameintro() 函数来启动游戏。当我导入 intro.py 时,此代码重新运行导致循环。被屏蔽游戏介绍里面:

Def main():
    gameIntro()

If __name__=="__main__":
    Main()

gameintro 函数只运行一次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2018-05-18
    • 2020-08-28
    • 2013-12-24
    • 2011-08-30
    相关资源
    最近更新 更多