Экран Pygame возвращается назад - PullRequest
0 голосов
/ 22 октября 2019

Я делаю игру на 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)

1 Ответ

0 голосов
/ 23 октября 2019

Проблема была в том, что я не понимал, как python обрабатывает импортированный код. В самом конце intro.py вызывается функция gameintro () для запуска игры. Когда я импортировал intro.py, этот код был перезапущен, вызывая цикл. Защищая интро игры:

Def main():
    gameIntro()

If __name__=="__main__":
    Main()

Функция gameintro запускается только один раз

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...