Adding enemies to a pygame platformer

1.4k views Asked by At

I'm new to pygame and trying to make a platformer game that's based on this tutorial: http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.py

I can't quite figure out how to add moving enemies, can you help me?

1

There are 1 answers

0
Narpar1217 On

Moving enemies would be something of a combination of how the Player and Platform objects work in the example to which you linked:

  1. The enemy class would be a subclass of pygame.sprite.Sprite, similar to both aforementioned objects.

  2. They would have to implement an update() method, similar to Player, to define how they move on each frame. Look at Player.update() for guidance; basically, move the Enemy's rect in some way.

  3. Instances of the enemy class should be added to a level's enemy_list object (which already exists in the example code), which means they would be updated and drawn on every frame. This is similar to how Level_0x constructors add Platform instances to the level's platform_list variable.

In short, that would look something like:

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        # Set the size, look, initial position, etc. of an enemy here...
        pass

    def update(self):
        # Define how the enemy moves on each frame here...
        pass

class Level_01(Level):
    def __init__(self, player):
        # platform code already in example goes here...

        # Add two enemies to the level
        self.enemy_list.add(Enemy())
        self.enemy_list.add(Enemy())