我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

编写一款致敬《炸弹杰克》(Bomb Jack)的爆炸性游戏 | 《Wireframe》第59期

帮助杰克收集炸弹——用 Python 和 Pygame Zero 重制这款街机经典。马克·范斯通(Mark Vanstone)提供完整源代码,直接来自本期《Wireframe》杂志。《Wireframe》杂志。

1984 年,一位新英雄飞入街机厅。由 Tehkan(后更名为 Tecmo)开发的《炸弹杰克》中,这位披着斗篷的主角在平台上腾跃穿梭,一边收集炸弹,一边躲避巡逻的敌人。若能收集到引信正在燃烧的炸弹,杰克还可获得额外分数;一旦清空当前屏幕,便进入下一个环游世界的关卡。《炸弹杰克》很快被移植至家用电脑平台,并陆续推出多部续作与复刻版,其中包括登陆 Nintendo Switch 的版本。

每一关《炸弹杰克》都拥有不同的背景与平台布局,以保持新鲜感。

在我们的 Python 重制版中,我们将重点实现杰克独特的移动方式:他能跳得极高,并缓缓飘落;同时,他的斗篷可用于左右引导方向,并以更慢的速度滑翔。借助 Pygame Zero,我们首先加载原作中的埃及风格背景,再在其上叠加若干平台。每个平台均使用不同图像,并为每个平台创建一个 Actor 对象。我们将这些 Actor 存入列表中,以便后续更便捷地检测碰撞。我们还为杰克创建一个 Actor,并添加若干额外属性,用于追踪其朝向与推力。完成上述步骤后,我们即可为炸弹创建 Actor。我们先定义一个坐标列表来表示每颗炸弹的位置,然后遍历该列表,在每个位置创建一颗炸弹 Actor,并将其加入炸弹列表中。

我们的draw() function will first draw the background followed by the platforms (from the platform list we created earlier), then the bombs from the bomb list, and then Jack. We can also add the score at the top of the screen. You’ll see that the bomb Actors also have a property called ‘state’, which we’ll use later when checking to see if Jack has collected them or not. A gameState variable can also be set when all the bombs have been collected to trigger the display of a message saying that the level has been completed.
在我们的update() function, we’ll handle Jack’s movement. We can assume that Jack is facing forward to start with, and then we can adjust his direction depending on what keys are being pressed or if he’s jumping up or floating down. We need to do two collision checks because we don’t want Jack to jump up through the bottom of a platform, but at the same time, we want him to land on top of a platform if he’s falling down. If there are no collisions, then we increase Jack’s Y coordinate to simulate gravity, and to counter that, subtract any thrust value that should be applied. The thrust value is reduced each update until it gets to zero. If the left or right keys are pressed and Jack’s falling, we move his X coordinate accordingly and give him a little boost upwards to stop him falling so quickly. If he’s on a platform, we display animation frames to make him run left or right. We capture the jump key press in the on_key_down() function and make the thrust property equal to 20, which will send Jack up in the air.

收集全部炸弹,致敬《炸弹杰克》。

《炸弹杰克》的另一部分是update() function checks the state of the bombs. We run through the list of bombs, checking to see if Jack has collided with them. If he has, we set the bomb state to a value higher than 1. This sets off an animation cycle through a few frames until the bomb disappears. In this loop, we count how many bombs have reached the invisible state – when that number reaches the total number of bombs, we set gameState to 1 and the level is completed.

若要拓展本项目,你可以加入反派角色;别忘了,引信正在“嘶嘶”燃烧的炸弹若被杰克及时拾取,还能为其带来额外奖励。一如往常,这些功能留待你自行添加。

这里是马克编写的类《炸弹杰克》平台游戏代码。要在你的系统上运行它,首先需安装 Pygame Zero。完整安装说明请访问wfmag.cc/pgzero。完整的代码清单请见请访问我们的Github。

获取《Wireframe》第59期纸质版

你可在《Wireframe》第58期中阅读更多类似专题,可直接从树莓派出版社购买——我们支持全球配送,且提供多种订阅优惠以节省单期购买费用。

若你偏好数字版杂志,亦可下载《Wireframe》第58期的免费PDF版!

暂无评论

评论已关闭