0

I want to create main menu for my game. In main menu I give 2 options to play i.e press SPACE to start the game or press ESC to end the game.

What I tried so far,

This is my mainMenu() function which invokes 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()

and this readyGame() Function. This function which is responsible to launch the game by calling runGame() function - This function starts the game.

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)

And runGame() function is something like this,

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()

Can anyone help me in finding a way to create main menu which will have just 2 options that are - SPACE to start game and ESC to close it. And i want this to happen when player dies.

Thanks in Advance

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Eddy
  • 344
  • 5
  • 16

0 Answers0