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

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

用 Python 和 Pygame Zero 自行编写一款俯视角《塞尔达传说》风格游戏 | 线框图 #61

使用 Python 和 Pygame Zero 创作属于你自己的海拉鲁大陆致敬之作。马克·范斯通在最新一期中提供了代码《线框图》杂志,现已上市。


1986 年发布的《塞尔达传说》开创了前所未有的自由度,超越了任天堂此前所有作品的冒险体验。作为勇敢的英雄林克,玩家可以自由选择任意方向前进,挥舞剑刃斩杀敌人,并探索充满奇幻色彩的海拉鲁世界中的各种秘密。原版游戏的成功不言而喻,其后诞生了众多广受欢迎的续作与衍生作品,整个系列对游戏设计产生了深远影响。

The Legend Of Zelda on the NES
林克在寻找公主塞尔达的旅途中对抗着各种怪物。

在我们的 Pygame Zero 重制版中,我们将聚焦于原始 NES 版本的俯视角玩法。我们将创建一个由方块构成的地图,随着林克的探索而滚动;同时我们还会加入一些敌人,让他用信赖的剑来击败。

首先,让我们生成一个塞尔达风格地图。屏幕元素由网格排列的方形方块组成。其中包括代表树木、岩石和其他地形(如水域)的方块。在此示例中,我们仅使用少量方块类型,但你可以自行添加更多。若需为这类复古项目寻找图形素材,spriters-resource.com值得一试——我们项目中使用的精灵图由 MisterMike 制作。当设计一个比屏幕大得多的区域的地图系统时,我们不能简单地用一张巨大图像存储,因此需要将其分解为一个个小方块。本例中,我们将使用 50 像素见方的方块,可视地图区域为宽 16 个方块、高 10 个方块。

我们可以把所有地图数据存放在一个巨大的二维列表中,每个方块用一个数字表示,但这意味着即使是一个较小的地图,也会产生大量冗长的数据,输入起来非常枯燥。更聪明的做法是:将整张地图缩小为实际大小的 1/50 的图像,每个像素代表一个方块。地面用黑色像素表示,树木用绿色像素,岩石用黄色,巨石则用红色。然后我们可以读取当前屏幕上显示的迷你地图区域,并将其转换为我们的地图方块。

An homage to The Legend of Zelda made in Python and Pygame Zero.
我们对《塞尔达传说》的 Python 与 Pygame Zero 致敬之作。

当我们绘制地图方块时,会从屏幕下方 100 像素处开始绘制,以便在顶部留出迷你地图的空间。因此,我们只需为drawMap() function is run through an embedded loop of x and y values to translate the pixel colours (which we read using the image.get_at() function from the Pygame module) into our larger map blocks and blit them to the screen in the correct positions.
我们正在显示的地图部分由变量mapx and mapy. If we want to see a different part of the map then we just need to change those values and that area will be displayed on the next draw cycle. To make the map scroll rather than just switch, we have the variables mapScrollx and mapScrolly. If we set the mapScrollx to 16, we can count down with that variable each update cycle and increase the mapx value by one each time. This means that in 16 cycles, the viewing area of the map will have moved 16 blocks to the right. We can use the same process for negative scroll values and for the y axis, but in that case, only scrolling 10 blocks at a time.

英雄时间

现在我们已经准备好添加主角。林克将由一个 Actor 对象表示,我们在动画的第一帧初始化它,并放置在游戏区域的中间位置。每一帧都会在draw() function, but we’ll want to add some extra code in a function called drawChars(). To get Link to move around the map, we’ll define some keys to move in four directions. We could limit Link so he can only move from one block to the next, but this can make the movement look rather jerky, so we’re going to allow Link to move a couple of pixels each time we detect that a key is held down. This means we need to work out which block he’s moving towards, and if it’s not a ground block (represented on our minimap by black pixels), we cancel the move. If Link does move, we need to cycle his animation frames using a frame counter, and then allocate an image based on that frame counter and the direction he’s facing.

The code listing for Wireframe's Zelda homage.
这是马克实现的俯视角《塞尔达传说》风格游戏代码。要在你的系统上运行,首先需要安装 Pygame Zero——完整安装说明请访问wfmag.cc/pgzero。完整代码请前往我们的 GitHub 仓库.

现在林克已在屏幕上移动,我们需要在他到达边缘时启用屏幕滚动功能。通过检测他是否到达 x<0 或 x>800,或 y>600 或 y<100 来实现。一旦触发,我们就可以设置mapScrollx or mapScrolly variables; this will trigger the scrolling mechanism which will take us to the next section of the map. While we’re scrolling, however, we need to make sure other objects on the map also scroll so that they remain in the correct position relative to the map.

一旦林克能在不同屏幕间自由移动,我们便需要加入一些敌人供他战斗。我们将以 Actor 的形式创建一些怪物,并随机散布在地图上。为了让它们有一定活动范围,我们可以在更新循环中加入逻辑,使它们在与林克处于同一屏幕时朝他移动。我们可以通过与林克相同的方块碰撞检测方式来测试。

接下来,我们需要为林克添加挥剑战斗能力。当按下空间当按下空格键时,会将剑作为一个独立的演员(Actor)触发。虽然可以通过改变剑的朝向来实现相同的效果,但这里使用了四个不同方向的图像。当该动画运行时,我们可以检测是否与任何怪物发生碰撞,如果发生碰撞,则触发状态变化以消灭它们。我们将怪物的状态从10改为9,然后在更新过程中通过倒计时并旋转演员,当该状态变为零时,敌人将不再显示。

我们要介绍的最后一部分是屏幕左上角的小地图。它指示林克在大地图上的位置,因此我们只需要将他在大地图上的坐标转换为小地图的尺寸,并绘制一个方块即可。

就这样——我们已经完成了俯视角《塞尔达》类游戏的基础!你可以添加各种各样的元素——药水和其他有用的物品、更多的敌人、隐藏路径等等——但和往常一样,这些都留给你自己去发挥想象吧。

获取你的《Wireframe》第61期副本

你可以在《Wireframe》第61期中阅读更多类似的文章,可直接从树莓派出版社购买——我们支持全球配送。

我们有全新订阅优惠! 订阅《Wireframe》一年,即可免费获得价值39英镑的8BitDoPro 2 蓝牙控制器。

Wireframe issue 61.

如果你喜欢电子版杂志,你也可以下载《Wireframe》第61期的免费PDF版!

4条评论

solar3000 avatar

在第一张图片里,那东西看起来像……呃,算了,别说了。

Buck avatar

啊……现在我再也无法忽视它了。

Nish avatar

任天堂是我们童年的挚爱。

Soundcloudtomp3downloader avatar

刚在Python课上完成了我的第一个“你好世界”。看来是时候开始制作塞尔达了

评论已关闭