简体   繁体   中英

Adding enemies to a pygame platformer

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM