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

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

制作一款键盘狂轰战跑酷游戏 | Wireframe 第23期

学习如何用树莓派的原生功能,直接从戴利·汤普森的“十项全能”中打造一款冲刺小游戏里克·克罗斯.

在科乐美“超级运动”大获成功的基础上,“戴利·汤普森的十项全能”推出了大量能彻底毁掉控制器的游戏迷你游戏。

戴利·汤普森的十项全能

1984年发行的“戴利·汤普森的十项全能”,是有时被称作“操纵杆杀手”类型中令人难忘的一部作品:玩家在各类体育赛事中展开角逐,而这些赛事大多需要玩家疯狂地摇动控制器,或是猛烈敲击键盘。接下来,我将向大家展示如何使用 Python 和 Pygame 来实现一款冲刺类游戏机制。

Python 突击赛游戏

游戏中包含一些变量Sprinter() class to keep track of the runner’s speed and distance, as well as global constant ACCELERATION and DECELERATION values to determine the player’s changing rate of speed. These numbers are small, as they represent the number of metres per frame that the player accelerates and decelerates.

玩家通过交替按下左右方向键,来提升冲刺者的速度。这一输入由冲刺者自身的isNextKeyPressed() method, which returns True if the correct key (and only the correct key) is being pressed. A lastKeyPressed variable is used to ensure that keys are pressed alternately. The player also decelerates if no key is being pressed, and this rate of deceleration should be sufficiently smaller than the acceleration to allow the player to pick up enough speed.

交替按下左右方向键,以提升冲刺者的速度。物体从屏幕右侧移动至左侧,营造出冲刺者不断向前奔跑的错觉。

在动画制作上,我使用了来自 gameart2d.com 的免费精灵“男孩”。,并利用了一张空闲状态的图像以及15张奔跑循环的图像。冲刺者初始处于空闲状态,但每当其速度大于0时,便会切换到奔跑循环模式。实现这一效果的方法是通过为了营造出运动的幻觉,我在游戏中加入了会从玩家身边掠过的物体:设有终点线和三个标记,用于定期显示玩家所行进的距离。这些物体的移动距离,是根据冲刺者在屏幕上的 x 坐标,结合其已行进的距离来计算得出的。不过,这意味着每个物体与玩家之间的距离最多只有100像素,因此看起来移动得相当缓慢。可以通过使用index() to find the name of the current sprinter image in the runFrames list, and setting the current image to the next image in the list (and wrapping back to the first image once the end of the list is reached). We also need the sprinter to move through images in the run cycle at a speed proportional to the sprinter’s speed. This is achieved by keeping track of the number of frames the current image has been displayed for (in a variable called timeOnCurrentFrame).

最后,SCALE factor, which is the relationship between metres travelled by the sprinter and pixels on the screen. This means that objects are initially drawn way off to the right of the screen but then travel to the left and move past the sprinter more quickly.

这里是里克编写的代码,它让一款冲刺游戏在 Python 中顺利运行(无意中打了个双关)。要让它在你的系统上正常运行,你首先需要startTime and finishTime variables are used to calculate the race time. Both values are initially set to the current time at the start of the race, with finishTime being updated as long as the distance travelled is less than 100. Using the time module, the race time can simply be calculated by finishTime - startTime.

下载代码安装Pygame Zero获取《Wireframe》第23期的副本这里.

《Wireframe》第23期

你可以在本期杂志中阅读更多类似内容或者,你也可以直接从树莓派出版社购买《Wireframe》——全球范围内均可配送。如果你想要一份方便的杂志数字版,还可以现已在特易购、WHSmith、所有优质的英国独立报刊亭以及剑桥的树莓派商店有售。

免费下载第23期自航者即将以萌趣之姿,登陆你的电脑,开启一场全新的冒险之旅。更多精彩内容,尽在《Wireframe》第23期。的PDF格式版本。

制作一种类似“乱斗”风格的滚动景观 | Wireframe 第22期

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

暂无评论

评论已关闭