编写反重力平台跳跃游戏机制 | Wireframe #65
在我们向NES经典游戏Metal Storm致敬的作品中体验在天花板上行走。Mark Vanstone在最新一期《线框图》杂志中所解释的那样,该期刊现已发布。

1991年初,NES平台推出了一款令人耳目一新的平台跳跃游戏。Metal Storm的独特之处在于,它允许玩家切换重力方向,使其驾驶的M-308 Gunner机甲可以在地板或天花板上行走。加上各种前来消灭玩家的敌人和Boss,这在当时堪称一款相当复杂且极具挑战性的游戏。
在本示例中,我们将重点介绍这一重力机制,用Pygame Zero编写该游戏的复刻版。Metal Storm采用了视差滚动背景,我们通过绘制两个背景层来实现。第一层是蓝色波浪图案,其移动速度为主平台层的一半。我们使用变量来追踪背景X坐标的位置mapx and divide that by 2 when drawing the blue background. Then we draw the section of the platform map that will appear on the screen.
为了定义平台和墙壁的位置,我们使用一张小图像,其中一个像素代表一个平台方块。绿色像素代表绿色方块,灰色像素代表灰色方块,红色像素代表可以透视背景的区域。每个方块都被绘制为75 × 75像素的图像。

接下来,我们需要制作Gunner。快速访问spriters-resource.com可以获取一些合适的动画帧来制作Actor。我们的玩家角色通常会定位在屏幕X轴的中心,并在Y轴上上下移动。
我们可以设置左右方向键,让平台在Gunner行走时在其身后移动,但需要检查小地图图像以确保它不会穿墙。我们可以向movement function to check if our Gunner has reached the edge of the map, and if so, move the Gunner Actor instead of the platforms. The walking animation is done by cycling through numbered frames for either left or right movement.
我们还希望Gunner能够跳跃。默认情况下,我们设置Actor向下坠落直到接触平台。有些平台足够低可以跳上去,我们可以将SPACE键分配为执行跳跃动画。当按下SPACE键时,我们为Gunner Actor的jump属性赋值。在更新Actor时,如果jump property is greater than zero, we boost the Actor into the air and reduce the jump value by 1. Once jump reaches zero, we stop boosting and let the Actor fall by applying the default gravity calculation.

现在我们已经让Gunner四处行走和跳跃了,接下来需要实现重力方向的切换。为此,我们添加一个gravity property to the Gunner Actor. To start with, we’ll set this to 1, and any calculations we do that involve movement on the Y axis, we multiply by this value. With this value as 1, everything will stay the same, the Gunner will fall downwards and jump upwards. But if we change it to -1, the same calculations will cause the Gunner to fall upwards and jump downwards, providing the effect of gravity being switched. This also means the mech can get past obstacles that we wouldn’t be able to jump over. We set this switch from 1 to -1 to happen when the up arrow is held down and the SPACE键被按下时。
就这样,我们完成了一款可切换重力的Metal Storm风格平台跳跃游戏。现在只差各种敌方机器人以及射击它们的方法了,不过这些扩展内容就留给你们自己去实现吧。
获取您的Wireframe #65
你可以在更多类似文章在Wireframe第65期中,可直接从树莓派出版社购买——我们支持全球配送。

如果您更喜欢数字版杂志,还可以下载 Wireframe 第65期,格式为免费PDF版!
1条评论
Esbeeb
Metalstorm 是一款被低估的游戏。当年在初代 NES 主机上玩的时候我就非常喜欢。:) 游戏的物理效果非常棒。
评论已关闭