Speak forty-ninth Scratch: Perfect fall and rebound

Done a lot of games , we will face the situation and collision rebound, CC brother most of the time are also simple to handle it, including lectures before have mentioned, but no serious explanations. Today specialize in a theme for this talk, this part thoroughly publicize the contents thoroughly, we can discuss it together.

 

It is not that simple, is a small ball fell to the ground and then bounced, but let us together worked on the rebound this fall how many there are pit.

 

1: uniform fall

First look at the whereabouts of a simple drop procedure is as follows:

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

Uniform drop the ball, hit the black line stop. This is certainly not perfect, because the fall should be added to the speed , not the uniform. v = at, which is to add speed formula. So every step of the fall have increased over time, the falling distance increases.

 

2: falling accompanied by acceleration.

 

This is not complicated, so each cycle when they are falling speed will increase a little, so there is the effect of the acceleration.

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

With the effect of acceleration, but we did not find the problem? The ball hit the black line and did not stop, but will pass through a part of the black line to stop. That the ball does not come to a complete stop when the black line. This is how it happened?

 

Because every step of walking distance from the ball drop will be larger, we carefully read the program, which is a small step before the ball hit the black line between the step and hit the black line, the number of steps have been traveling a relatively large , it has exceeded the distance of the ball and the last black line. So when confronted with the black line it has actually crossed the part of the black line. How then to solve this problem?

 

3: hit black line just stopped.

 

If you solve this problem, the solution is to let the ball then passed back above the black line. At this time we do not know how many steps should the ball back, we can only try step by step, until the little ball and black lines separated.

 

We added a program, if the ball hit the black then executes a loop, so that the ball back to the dark side of the line.

 

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

Strange is not? Back up the ball in after leaving a black line why they fell down, and then repeatedly shake the black line? In fact, at the time of the collision process of doing this it is often out of a bug.

 

我们仔细读程序,就会发现,在这个循环里的判断逻辑是这样,如果没有碰到黑线,就往下落。每一步下落的距离就是“下落速度”这个变量的值。如果碰到黑线,就往上后退,直到离开黑线。那大家有没有发现,这个逻辑其实就是个死循环。小球永远不可能停下来。

 

我们修改一下程序,重新把逻辑判断的位置调整一下,同时再加上一条关键语句。

Speak forty-ninth Scratch: Perfect fall and rebound

 

 

Speak forty-ninth Scratch: Perfect fall and rebound

逻辑判断调整成,在每一步下落时都判断是否碰到黑线,如果碰到黑线后就后退,后退完毕后又增加了一条语句,让小球再下降一步,保证小球和黑线接触上。小球再下降一步就是一个关键语句。然后大循环就终止了。大家再好好理解一下。

 

但是大家发现没有,这样效果还是不好,小球在返回黑线上方有个过程,我们已经没有加等待命令了,这已经是最快的方式了。但是这个过程还是让人不舒服。(有人在返回黑线上方的操作做的更复杂些,比如一次退回更多步数,比如一次退10步,如果退过了,再返回5步,这种反复趋近的操作也是一种办法。但是程序复杂不说,效果也不好)那如何解决这个问题呢?

 

这里教大家一种方法,用自定义命令的方式。把后退的操作用一个单独的自定义命令来做。

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

猛一看,这段程序跟刚才没什么区别呀?其实区别是有的,之所以大家看到小球向上回退的过程,是因为程序有刷新的过程,每一步都需要刷新屏幕。而把这段程序放到自定义的语句,就可以解决这个问题。因为新的积木属性里面有一个 选项:运行时不刷新屏幕。只要勾选了这个选项,那么这段程序每一步执行的时候就不会刷新屏幕。大家看看效果。

 

Speak forty-ninth Scratch: Perfect fall and rebound

 

 

 

Speak forty-ninth Scratch: Perfect fall and rebound

看,通过这种方法就完美解决了吧。

 

4:下坠加反弹

 

这段代码不复杂,反弹操作发生在当球碰到了黑线停止不动后,然后以之前的速度进行向上走的过程,走的过程,速度不断减小到零。为了让代码看着简单清晰,我们又用了自定义语句的做法。单独做了个反弹的语句命令。

 

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

 

5:下坠反弹,反弹幅度不断减小直到静止在黑线上。

 

This effect is not complicated, just need to bounce back each time to reduce the initial speed of the rebound on the line. Let's add this statement.

Speak forty-ninth Scratch: Perfect fall and rebound

Speak forty-ninth Scratch: Perfect fall and rebound

Strange, after the final ball still, not even to stop, but flew to heaven. This is what bug it? This is because in the end when the rebound velocity is zero, it should still have been down, because the judge did not do controlled rebound speed values continue to increase has turned positive from negative, is no longer the downward descent, but up to fly. So the ball flew straight up and accelerated. So we have to add a judge, when the initial velocity is zero rebound, rebound to stop.

 

Speak forty-ninth Scratch: Perfect fall and rebound

 

 

Speak forty-ninth Scratch: Perfect fall and rebound

This fall and bounce now, it seems quite perfect.

 

6: use the space bar to be a take-off process.

 

The final step is to make this program looks like the beginning of the video, you need to use the spacebar to add a control process off the ball.

 

Speak forty-ninth Scratch: Perfect fall and rebound

 

Looks very simple, usually we will do so, but the actual implementation of the results of it? We look at:

 

Speak forty-ninth Scratch: Perfect fall and rebound

 

Press the spacebar on, we have had a talk before, the main key is the process so simple deal with two issues:

1: Process button to Delay, quick press of some people, some people by slow, so you might press a bit slow due according to the program as you press a few times.

2: During the jump ball, you press the space bar, the ball is in the air into the process of take-off.

 

To solve this problem we must add state variables, and only when the ball has been resting in the black line, only allowed to use the spacebar to jump.

Speak forty-ninth Scratch: Perfect fall and rebound

Now I put this program all posted. You can try to do it.

 

Speak forty-ninth Scratch: Perfect fall and rebound

 

We do the procedure sometimes is like painting a picture as if painting a bad place, you will always think of it from time to time, always wanted to paint it well. Do the procedure is the same, only you yourself are very satisfied with the program, you will feel very happy.

Guess you like

Origin www.cnblogs.com/scratch3/p/10932365.html