小学生python游戏编程arcade----原码分析 物理引擎 self.physics_engine = arcade.PhysicsEnginePlatformer(
我们正在发送我们的 SpriteList
对于玩家应该碰撞到的东西 walls
物理引擎的参数。正如我们将在后面的章节中看到的,Platform物理引擎有一个 platforms
和 walls
参数。这两者之间的区别非常重要。静态不移动精灵列表应始终发送到 walls
参数,并且移动的精灵应发送到 platforms
参数。确保您做到这一点将对性能有极大的好处。
通过添加静态精灵 platforms
参数大致是一个O(N)运算,这意味着随着您添加更多的精灵,性能将线性下降。如果将静态精灵添加到 walls
参数,那么它几乎是O(1),并且例如100个和50,000个不移动的精灵之间基本上没有区别。
我们还在这里看到了一些与我们的 Scene
对象。您可以像访问Python词典一样访问场景,以便从中获取SpriteList。有多种方法可以访问场景中的SpriteList,但这是最简单、最直接的方法。
class PhysicsEngineSimple: """ Simplistic physics engine for use in games without gravity, such as top-down games. It is easier to get started with this engine than more sophisticated engines like PyMunk. :param Sprite player_sprite: The moving sprite :param Union[SpriteList, Iterable[SpriteList] walls: The sprites it can't move through. This can be one or multiple spritelists. """ def __init__(self, player_sprite: Sprite, walls: Union[SpriteList, Iterable[SpriteList]]): """ Create a simple physics engine. """ assert isinstance(player_sprite, Sprite) if walls: if isinstance(walls, SpriteList): self.walls = [walls] else: self.walls = list(walls) else: self.walls = [] self.player_sprite = player_sprite def update(self): """ Move everything and resolve collisions. :Returns: SpriteList with all sprites contacted. Empty list if no sprites. """ return _move_sprite(self.player_sprite, self.walls, ramp_up=False)
第二种引
class PhysicsEnginePlatformer: """ Simplistic physics engine for use in a platformer. It is easier to get started with this engine than more sophisticated engines like PyMunk. **Note:** Sending static sprites to the ``walls`` parameter and moving sprites to the ``platforms`` parameter will have very extreme benefits to performance. **Note:** This engine will automatically move any Sprites sent to the ``platforms`` parameter between a ``boundary_top`` and ``boundary_bottom`` or a ``boundary_left`` and ``boundary_right`` attribute of the Sprite. You need only set an initial ``change_x`` or ``change_y`` on it. :param Sprite player_sprite: The moving sprite :param Optional[Union[SpriteList, Iterable[SpriteList]]] platforms: Sprites the player can't move through. This value should only be used for moving Sprites. Static sprites should be sent to the ``walls`` parameter. :param float gravity_constant: Downward acceleration per frame :param Optional[Union[SpriteList, Iterable[SpriteList]]] ladders: Ladders the user can climb on :param Optional[Union[SpriteList, Iterable[SpriteList]]] walls: Sprites the player can't move through. This value should only be used for static Sprites. Moving sprites should be sent to the ``platforms`` parameter. """ def __init__(self, player_sprite: Sprite, platforms: Optional[Union[SpriteList, Iterable[SpriteList]]] = None, gravity_constant: float = 0.5, ladders: Optional[Union[SpriteList, Iterable[SpriteList]]] = None, walls: Optional[Union[SpriteList, Iterable[SpriteList]]] = None, ): """ Create a physics engine for a platformer. """ self.ladders: Optional[List[SpriteList]] self.platforms: List[SpriteList] self.walls: List[SpriteList] if ladders: if isinstance(ladders, SpriteList): self.ladders = [ladders] else: self.ladders = list(ladders) else: self.ladders = None if platforms: if isinstance(platforms, SpriteList): self.platforms = [platforms] else: self.platforms = list(platforms) else: self.platforms = [] if walls: if isinstance(walls, SpriteList): self.walls = [walls] else: self.walls = list(walls) else: self.walls = [] self.player_sprite: Sprite = player_sprite self.gravity_constant: float = gravity_constant self.jumps_since_ground: int = 0 self.allowed_jumps: int = 1 self.allow_multi_jump: bool = False