项目报名截止日期为4月14日。
Raspberry Pi 高级学习经理里克·克罗斯(Rik Cross)向您演示如何复现经典气球爆破街机游戏《庞》(Pang)中的物体生成机制。

《庞》:自1989年起,将“气球仇恨”普及大众。
卡普空(Capcom)版《庞》
由米切尔(Mitchell)公司编程、卡普空(Capcom)发行,《庞》最初于1989年作为街机游戏推出,之后被移植到大量家用电脑平台,包括ZX Spectrum、Amiga和Commodore 64。游戏中,玩家目标是击破在屏幕上弹跳的气球——可单人游玩,也可双人合作,在日益复杂的关卡中完成挑战。击破气球有时还会生成道具:例如短暂冻结所有气球,或为玩家提供更强力的武器以更高效地击破气球。
游戏初始阶段,玩家需击破数量较少但体积较大的气球。然而,击破一个大气球会生成两个较小的气球;再击破小气球,又会各自生成两个更小的气球,依此类推。只有当所有气球均被逐级分解并彻底摧毁后,当前关卡才算完成。为增加挑战性,不同尺寸的气球具有不同属性:小气球移动更快、弹跳高度更低,因而更难击破。

里克用 Pygame Zero 实现的气球生成效果已正常运行。按空格键可将气球分裂为更小的气球。
气球生成机制
实现该游戏机制的方法有多种,而我在本例中采用的是面向对象编程(OOP)的若干特性(如常例,示例代码使用 Python 编写,并基于 Pygame Zero 库)。另需说明:为简洁起见,示例代码仅处理对象的简单生成与销毁逻辑,未包含气球运动及碰撞检测功能。
基类Enemy class is simply a subclass of Pygame Zero’s Actor class, including a static enemies list to keep track of all enemies that exist within a level. The Enemy subclass also includes a destroy() method, which removes an enemy from the enemies list and deletes the object.
此外还有三个继承自该基类的子类Enemy class, called LargeEnemy, MediumEnemy, and SmallEnemy. Each of these subclasses are instantiated with a specific image, and also include a destroy() method. This method simply calls the same destroy() method of its parent Enemy class, but additionally creates two more objects nearby — with large enemies spawning two medium enemies, and medium enemies spawning two small enemies.
在示例代码中,初始时生成两个LargeEnemy objects are created, with the first object in the enemies list having its destroy() method called each time the Space key is pressed. If you run this code, you’ll see that the first large enemy is destroyed and two medium-sized enemies are created. This chain reaction of destroying and creating enemies continues until all SmallEnemy objects are destroyed (small enemies don’t create any other enemies when destroyed).
如前所述,此方法并非实现该行为的唯一途径,且各有优劣。为每种尺寸敌人分别定义子类,便于高度定制化;但若敌人尺寸种类远超三种,该方式可能变得难以维护。一种替代方案是仅定义单一Enemy class, with a size attribute. The enemy’s image, the entities it creates when destroyed, and even the movement speed and bounce height could all depend on the value of the enemy size.
您可以在《Wireframe》第10期,现于英国特易购(Tesco)、WHSmith及所有优质独立报刊亭有售。
或您亦可购买直接从我们这里获取《Wireframe》杂志线框图(Wireframe)——支持全球配送。若您希望拥有该杂志便捷的数字版,还可免费下载PDF版本。

请关注《线框图》的和以及脸书获取最新资讯与独家内容,以及订阅服务,请访问 Wireframe 官方网站享新闻报摊售价立减49%的优惠!

暂无评论
评论已关闭