创建你自己的街机风格续关画面 | Wireframe #19
Raspberry Pi 的 Rik Cross 向你展示如何创建游戏状态,以及在不同状态之间切换的规则。

Ninja Gaiden 极具戏剧性的续关画面。谁会狠心离开呢?
续关画面虽然现在已不多见,但它曾是街机游戏的标配功能,提供了(以少量费用)让游戏主角复活并继续之前进度的机会。
续关画面
像 Tecmo 的 Ninja Gaiden 街机(在某些地区名为 Shadow Warriors)这样的游戏,为它们的续关画面增添了紧张感,以图让我们掏钱。
续关画面通常只是玩家可能身处其中的众多画面之一;其他可能还有标题画面或说明画面。我将向你展示如何以结构化的方式为游戏添加多个画面,避免一团if…else statements and variables.
解决这个问题的一个简单方法是为每个画面创建独立的 update 和 draw 函数,然后根据需要在这些函数之间切换。函数是 Python 语言中的“一等公民”,这意味着它们可以像数字、文本和类实例等其他对象一样被存储和操作。它们可以存储在变量以及列表和字典等其他数据类型中,也可以作为参数传递给(或从其他函数返回)其他函数。

SNK 于 1981 年发布的 Fantasy 是首款带有续关画面的街机游戏。
我们可以利用 Python 函数的一等特性,将当前画面的函数存储在变量中,然后在 mainupdate() and draw() functions. In the following example, notice the difference between storing a function in a variable (by using the function name without parentheses) and calling the function (by including parentheses).
[编辑评论:我们在这里不得不使用图片,因为 WordPress 似乎不允许代码缩进。我们知道这很烦人,因为你无法复制粘贴代码,所以如果你知道更好的解决方案,请在评论中告诉我们。]
上面的示例代码调用currentupdatefunction() and currentdrawfunction(), which each store a reference to separate update() and draw() functions for the continue screen. These continue screen functions could then also include logic for changing which function is called, by updating the function reference stored in currentupdatefunction and currentdrawfunction.
这种代码组织方式可以更进一步,方法就是使用状态机。在状态机中,系统可以处于(有限个)预定义状态之一,规则决定了系统在什么条件下可以从一个状态转换到另一个状态。

规则定义了在状态之间移动所需满足的条件。
状态机(在本例中是一个非常简化的版本)可以通过首先创建一个核心State() class. Each game state has its own update() and draw() methods, and a rules dictionary containing state:rule pairs – references to other state objects linked to functions for testing game conditions. As an example, the continuescreen state has two rules:
- 转换到
gamescreenstate if theSPACEkey is pressed; - 转换到
titlescreenstate if the frame timer reaches 10.
这是通过一个StateMachine() class, which keeps track of the current state. The state machine calls the update() and draw() methods for the current state, and checks the rules for transitioning between states. Each rule in the current state’s rules list is executed, with the state machine updating the reference to its current state if the rule function returns True. I’ve also added a frame counter that is incremented by the state machine’s update() function each time it is run. While not a necessary part of the state machine, it does allow the continue screen to count down from 10, and could have a number of other uses, such as for animating sprites.
另外需要指出的是,在状态中添加规则时使用了 lambda 函数。Lambda 函数是较小的单表达式匿名函数,在被调用时返回其表达式求值的结果。在这个例子中使用 lambda 函数只是为了让代码更简洁一些,因为为传递给addrule().
状态机还有许多其他潜在用途,包括建模玩家状态。在这个例子中,还可以通过添加onenter() and onexit() functions that can be called when transitioning between states.

这是 Rik 的代码,它用 Python 实现了一个简单的续关画面。要让它在你的系统上运行,你需要安装Pygame Zero。要下载完整代码,访问我们的GitHub仓库:这里。
获取你的 Wireframe 第 19 期
你可以在本期杂志中阅读更多类似内容Wireframe 第 19 期,现已在特易购、WHSmith以及所有优质的英国独立报刊店发售。
或者你可以直接从树莓派出版社购买从树莓派出版社直接购买线框图——全球配送均可。如果你想要一份方便的电子版杂志,还可以免费下载第 19 期的PDF格式版本。


4条评论
乔恩·威茨(Jon Witts)
这是我在 WordPress 博客中使用的代码插件……http://alexgorbatchev.com/SyntaxHighlighter/
乔恩·威茨(Jon Witts)
这是实际 WordPress 插件的链接……https://wordpress.org/plugins/syntaxhighlighter/
Jed Fox
你也可以嵌入一个 [GitHub Gist](https://gist.github.com)。
Benjamin
同意代码需要以文本形式呈现……图片无法被 screen-readers 读取,这对 accessibility 来说是个大问题。
评论已关闭