将敌人添加到 pygame 平台游戏 [英] Adding enemies to a pygame platformer
问题描述
我是 pygame 的新手,正在尝试制作基于本教程的平台游戏:http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.py
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?
推荐答案
移动敌人将是示例中 Player
和 Platform
对象如何工作的组合您链接到的:
Moving enemies would be something of a combination of how the Player
and Platform
objects work in the example to which you linked:
敌人类将是
pygame.sprite.Sprite
的子类,类似于上述两个对象.
The enemy class would be a subclass of
pygame.sprite.Sprite
, similar to both aforementioned objects.
他们必须实现一个 update()
方法,类似于 Player
,来定义他们如何在每一帧上移动.查看 Player.update()
以获得指导;基本上,以某种方式移动 Enemy
的 rect
.
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.
敌人类的实例应该添加到关卡的 enemy_list
对象(示例代码中已经存在),这意味着它们将在每一帧更新和绘制.这类似于 Level_0x
构造函数如何将 Platform
实例添加到关卡的 platform_list
变量中.
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())
这篇关于将敌人添加到 pygame 平台游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!