如何在pygame中按下按钮开始游戏

时间:2019-07-11 12:23:27

标签: python pygame

我想为我的游戏创建主菜单。在主菜单中,我提供2个选项进行播放,即按SPACE键开始游戏或按ESC键结束游戏。

到目前为止我尝试过的,

这是我的mainMenu()函数,它调用readygame()

def mainMenu():
    screen.blit(menu, (0, 0))

    pygame.display.flip()

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        readyGame(True, False)
    elif keys[pygame.K_ESCAPE]:
        pygame.quit()
    pygame.event.pump()

和此readyGame()函数。该函数负责通过调用runGame()函数启动游戏-此函数启动游戏。

def readyGame(run, gameOver):
game = Game(1)
game2 = Game(2)
game3 = Game(3)

while run:
    if game.level == 1:

        runGame(game)

        keys = pygame.key.get_pressed()

        if keys[pygame.K_x]:
            game = Game(1)
        elif keys[pygame.K_ESCAPE]:
            run = False
        pygame.event.pump()

        if gameOver:
            runGame(game)

    elif game.level == 2:
        runGame(game2)

        keys = pygame.key.get_pressed()

        if keys[pygame.K_x]:
            game2 = Game(2)
        elif keys[pygame.K_ESCAPE]:
            run = False
        pygame.event.pump()

        if gameOver:
            runGame(game)

    elif game.level == 3:
        runGame(game3)

        keys = pygame.key.get_pressed()

        if keys[pygame.K_x]:
            game3 = Game(3)
        elif keys[pygame.K_ESCAPE]:
            run = False
        pygame.event.pump()

        if gameOver:
            runGame(game)

runGame()函数就是这样,

def runGame(theGame):

theGame.clock.tick(FPS)

# This function consists code for Events
theGame.events()
# This function consists code from enemy hit events
theGame.hit_or_not()
# This function consists code for player movements
theGame.movements()

if theGame.level == 1:
    # This function consists code for drawing the sprites over the screen
    theGame.redrawGameWindow()

elif theGame.level == 2:
    theGame.redrawGameWindow2()

elif theGame.level == 3:

    for o in objects:
        o.x -= 1.4
        if o.x < o.width * -1:
            objects.pop(objects.index(o))

    for o in objectsL:
        if o.x >= 300:
            objectsL.pop(objectsL.index(o))

    theGame.redrawGameWindow3()

任何人都可以帮助我找到一种创建主菜单的方法,该菜单只有两个选项-SPACE启动游戏,而ESC关闭游戏。我希望玩家死亡时会发生这种情况。

预先感谢

0 个答案:

没有答案