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

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

重现《Flappy Bird》的飞行机制 | 《Wireframe》第29期

摘自去年《Wireframe》杂志第29期:学习如何创建属于你自己的简易却令人上瘾的横版滚动游戏《Flappy Bird》。Raspberry Pi 的里克·克罗斯(Rik Cross)将为你演示。

《Flappy Bird》:2014 年曾风靡一时(至少在一段时间内)。

《Flappy Bird》由程序员 Dong Nguyen 于 2013 年发布,凭借简洁直观的游戏机制成为一款令人上瘾的热门作品。玩家点击屏幕为角色提供“升力”,从而策略性地穿越一系列横向移动的管道。每成功穿过一根管道即可获得一分。该创意如此令人上瘾,以至于 Nguyen 最终后悔创造了它,并将游戏从 Google 和 Apple 应用商店下架。本文中,我将向你展示如何使用 Python 和 Pygame Zero 复刻这款看似简单却极其耗时的游戏。

玩家的运动方式与标准平台跳跃游戏非常相似:在重力作用下持续向下坠落至屏幕底部。更多关于此类运动的实现方法,请参阅《Wireframe》第7期文章《超级马里奥风格跳跃物理》。按下按键(本例中为SPACE bar) gives the player some upward thrust by setting its velocity to a negative value (i.e. upwards) larger than the value of gravity acting downwards. I’ve adapted and used two different images for the sprite (made by Imaginary Perception and available on opengameart.org),使其看起来像正在扇动翅膀以产生升力并向上移动。

按下空格键(SPACE)可为小鸟提供对抗重力的“升力”,助其穿越移动的管道。

管道组在水平方向上等距排列,并在游戏每一帧中缓慢向玩家移动。这些管道以两个矩形列表的形式存储,top_pipes and bottom_pipes, so that the player can attempt to fly through gaps between the top and bottom pipes. Once a pipe in the top_pipes list reaches the left side of the screen past the player’s position, a score is incremented and the top and corresponding bottom pipes are removed from their respective lists. A new set of pipes is created at the right edge of the screen, creating a continuous challenge for the player. The y-position of the gap between each newly created pair of pipes is decided randomly (between minimum and maximum limits), which is used to calculate the position and height of the new pipes.

若玩家与管道或地面发生碰撞,游戏即终止,并显示“Game over”(游戏结束)提示。本游戏中的碰撞检测采用player.colliderect() method, which checks whether two rectangles overlap. As the player sprite isn’t exactly rectangular, it means that the collision detection isn’t pixel-perfect, and improvements could be made by using a different approach. Changing the values for GRAVITY, PIPE_GAP, PIPE_SPEED, and player.flap_velocity through a process of trial and error will result in a game that has just the right amount of frustration! You could even change these values as the player’s score increases, to add another layer of challenge.

这是 Rik 编写的代码,它用 Python 实现了一个致敬《Flappy Bird》的游戏。要在你的系统上运行它,你首先需要安装Pygame Zero。要下载完整代码,请点击此处。

如果你想阅读《Wireframe》杂志往期内容,可在以下位置找到完整过刊合集(免费 PDF 下载).

《Wireframe》最新一期已上线 Raspberry Pi Press 商店,支持在线购买纸质版购买;往期纸质刊物也大幅折扣销售中。你也可以在当地报亭购买《Wireframe》,但当前我们应尽可能居家隔离,因此何不在线购入一本,省去出门一趟?

请关注《线框图》的和以及脸书获取更新、独家优惠及赠品信息。同时请在《线框图》官网订阅可比零售价节省高达49%!

7条评论

Dan Aldred avatar

另外,不妨看看 Raspberry Pi 使用人工智能学习玩《Flappy Bird》的项目——https://www.youtube.com/watch?v=QfOzZpMFFl8&safe=active

Gavin McIntosh avatar

直到看到这个《Flappy Bird》版本之前,我从未听说过 NEAT。
https://github.com/techwithtim/NEAT-Flappy-Bird
我惊讶于它学习的速度之快。
这在一定程度上说明了特斯拉Autopilot的碰撞规避功能其实很简单?它应该能比我更熟练地玩《兔子》/《青蛙过河》这类游戏吧?

Ishan Thapar avatar

真的很棒……让我想起了自己玩电子游戏的岁月……

empleosgt avatar

真令人惊叹……让我想起了自己玩电子游戏的岁月……

Robert avatar

不错!我一直很喜欢《Flappy Bird》。

mobilbahis avatar

天哪,太棒了!

Binharris avatar

《Flappy Bird》是我有史以来最喜欢的游戏之一!另外,感谢分享…… :)

评论已关闭