# How to make player move into next level in pygame

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

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

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)



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?

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.

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

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.