【发布时间】:2015-06-14 19:01:09
【问题描述】:
我是 pygame 的新手,正在尝试制作基于本教程的平台游戏: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
我不太明白如何添加移动的敌人,你能帮我吗?
【问题讨论】:
在您链接的示例中,移动敌人可能是 Player 和 Platform 对象如何工作的组合:
敌人类将是pygame.sprite.Sprite 的子类,类似于上述两个对象。
他们必须实现一个update() 方法,类似于Player,来定义他们如何在每一帧上移动。看Player.update()指导;基本上,以某种方式移动Enemy 的rect。
应该将敌人类的实例添加到关卡的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())
【讨论】: