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

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

编程实现《太空侵略者》中逐渐瓦解的护盾 | 《Wireframe》第9期

它们为这一定义类型的游戏增添了策略性。安德鲁·吉莱特(Andrew Gillett)揭开了《太空侵略者》中护盾逐步瓦解机制的奥秘。

Wireframe 9 Space Invaders

《太空侵略者》于1978年发布,其所引入的游戏理念如此基础、深远,以至于我们几乎难以想象在它出现之前的游戏世界。而它仅凭定制硬件便实现了这一切——以当今标准来看,该硬件运行速度慢得难以置信。

《太空侵略者》运行于主频2MHz的Intel 8080 CPU之上。在如此有限的处理能力下,仅在屏幕上移动精灵(sprite)都极为困难。而在现代2D游戏中,每一帧开始时,整个屏幕都会被重置,然后重新绘制所有对象。

对《太空侵略者》的硬件而言,这种重置方式过于缓慢。因此,每当需要移动一个精灵时,游戏首先将其从屏幕上擦除,再于新位置重新绘制。此外,游戏每帧仅更新一个外星人——这导致了“外星人数量越少,移动越快”的视觉效果。这些技术将每帧需更新的像素数从近60,000个大幅削减至约100个。

Wireframe 9 Space Invaders

《太空侵略者》最显著的特性之一便是其四面护盾。它们可为玩家提供抵御敌方火力的掩体,但经多次命中后会逐渐损毁。玩家可利用护盾的可破坏性:反复朝同一护盾底部位置射击,即可凿出一条狭窄缝隙,进而借此消灭敌人。(当然,玩家自身也可能透过同一缝隙被击中。)

“仅更新最少必要像素”的系统,在对象互不重叠时运作良好;但对护盾而言,对象发生重叠的情形恰恰是其工作机制的核心所在。每当子弹击中某物,该处即被替换为爆炸精灵;数帧之后,该爆炸精灵又从屏幕上删除。若爆炸精灵与护盾发生重叠,则护盾被覆盖的部分亦随之一同删除。

Wireframe 9 Space Invaders

下面是一段代码片段,展示了安德鲁用Python实现的《太空侵略者》风格可瓦解护盾。要在您的系统上运行此代码,您需先安装Pygame Zero——完整安装说明请见此处。此外,您还可在此处下载上述代码.

右侧代码显示四面护盾,并向其连续发射多枚撞击即爆的炮弹。我已将所用精灵放大十倍,以便更清晰地观察其运行过程。

我们首先创建两个空列表——其中一个用于存储所有shots on screen, as well as explosions. These will be displayed on the screen every frame. Each entry in the shots list will be a dictionary data structure containing three values: a position, the sprite to be displayed, and whether the shot is in ‘exploding’ mode — in which case it’s displayed in the same position for a few frames before being deleted.

第二个列表为to_delete, is for sprites which need to be deleted from the screen. For simplicity, I’m using separate copies of the shot and explosion sprites where the white pixels have been changed to black (the other pixels in these sprites are set as transparent).

函数create_random_shot is called every half-second. The combination of dividing the maximum value by ten, choosing a random whole number between zero and the maximum value, and then multiplying the resulting random number by ten, ensures that the chosen X coordinate is a multiple of ten.

安德鲁基于Pygame Zero实现的《太空侵略者》护盾。

在draw function, we first check to see if it’s the first frame, as we only want to display the shields on that frame. The screen.blit method is used to display sprites, and Pygame Zero’s images object is used to specify which sprite should be displayed. We then display all sprites in the to_delete list, after which we reset it to being an empty list. Finally we display all sprites in the shots list.

Wireframe 9 Space Invaders

在update function, we go through all sprites in the shots list, in reverse order. Going through the list backwards avoids problems that can occur when deleting items from a list inside a for loop. For each shot, we first check to see if it’s in ‘exploding’ mode. If so, its timer is reduced each frame — when it hits zero we add the shot to the to_delete list, then delete it from shots.

若该项目为普通炮弹而非爆炸效果,则将其当前位置添加至to_delete, then update the shot’s position to move the sprite down the screen. We next check to see if the sprite has either gone off the bottom of the screen or collided with something. Pygame’s get_at method gives us the colour of a pixel at a given position. If a collision occurs, we switch the shot into ‘exploding’ mode — the explosion sprite will be displayed for five frames.

您可以在《Wireframe》第9期,现于英国特易购(Tesco)、WHSmith及所有优质独立报刊亭有售。

或您亦可购买直接从我们这里获取《Wireframe》杂志线框图(Wireframe)——支持全球配送。若您希望拥有该杂志便捷的数字版,还可免费下载PDF版本。

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

暂无评论

评论已关闭