简体   繁体   English

将敌人添加到 pygame 平台游戏

[英]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我是 pygame 的新手,并试图制作基于本教程的平台游戏: 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:移动敌人将是您链接到的示例中PlayerPlatform对象如何工作的组合:

  1. The enemy class would be a subclass of pygame.sprite.Sprite , similar to both aforementioned objects.敌人类将是pygame.sprite.Sprite的子类,类似于上述两个对象。

  2. They would have to implement an update() method, similar to Player , to define how they move on each frame.他们必须实现一个update()方法,类似于Player ,来定义他们如何在每一帧上移动。 Look at Player.update() for guidance;查看Player.update()以获得指导; basically, move the Enemy 's rect in some way.基本上,以某种方式移动Enemyrect

  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.应将敌人类的实例添加到关卡的enemy_list对象(示例代码中已存在),这意味着它们将在每一帧更新和绘制。 This is similar to how Level_0x constructors add Platform instances to the level's platform_list variable.这类似于Level_0x构造函数如何将Platform实例添加到关卡的platform_list变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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