When my player reaches at the end of the map, I want it to enter to the next level. I've created the 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):


    # This function consists code for Events
    # This function consists code from enemy hit events
    # This function consists code for player movements
    # This function consists code for drawing the sprites over the screen

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

    while run:

        keys = pygame.key.get_pressed()

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

        if gameOver:

readyGame(True, False)

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 the 1st level?

Can anyone please help me out in this issue? Thanks in advance!


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
        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.

  • \$\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.