6.通过加速度来实现跳跃和下落.

6.What Goes Up Must Come Down: implementation falling and jumping through acceleration.

This post is about making things jump up and fall down (e.g. for platform games), or: modelling acceleration.

这篇帖子讨论如何跳跃及下落(例如平台游戏),或者说:模拟加速度。

When building 2D games and simulations in environments like Scratch or Greenfoot, you can either use the 2D view to provide a top-down view on the world, or a side-on view. Platform games (Sonic, Mario, etc) are the main examples of the latter, and in these games, you often want to make things fall down when there is no solid ground beneath them, and to allow the character to jump around. This post will look at how to do this: it’s actually best to start by implementing falling first, and then implement jumping.

在类似于Scratch或Greenfoot这样的环境中创建2D游戏或仿真程序的时候,你可以在游戏世界中提供俯视的2D视角,或者一个侧视的视角。平台游戏(索尼克,超级玛丽等)是后者的主要代表,在这些游戏中,当物体下方没有坚实的地面时你通常希望它们能够下落,同时希望让游戏角色能够跳跃。这篇帖子将探讨如何去实现:实际上最好从实现物体下落开始,然后实现跳跃。

Falling Down

下落

One way to make things fall is to move them down the screen at a constant rate. However, this often doesn’t look quite right — because it’s not the way that the real world works! Gravity produces an acceleration downwards, so when things fall down, they increase speed as they fall. In order to model the effect of acceleration, you must keep track of the current speed in a variable. Here’s the code in Greenfoot:

一种实现物体下落的办法是让它们以固定的速率向屏幕下方移动。然而这通常看起来不是很合适——因为现实世界中的情形不是这样的!由于重力造成了一个向下的加速度,因此当物体下落时,它们的速度是不断增加的。为了模仿加速度的效果,必须定义一个变量来保存当前的速度值。在Greenfoot中可用以下代码实现:

public class AccFall extends Actor
{
    double speed = 0;
    double exactY;

    public void act() 
    {
        speed += 0.4;
        exactY += speed;
        setLocation(getX(), (int)exactY);
    }    

    protected void addedToWorld(World world)
    {
        exactY = getY();
    }
}

This code is a bit more fiddly than it needs to be, due toGreenfoot using integers to store coordinates, and me using an acceleration to fall at that’s not a whole number (otherwise it’s a bit too fast). To visualise the difference between falling at a constant rate, and falling at an accelerating rate,I’ve coded both in the same scenario. You can watch by clicking the apples below — the green apple on the left-hand side falls at a constant rate, while the red apple on the right-hand side falls at an accelerating rate. Try covering up one side with your hand and just watch the other side, to try to see the difference:

以上代码中我们没使用整数来实现加速,实际上并不需要如此精确,因为Greenfoot使用整型来存储坐标值(因此显得稍微有点快)。为了可视化地显示匀速下落和加速下落的区别,我将两者编写到了同一个游戏剧本中。你可以通过点击下图所示的苹果来观察——左边的绿苹果匀速下落,右边的红苹果加速下落。试着用手盖住一个苹果然后观察另一个,看看两者有何区别:

Jumping Up

跳跃

The other half of implementing a platform game is allowing the character to jump. It turns out that this is quite simple to model: you can make the character jump up by setting the speed variable to be a large negative value (in effect, an instantaneous upwards acceleration). This causes the character to move upwards, and thereafter gravity takes over by reducing the speed (downwards acceleration is equivalent to a slowing of upwards speed) until the speed takes the character downwards again, just like the red apple was falling.

编写平台游戏的另一个任务是实现角色的跳跃。这很容易实现:可以通过将速度变量设为一个很大的负数值来实现角色的跳起(相当于一个向上的瞬时速度)。这将导致角色向上移动,在那之后重力将使得速度减慢(此时向下加速相当于向上减速)直到速度改变到足以使得角色重新下落,就如同红苹果下落的情形。

You may not have followed all that in text, so here’s how you can see it in action. I madea scenario with a frog on a log that can jump (press up to jump) . If you watch carefully, you can see that the frog slows until it hits the peak of its jump and then accelerates as it comes back down. Greenfoot has a useful “Inspect” feature that allows us to see exactly what is happening.

或许你曾经在书本上已经了解了这些原理,在这我们看看如何在实际中去应用。我制作了一个游戏剧本,其中一只站在木头上的青蛙可以跳跃(按up键起跳)。如果你仔细观察,你会发现青蛙缓慢上升直到跳跃的最高点,然后加速向下掉落。Greenfoot中有一个有用的“查看”功能让我们去精确地观察发生了什么。

 

If you want to play along yourself, open the scenario in Greenfoot. Hit compile in the bottom-right, and make sure you can see the frog on the log. Right-click on the Frog and go to “Inspect”. Move the inspect window so you can see it alongside the main Greenfoot window:

如果你想自己游戏,可以在Greenfoot中将它打开。点击右下角的编译按钮,确定你能看到木头上得青蛙。用鼠标右键点击青蛙并选择“查看”。 移动查看窗口以便你能够在Greenfoot主窗口旁边看到它,如图:

The two variables of interest are “int y” (currently 222) and “private int verticalSpeed” (currently 0). Right-click on the frog and select the method “void jump()” to call it. You’ll notice the frog will jump up (by 30: y becomes 192, and verticalSpeed becomes -30). That’s the instant upwards acceleration. Press the Act button once. You’ll see that verticalSpeed becomes -27, and y becomes 165 (which is 192 + (-27), old position plus vertical speed). Press it again, and you get -24 and 141 and so on. Press Act a few more times in quick succession and you’ll be able to see (looking at the frog) that the speed of the frog’s rise slows until it hits the peak of its jump, when verticalSpeed is zero. After that, verticalSpeed increases and the frog falls faster and faster downwards (keep clicking Act) until it hits the log again, when the speed is set back to zero.

需要关注的两个变量是“整型变量 y”(当前值是222)和“私有整型变量verticalSpeed"(当前值为0)。在青蛙上单击鼠标右键并调用“void jump()” 方法。你会发现青蛙将跳起来(跳起30个单位:y坐标值变为192,verticalSpeed值变为-30)。那便是向上的瞬时加速。按一次“Act”按钮,你将发现verticalSpeed 值变为-27,y值变为165(192+(-27),即旧坐标加上垂直速度值)。再按一次,上两个值将变为-24和141,以此类推。 快速地多按几次Act按钮,你将发现(观察青蛙)青蛙的高度缓慢上升直到它抵达跳跃的最高点,此时verticalSpeed 值为0.在那之后,verticalSpeed 值增加,同时青蛙下落得越来越快(持续点击Act按钮来观察)直到它再次碰到木头,此时速度值重新设为0.

So the whole time, the verticalSpeed increases at a rate of +3. On the way up this causes the negative speed to get closer to zero, slowing the frog, but on the way down it increases the positive speed, causing the frog to fall faster and faster. This can seem a bit magical, but this is simply the effect of a constant acceleration from gravity, causing the arc of the frog’s jump. If you don’t quite understand, I encourage you to play more with the scenario to get a feel for it .

因此在整个过程中,verticalSpeed 值每次增加+3个单位。在跳起过程中这将使得负速度值趋向于0,青蛙上升放缓,但是在下落过程中它却增加正速度值,使得青蛙掉落越来越快。这看起来有一点神奇,但这是重力造成的简单的恒加速效果,使得青蛙跳跃成弧线。如果你不是十分清楚,我建议你多玩一下去感受它。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值