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

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

重现标志性的80年代游戏爆炸效果 | Wireframe 第12期

横跨树莓派基金会的高级学习经理里克·克罗斯将向你展示如何重现经典游戏《炸弹人》中那致命的爆炸效果。

这是NES平台上的早期《炸弹人》版本;如今,在科乐美旗下,这一系列依然风靡一时。

制作《炸弹人》

《炸弹人》最早于20世纪80年代初作为一款BASIC编译器的技术演示作品问世,但很快便发展成了一款广受欢迎的系列游戏,至今仍在持续流行。在《炸弹人》中,玩家需利用炸弹摧毁敌人,并通过可破坏的方块揭开隐藏在其中的门。在这篇文章中,我将向你展示如何重现那些四方向爆炸的炸弹——它们不仅会摧毁关卡中的各个部分,还会炸毁玩家所经过的路径!

游戏关卡以二维数组的形式存储为网格地图。地图中的每一个方块都代表着一个……Tile object, which contains the tile type, and corresponding image. For simplicity, a tile can be set to one of five types; GROUND, WALL, BRICK, BOMB, or EXPLOSION. In this example code, BRICK and GROUND can be exploded with bombs, but WALL cannot, but of course, this behaviour can be changed.

每个Tile object also has a timer, which is decremented each frame of the game. When a tile’s timer reaches 0, an action is carried out, which is dependent on the tile type. BOMB tiles (and surrounding tiles) turn into EXPLOSION tiles after a short delay, and EXPLOSION tiles eventually turn back into GROUND. At the start of the game, the tilemap for the level is generated, in this case consisting of mostly GROUND, with some WALL and a couple of BRICK tiles. The player starts off in the top-left tile, and moves by using the arrow keys. Pressing the SPACE key will place a bomb in the player’s current tile, which is achieved by setting the Tile at the player’s position to BOMB. The tile’s timer is also set to a small number, and once this timer is decremented to 0, the bomb tile and the tiles around it are set to EXPLOSION.

这里是里克提供的示例代码,用Python重现了《炸弹人》的爆炸效果。要让代码在你的系统上运行,首先需要安装Pygame Zero——你可以……此处……而且你还可以……在这里下载代码.

炸弹会向四个方向向外爆炸,其爆炸范围由……决定。RANGE, which in our code is 3. As the bomb explodes out to the right, for example, the tile to the right of the bomb is checked. If such a tile exists (i.e. the position isn’t out of the level bounds) and can be exploded, then the tile’s type is set to EXPLOSION and the next tile to the right is checked. If the explosion moves out of the level bounds, or hits a WALL tile, then the explosion will stop radiating in that direction. This process is then repeated for the other directions.

有一种巧妙的方法,可以避免重复编写四次相同的代码来实现炸弹的爆炸效果,而这一方法依赖于四个方向角度的正弦和余弦值。这些角度分别是:0°(向上)、90°(向右)、180°(向下)和270°(向左)。当炸弹向右爆炸时(角度为90°),sin(90)等于1,cos(90)等于0,这分别对应于X轴和Y轴上的偏移方向。这些数值可以与方块的偏移量相乘,从而实现四方向的爆炸效果。

获取《Wireframe》第12期的副本

您可以在《Wireframe》第12期,现已在特易购、WHSmith以及所有优质的英国独立报刊店发售。

或者你可以直接从树莓派出版社购买从树莓派出版社直接购买线框图– 本杂志现已全球发售。如果你想要一份便捷的数字版杂志,也可以……免费下载第12期以PDF格式。

请关注《线框图》的和以及脸书以获取最新资讯与独家内容。在线框图网站上订阅订阅可比零售价节省高达49%!

1条评论

6by9 avatar

虽然这种做法颇具趣味性,但从度数转换为弧度,再对每个角度分别调用正弦和余弦函数,效率实在低得令人咋舌!

使用上述原理反而更加高效,只需为四个基本方向定义一个简单的数组即可。
sine_90 = [0, 1, 0, -1]

对于每个角度,从0开始,每次递增1:
sina = sine_90[angle & 3]
cosa = sine_90[(angle + 1) & 3]

所有整数运算、逻辑运算以及查找操作,尽在其中。

现在我们知道了为什么我们需要多GHz的处理器来运行游戏 :-/

评论已关闭