Pygame屏幕循环b

2024-09-25 02:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python做一个游戏,它有一个主菜单,后面是一个难度选择屏幕。当我选择play game(玩游戏)按钮时,屏幕会按预期变为难度选择屏幕,但当我单击一个难度时,它会返回到功能齐全的主菜单屏幕,使我相信该功能仍在继续运行。如果我然后返回到难度选择屏幕,并选择一个困难的第二次游戏进展如预期。没有其他按钮导致这个错误,这让我相信错误是在选择难度函数本身

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)

Tags: andtheimageloop屏幕displaybuttonpygame
1条回答
网友
1楼 · 发布于 2024-09-25 02:30:58

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

Def main():
    gameIntro()

If __name__=="__main__":
    Main()

gameintro函数只运行一次

相关问题 更多 >