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

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

制作一款类似《大金刚》风格的步行自行车|线框图第14期

有效的动画赋予了《大金刚》大量的个性。Raspberry Pi 的里克·克罗斯(Rik Cross)解释了如何创建类似的行走循环。

《大金刚》并不是第一个拥有能够行走和跳跃的动画角色的游戏,但在1981年发布时,它确实比之前的游戏更有个性。你只需要将《大金刚》与两年前发布的另一款任天堂街机游戏——半被遗忘的俯视射击游戏《警长》进行比较,就可以看出在这短短的一段时间内技术和像素艺术进步得有多快。尽管按照现代标准来看很简单,《大金刚》中的英雄Jumpman(后来被称为马里奥)仅用了几帧动画就充满了动作感和个人特色。

在这篇文章中,我将向你展示如何使用Python和Pygame来创建一个像《大金刚》中Jumpman那样的简单行走循环动画的角色。然而,这段代码可以适用于任何需要动画的游戏对象,甚至多个游戏对象的动画,正如我稍后会解释的那样。

Jumpman(又名马里奥)的行走循环只包含三帧动画。

首先,我们需要一些用来制作动画的图像。由于本文重点在于动画代码而非创建行走循环图像背后的理论,我选取了一些由Kenney Vleugels创作并可在opengameart.org.

获取的适合图像。让我们从用简单的行走循环来为玩家角色添加动画开始。用于动画中的两张图片存储在一个images list, and an animationindex variable keeps track of the index of the current image in the list to display. So, for a very simple animation with just two different frames, the images list will contain two different images:

images = [‘walkleft1’,‘walkleft2’

为了实现循环动画,animationindex is repeatedly incremented, and is reset to 0 once the end of the images list is reached. Displaying the current image can then be achieved by using the animationindex to reference and draw the appropriate image in the animation cycle:

self.image = self.images[self.state][self.animationindex]

使用一系列图像加上索引来循环遍历动画周期。

到目前为止描述的代码的问题在于animationindex is incremented once per frame, and so the walk cycle will happen way too quickly, and won’t look natural. To solve this problem, we need to tell the player to update its animation every few frames, rather than every frame. To achieve this, we need another couple of variables; I’ll use animationdelay to store the number of frames to skip between displayed images, and animationtimer to store the number of frames since the last image change.

因此,为玩家角色添加动画所需的代码变为:

self.animationtimer += 1
if self.animationtimer >= self.animationdelay:
self.animationtimer = 0
self.animationindex += 1
if self.animationindex > len(self.images) - 1:
self.animationindex = 0
self.image = self.images[self.animationindex]

这样我们就有了一个看似在行走的玩家角色,但问题是这个玩家一直在走,并且总是朝同一个方向!本文的其余部分将向您展示如何解决这两个相关问题。

有几种不同的方法可以解决这个问题,但我将采用的方法是利用游戏对象状态,然后为每个状态设置不同的动画。这种方法稍微复杂一些,但它非常灵活。

首要的事情是确定玩家可能处于的“状态”是什么 —stand, walkleft, and walkright will do as a start. Just as we did with our previous single animation, we can now define a list of images for each of the possible player’s states. Again, there are lots of ways of structuring this data, but I’ve opted for a Python dictionary linking states and image lists:

self.images = { ‘stand’ : [‘stand1’],
‘walkleft’ : [‘walkleft1’,‘walkleft2’],
‘walkright’ : [‘walkright1’,‘walkright2’]
}

玩家的state can then be stored, and the correct image obtained by using the value of state along with the animationindex:

self.image = self.images[self.state][self.animationindex]

之后可以通过获取键盘输入来设置正确的玩家状态,将玩家设置为walkleft if the left arrow key is pressed or walkright if the right arrow key is pressed. If neither key is pressed, the player can be set to a stand state; the image list for which contains a single image of the player facing the camera.

动画循环可以与玩家的“状态”关联。

为了简化,每个动画循环最多使用两张图片;添加更多图片会使动画更加流畅或更逼真。

使用上述代码,还可以轻松地为跳跃或与敌人战斗等动作添加额外的状态。你甚至可以通过定义一个Animation() object for each player state. This way, you could specify the speed and other properties (such as whether or not to loop) for each animation separately, giving you greater flexibility.

这是Rik用Python编写的行走动画循环。要让它在你的系统上运行,你首先需要安装Pygame Zero。要下载完整代码,请点击此处。

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

您可以在以下位置阅读更多类似的文章:《Wireframe》第14期,现已在特易购、WHSmith以及所有优质的英国独立报刊店发售。

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

请关注《线框图》的和以及脸书以获取更新、独家优惠及抽奖活动信息。在线框图网站上订阅订阅可比零售价节省高达49%!

暂无评论

评论已关闭