【问题标题】:Adding enemies to a pygame platformer将敌人添加到 pygame 平台游戏
【发布时间】:2015-06-14 19:01:09
【问题描述】:

我是 pygame 的新手,正在尝试制作基于本教程的平台游戏:http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.py

我不太明白如何添加移动的敌人,你能帮我吗?

【问题讨论】:

    标签: python pygame scroller


    【解决方案1】:

    在您链接的示例中,移动敌人可能是 PlayerPlatform 对象如何工作的组合:

    1. 敌人类将是pygame.sprite.Sprite 的子类,类似于上述两个对象。

    2. 他们必须实现一个update() 方法,类似于Player,来定义他们如何在每一帧上移动。看Player.update()指导;基本上,以某种方式移动Enemyrect

    3. 应该将敌人类的实例添加到关卡的enemy_list 对象(示例代码中已经存在),这意味着它们将在每一帧上更新和绘制。这类似于Level_0x 构造函数如何将Platform 实例添加到关卡的platform_list 变量中。

    简而言之,这看起来像:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2020-05-10
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多