0
\$\begingroup\$

When my player reaches at the end of the map, I want it to enter to next level. I've created first level and it all works fine. All I want to do is that when player finishes 1st level I want to load another map on which it can move forward. But I'm not able to understand how to do it Here is piece of my code

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()
    # This function consists code for drawing the sprites over the screen
    theGame.redrawGameWindow()


def readyGame(run, gameOver):
    game = Game()

    while run:
        runGame(game)

        keys = pygame.key.get_pressed()

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

        if gameOver:
            runGame(game)


readyGame(True, False)

and Here is my whole code which is responsible for my 1st level please kindly check it - https://pastebin.com/zMFetAd6 (Game Class)

Do I have to create another class for 2nd level like i did for 1st level? What i must do

Can anyone help me out in this issue. Thanks in Advance

\$\endgroup\$
0
\$\begingroup\$

runGame can return a value as it is a function. Let's have it return True if a player completes the level. I'm assuming you already have a check for that in one of your other functions.

Your Game class can take arguments by adding them to its __init__ method. Let's add a "level" argument and set the default to 0.

def __init__(self, level):
    if level == 0:
        # existing code for level 1 here
    elif level == 1:
        # code for level 2 here.

    # code that applies to all levels here.

Now all we have to do is create a new variable called level.

def readyGame(run, gameOver):
    level = 0
    game = Game(level)

    while run:
        if runGame(game) is True:
            print("Next level")
            level = level + 1
            game = Game(level)

        ...

From a pure code quality perspective, there are some things I might change about this implementation, but this will get you up and running.

In response to your comment:

class Game(pygame.sprite.Sprite):

    def __init__(self, level):  # add level parameter here
        super().__init__()
        self.clock = pygame.time.Clock()
        # This part stays the same for every level
        #   so I am cutting it out to save space

        if level == 1:
            self.hero = thePlayer(45, 625, 40, 40)

            self.enemy = # enemies for level 1
            self.platform = # platforms for level 1
        elif level == 2:
            self.hero = thePlayer(45, 625, 40, 40)

            self.enemy = # enemies for level 2
            self.platform = # platforms for level 2
        elif level == 3:
            # and so on

        self.facing = 0
        self.landed = True
        self.enterGame = pygame.image.load('startorclose.png')

        self.showMenu = pygame.image.load('MainMenu.png')
        self.stopScreen = False
        self.gameOver = False

This doesn't fit your code exactly because it seems like you'd have the the menu show up every time. But in short all you're adding is a new argument to __init__ in the Game class and a if blocks here and there for the levels.

Your code has a lot of little issues so I'll leave you to hash it out and learn from the experience. Keep working and posting questions.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I didn't get it - how can i put my first level code inside of init function - can you please show me how to do it using an example? \$\endgroup\$ – hotstepper Jun 30 '19 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.