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

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

编写你自己的《管道狂热》(Pipe Mania)益智游戏 | 《Wireframe》第46期

在水流开始涌出之前,构建一套管道网络——我们对这款经典益智游戏的复刻版。乔迪·桑通哈(Jordi Santonja)向你演示实现方法.

A screen grab of the game in motion
《管道狂热》的设计极为出色,因此它以各种形式出现在其他作品中——甚至作为《生化奇兵》(BioShock)中的一个小游戏出现。

《管道狂热》(Pipe Mania),在美国市场也称作《管道梦》(Pipe Dream),是由The Assembly Line公司于1989年为Amiga、Atari ST和PC平台开发的一款益智游戏,之后被移植到包括街机在内的其他平台。玩家需将随机生成的管道片段放置到网格上;当倒计时归零时,水流开始流动,必须经由相连的管道尽可能远地流过。

我们来看看如何使用Python和Pygame Zero复刻《管道梦》(Pipe Dream)。变量start is decremented at each frame. It begins with a value of 60*30, so it reaches zero after 30 seconds if our monitor runs at 60 frames per second. In that time, the player can place tiles on the grid to build a path. Every time the user clicks on the grid, the last tile from nextTiles is placed on the play area and a new random tile appears at the top of the next tiles. randint(2,8) computes a random value between 2 and 8.

我们的《管道狂热》致敬之作:在水流逃逸前搭建一条管道,并尝试打破自己的最高分纪录。

grid and nextTiles are lists of tile values, from 0 to 8, and are copied to the screen in the draw function with the screen.blit operation. grid is a two-dimensional list, with sizes gridWidth=10 and gridHeight=7. Every pipe piece is placed in grid with a mouse click. This is managed with the Pygame functions on_mouse_move and on_mouse_down, where the variable pos contains the mouse position in the window. panelPosition defines the position of the top-left corner of the grid in the window. To get the grid cell, panelPosition is subtracted from pos, and the result is divided by tileSize with the integer division //. tileMouse stores the resulting cell element, but it is set to (-1,-1) when the mouse lies outside the grid.

该images folder contains the PNGs with the tile images, two for every tile: the graphical image and the path image. The tiles list contains the name of every tile, and adding to it _block or _path obtains the name of the file. The values stored in nextTiles and grid are the indexes of the elements in tiles.

wfmag46code
这是乔迪编写的类《管道狂热》(Pipe Mania)益智游戏代码。要在你的系统上运行它,你需要安装Pygame Zero. 要下载完整的代码和资源文件,前往此处。

图片waterPath isn’t shown to the user, but it stores the paths that the water is going to follow. The first point of the water path is located in the starting tile, and it’s stored in currentPoint. update calls the function CheckNextPointDeleteCurrent, when the water starts flowing. That function finds the next point in the water path, erases it, and adds a new point to the waterFlow list. waterFlow is shown to the user in the draw function.

pointsToCheck contains a list of relative positions, offsets, that define a step of two pixels from currentPoint in every direction to find the next point. Why two pixels? To be able to define the ‘cross’ tile, where two lines cross each other. In a ‘cross’ tile the water flow must follow a straight line, and this is how the only points found are the next points in the same direction. When no next point is found, the game ends and the score is shown: the number of points in the water path, playState is set to 0, and no more updates are done.

获取《Wireframe》第46期纸质/电子版

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

wfcover

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

9条评论

teatv apk download avatar

看起来很复杂,但我正努力学习《管道狂热》,用于我的一个项目。谢谢您,先生!您的帮助非常宝贵。

Harry Hardjono avatar

下载链接跳转到了第45期?请修复,谢谢!

Ashley Whittaker avatar

已修复!谢谢!

Steve Spence avatar

仍未修复,链接跳转到了第45页,而非第46页。

Harry Hardjono avatar

你需要点击单词“for” :)

Steve Spence avatar

此游戏似乎无法在 Python 3.8.3 和 Pygame 1.9.6 环境下运行。

Steve Spence avatar

因此,以下是修复方法:需补充两行缺失的代码。
最开头的第一行:
import pgzrun
最后一行:
pgzrun.go()

swapnil avatar

谢谢兄弟!我一直在纳闷为什么我的代码无法运行。

Stephen Scott avatar

你或许会感兴趣:这款游戏中文版曾在1990年末以BBC Basic语言形式发布,作为一篇可手动输入的程序清单。你不仅可通过 JSbeeb 在以下网址运行该游戏:http://bbcmicro.co.uk//game.php?id=1021,还能浏览其源代码——该代码是 6502 汇编语言与 BASIC 的混合体。这是一个相当出色的版本,采用明亮的四色图形。

评论已关闭