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

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

编写 Light Cycle 街机小游戏 | Wireframe #47

在这个 Light Cycle 小游戏中,在竞技场中高速穿梭,避开墙壁和致命的尾迹。马克·范斯通代码见此处。

1980 年代初,迪士尼计划制作一种全新的动画电影,采用尖端的计算机图形技术。最终成片是 1982 年的 TRON,它不可避免地催生了最早的街机改编游戏之一。

在经典街机原作中与 AI 敌人对战。

这款游戏包含多个小游戏,其中一个基于电影中的 Light Cycle 片段:玩家驾驶高科技摩托车在竞技场中疾驰,身后留下致命的流光尾迹。如果竞争者撞到墙壁或越过任何尾迹,游戏即告结束。

玩家需要依次通过十二个以编程语言命名的关卡。在 Light Cycle 游戏中,玩家与驾驶黄色 Light Cycle 的 AI 选手在竞技场中对抗。随着关卡推进,AI 选手数量会不断增加。

由 Bally Midway 发行的 TRON 游戏在街机上广受好评,甚至还赢得了 Electronic Games Magazine 那项(想必)梦寐以求的 Coin-operated Game of the Year 奖项。

虽然这款街机游戏当时没有被移植到家用电脑上,但随后出现了许多类似游戏——以及明目张胆的克隆作品——例如那款名字毫不含蓄的 Light Cycle for the BBC Micro, Oric, and ZX Spectrum。

Light Cycle 小游戏本质上就是 Snake 的一个变种,玩家移动时会在身后留下尾迹。用 Pygame Zero 实现它有多种方式。

 

我们对 TRON Light Cycle 经典街机游戏的致敬。

 

在本示例中,我们将重点实现玩家 Light Cycle 的移动以及它移动时留下的尾迹。我们可以用画线函数绘制车身后的尾迹,也可以像 Snake 那样,在玩家移动时不断往尾迹中添加方块。

不过在这个例子中,我们将使用二维列表作为屏幕位置的矩阵。这意味着玩家在屏幕上移动到的任何位置,我们都可以将其标记为已访问,或检查它是否已被访问过;如果是,则触发游戏结束事件。

对于 maindraw() function, we first blit our background image which is the cross-hatched arena, then we iterate through our two-dimensional list of screen positions (each 10 pixels square) displaying a square anywhere the Cycle has been. The Cycle is then drawn and we can add a display of the score.

该update() function contains code to move the Cycle and check for collisions. We use a list of directions in degrees to control the angle the player is pointing, and another list of x and y increments for each direction. Each update we add x and y coordinates to the Cycle actor to move it in the direction that it’s pointing multiplied by our speed variable.

我们有一个on_key_down() function defined to handle changing the direction of the Cycle actor with the arrow keys. We need to wait a while before checking for collisions on the current position, as the Cycle won’t have moved away for several updates, so each screen position in the matrix is actually a counter of how many updates it’s been there for.

然后,我们可以在测试方块碰撞前先确认是否已经发生了 15 次更新,这样我们的 Cycle 就有足够时间离开该区域。如果检测到碰撞,就可以开始游戏结束流程。

我们设置gamestate variable to 1, which then means the update() function uses that variable as a counter to run through the frames of animation for the Cycle’s explosion. Once it reaches the end of the sequence, the game stops.

我们定义了一个按键(SPACE bar) in the on_key_down() function to call our init() function, which will not only set up variables when the game starts but sets things back to their starting state.

下面是 Mark 为 TRON 风格 Light Cycle 小游戏编写的代码。要在你的系统上运行它,你需要安装Pygame Zero. 要下载完整的代码和资源文件,点击这里.

这就是玩家 Light Cycle 移动和碰撞检测的基本原理。想让它更接近原版街机游戏,为什么不试着修改代码并添加几个电脑控制的对手呢?

获取你的 Wireframe 第 47 期

你可以在本期杂志中阅读更多类似内容Wireframe 第 47 期,可直接从树莓派出版社购买——我们提供全球配送。

如果你想要一份方便的电子版杂志,也可以下载第 47 期对于免费的PDF格式版本。

6条评论

Oreo avatar

那个了不起的伙计…

Zane avatar

等我买到 Raspberry Pi 4 或 400 后,我要安装 Pygame Zero 并编写这个游戏。

Zane avatar

我说的是我,不是我的。

Kaelon avatar

太棒了!真的很期待通过这款游戏重温旧日时光。;)

Rob James avatar

太棒了!伙计…

french avatar

非常厉害的人

评论已关闭