创建回合制战斗系统 | 线框图 #28
学习如何创建像《宝可梦》、《最终幻想》和《传说之下》这类游戏中常见的回合制战斗系统。树莓派的Rik Cross将为你演示。

由于强调交换与收集,以及回合制战斗,《宝可梦》系列游戏帮助将角色扮演游戏的概念带到了大众之中。
20世纪70年代末,高中生理查德·加里奥特制作了一款名为《阿卡莱贝斯》的小游戏。该游戏使用Applesoft BASIC编写,为计算机上的角色扮演游戏奠定了模板。即使在今天,回合制战斗依然是游戏中常见的元素,今年秋季推出的《宝可梦 剑/盾》就围绕一个让对手轮流策划并执行攻击或防御动作的战斗系统展开。
本文中的回合制战斗系统仅以文本形式呈现,通过允许玩家轮流选择攻击或防御对手来实现。当只剩一名玩家还拥有生命值时,战斗结束。
每个Player taking part in the battle is added to the static players list as it’s created. Players have a name, a health value (initially set to 100) and a Boolean defending value (initially set to False) to indicate whether a player is using their shield. Players also have an inputmethod attribute, which is the function used for getting player input for making various choices in the game. This function is passed to the object when created, and means that we can have human players that give their input through the keyboard, as well as computer players that make choices (in our case simply by making a random choice between the available options).

理查德·加里奥特的《阿卡莱贝斯》为《创世纪》系列奠定了基础,也是最早的电脑角色扮演游戏之一。
基础 Action class specifies an action owner and an opponent, as well as an execute() method which has no effect on the game. Subclasses of the base class override this execute() method to specify the effect the action has on the owner and/or the opponent of the action. As a basic example, two actions have been created: Defend, which sets the owner’s defending attribute to True, and Attack, which sets the owner’s defending attribute to False, and lowers the opponent’s health by a random amount depending on whether or not they are defending.
玩家轮流在战斗中选择一项行动,从人类“英雄”玩家开始。每轮结束后,choose_action() method is used to decide what to do next (in this case either attack or defend), as well as an opponent if the player has chosen to attack. A player can only be selected as an opponent if they have a health value greater than 0, and are therefore still in the game. This choose_action() method returns an Action, which is then executed using its execute() method. A few time.sleep() commands have also been thrown in here to ramp up the suspense!
会检查是否至少仍有两名玩家仍具有health value greater than 0, and therefore that the battle can continue. If so, the static get_next_player() method finds the next player still in the game to take their turn in the battle, otherwise, the game ends and the winner is announced.
我们的示例战斗可以很容易地扩展出许多有趣的方式。选择行动的人工智能也可以变得更复杂,例如通过分析对手的health or defending attributes before choosing an action. You could also give each action a ‘cost’, and give players a number of action ‘points’ per turn. Chosen actions would be added to a list, until all of the points have been used. These actions would then be executed one after the other, before moving on to the next player’s turn.

这是Rik的代码,它可在Python中运行一个简单的回合制战斗系统。要使它在你的系统上正常工作,你首先需要安装Pygame Zero。要下载完整代码,请点击此处。
你可以在本期杂志中阅读更多类似内容线框图第28期现已在特易购、WHSmith、所有优质的英国独立报刊亭以及剑桥的树莓派商店有售。
或者您也可以直接从树莓派出版社购买《线框图》——全球配送。如果您想要一份方便的电子版杂志,还可以免费下载第28期的PDF格式版本。

暂无评论
评论已关闭