0

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 first level: https://pastebin.com/yRb8T6ku (Game Class)

Do I have to create another class for second level like I did for first level?

halfer
  • 19,824
  • 17
  • 99
  • 186
hotstepper
  • 39
  • 6

1 Answers1

1

Firstly, some pieces of advice unrelated to your concrete question: Don't use enemy = ..., enemy2 = ..., enemy3 = ... and so on. Use lists

self.enemies = [
  enemy(),
  enemy(),
  enemy()
]

Same way for platforms and coins, when you can change most of the methods to cycles:

    def hit_or_not(self):
        # Checking Collision of Enemy and Hero
        for enemy in self.enemies:
            self.enemyCollide(self.hero, enemy)

then, let's look at what is level. I think it's hero, enemies, platforms, and coins, so declare

LEVELS = [
  {  # level1
    'enemies': [ 
      enemy(...),  # move here from your Game.__init__
      enemy(...),
      ....
    ],
    'platforms': [
      Platform(...),
      ...
    ]
    'coins': [
      Coins(...),
      ...
    'hero': thePlayer(...)
    ]
  }
  {  # level2
    ...
  }
  ...
] 

then you can switch to level something like that:

# in the Game class
def switchLevel(self, levelno):
   self.hero = LEVELS[levelno]['hero']
   self.enemies = LEVELS[levelno]['enemies']
   self.platforms = LEVELS[levelno]['platforms']
   self.coins = LEVELS[levelno]['coins']

so, when you decide to switch level, do

game.switchLevel(...)
game.redrawGameWindow()
Sav
  • 616
  • 3
  • 9
  • Can you please explain me how to make next level? I didnt got you – hotstepper Jun 28 '19 at 13:36
  • in the LEVELS showed above just copy all strings from marked '# level1' till marked as '# level2' and paste it below. You'll get level2 looking exactly as level1. Then edit coordinates, add/remove enemies, platforms, and coins in level2 as you like. – Sav Jun 28 '19 at 13:42
  • Sir it would be great if you write some code - sorry for being a noob – hotstepper Jun 28 '19 at 14:02