[英]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:移动敌人将是您链接到的示例中
Player
和Platform
对象如何工作的组合:
The enemy class would be a subclass of pygame.sprite.Sprite
, similar to both aforementioned objects.敌人类将是
pygame.sprite.Sprite
的子类,类似于上述两个对象。
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.基本上,以某种方式移动
Enemy
的rect
。
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.