编写你自己的《管道狂热》(Pipe Mania)益智游戏 | 《Wireframe》第46期
在水流开始涌出之前,构建一套管道网络——我们对这款经典益智游戏的复刻版。乔迪·桑通哈(Jordi Santonja)向你演示实现方法.

《管道狂热》(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.

图片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期,可直接从树莓派出版社购买——我们提供全球配送。

9条评论
teatv APK下载
看起来很复杂,但我正努力学习《管道狂热》,用于我的一个项目。谢谢您,先生!您的帮助非常宝贵。
哈里·哈吉诺
下载链接跳转到了第45期?请修复,谢谢!
树莓派团队艾什莉·惠特克
已修复!谢谢!
史蒂夫·斯彭斯
仍未修复,链接跳转到了第45页,而非第46页。
哈里·哈吉诺
你需要点击单词“for” :)
史蒂夫·斯彭斯
此游戏似乎无法在 Python 3.8.3 和 Pygame 1.9.6 环境下运行。
史蒂夫·斯彭斯
因此,以下是修复方法:需补充两行缺失的代码。
最开头的第一行:
import pgzrun
最后一行:
pgzrun.go()
斯瓦普尼尔
谢谢兄弟!我一直在纳闷为什么我的代码无法运行。
斯蒂芬·斯科特
你或许会感兴趣:这款游戏中文版曾在1990年末以BBC Basic语言形式发布,作为一篇可手动输入的程序清单。你不仅可通过 JSbeeb 在以下网址运行该游戏:http://bbcmicro.co.uk//game.php?id=1021,还能浏览其源代码——该代码是 6502 汇编语言与 BASIC 的混合体。这是一个相当出色的版本,采用明亮的四色图形。
评论已关闭