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

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

创建类似《Scramble》的卷轴式地形 | 《Wireframe》第22期

穿越随机生成的地形,马克·范斯通向经典街机游戏《Scramble》致敬。

《Scramble》由科乐美(Konami)开发,于1981年在街机平台发行。玩家需躲避地形障碍并击毁敌方飞行器。

科乐美《Scramble》

20世纪80年代初,街机厅与体育场馆中回荡着各式电子游戏的音效。由于家用电脑尚未普及至大多数家庭,狂热的电子游戏玩家唯一的选择,便是前往本地娱乐场所,用十便士硬币(当时面值更大)投币游玩。其中一台“吞噬零花钱”的机器,便是科乐美于1981年推出的《Scramble》——它是最早一批具备多关卡设计的横向卷轴射击游戏之一。

在《Scramble》中,玩家操控喷气式战机飞越随机生成的地形(有时会收窄为洞穴系统),避开障碍物与敌机、轰炸地面目标,并竭力避免坠毁。随着游戏推进,难度逐步提升。战机只能向前飞行,一旦错过目标,便无法折返重试。

编写你自己的卷轴式地形

在本示例代码中,我将为你展示一种使用 Pygame Zero 及若干额外 Pygame 函数生成《Scramble》风格卷轴地形的方法。在早期计算机上,屏幕间大量数据移动速度极慢——直至专用视频硬件(如“光栅操作芯片”blitter chip)问世。而卷轴效果则可通过两种方式实现:一是快速将显存中的字节向左或向右平移;二是在某些情况下,直接修改显存起始地址,速度更快。

使用方向键避开顶部与底部障碍。喷气机图形由 TheSource4Life 提供,来源:opengameart.org.

对于我们的卷轴效果,可使用一个与屏幕尺寸相同的 Pygame Surface。要实现卷轴效果,只需调用scroll() function on the surface to shift everything left by one pixel and then draw a new pixel-wide slice of the terrain. The terrain could just be a single colour, but I’ve included a bit of maths-based RGB tinkering to make it more colourful. We can draw our terrain surface over a background image, as the SRCALPHA flag is set when we create the surface. This is also useful for detecting if the jet has hit the terrain. We can test the pixel from the surface in front of the jet: if it’s not transparent, kaboom!

喷气机本身是 Pygame Zero 的Actor and can be moved up and down with the arrow keys. The left and right arrows increase and decrease the speed. We generate the landscape in the updateLand() and drawLand() functions, where updateLand() first decides whether the landscape is inclining or declining (and the same with the roof), making sure that the roof and floor don’t get too close, and then it scrolls everything left.

每次卷轴操作均使地形 Surface 上所有内容向左移动一个像素。

该drawLand() function then draws pixels at the right-hand edge of the surface from y coordinates 0 to 600, drawing a thin sliver of roof, open space, and floor. The speed of the jet determines how many times the landscape is updated in each draw cycle, so at faster speeds, many lines of pixels are added to the right-hand side before the display updates.

使用randint() can be changed to create a more or less jagged landscape, and the gap between roof and floor could also be adjusted for more difficulty. The original game also had enemy aircraft, which you could make with Actors, and fuel tanks on the ground, which could be created on the right-hand side as the terrain comes into view and then moved as the surface scrolls. Scramble sparked a wave of horizontal shooters, from both Konami and rival companies; this short piece of code could give you the basis for making a decent Scramble clone of your own:

以下是马克编写的代码,可在 Python 中运行出《Scramble》风格的卷轴地形。要在你的系统上运行该代码,首先需安装Pygame Zero。要下载完整代码,请点击此处.

获取《Wireframe》第22期

你可以在本期杂志中阅读更多类似内容《Wireframe》第22期,现已在特易购(Tesco)、W.H.史密斯(WHSmith)及英国各大优质独立报刊亭,以及剑桥的树莓派商店(Raspberry Pi Store)发售。

或者您也可以直接从树莓派出版社购买《线框图》——全球配送。如果您想要一份方便的电子版杂志,还可以免费下载第22期的PDF格式版本。

请关注《线框图》的和以及脸书以获取更新和独家优惠及赠品。在《Wireframe》网站订阅可比零售价节省最多49%!

2条评论

AWS Guy avatar

图片下方有一个超级有用的 GitHub 仓库链接!我第一次阅读时差点就错过了。写得不错——我很喜欢这些游戏教程。

Extraminogame avatar

这张图片让我回忆起过去,我的童年时光。

评论已关闭