This is what we will create:
Just a few simple steps:
Step 1: Setup stage, sprite and variables
The Stage is really simple for this one. We will just whip up the Paint Editor and put in a few strokes of blue for our "water".Painting "water" on the Stage |
The "Make a variable" button under the "Variables" block group |
Creating a variable for the ball sprite |
Our two variables |
Initializing ball position and the "y velocity" variable |
Step 2: Program ball movement
How are we going to program the ball movement? Before we get our hands dirty, let's talk about how a ball really moves in the real world. If we hold a ball still in the air, it has zero velocity because it does not move. The ball has a certain position - it could be say, 100m off the ground.Now what happens when we release our grip on the ball? At the very instant we release it, the only force working on the ball is gravity. The ball now picks up velocity, and moves faster and faster towards the ground.
Let's rewind this scene and play it very, very slowing in our heads. Imagine the movement of the ball at every second while it falls. Suppose the ball falls 0.1m after one second. Its position after that one second is now 99.9m off the ground. Since it fell 0.1m in one second, its velocity after one second is 0.1ms-1.
The ball's velocity is not going to stay at 0.1ms-1 throughout the fall. It will pick up because of gravity. Let's say that gravity increases its velocity by 0.1ms-1 at every second. Now imagine what happens after two seconds. Since gravity has increased the ball's velocity by 0.1ms-1, it would have travelled at 0.2ms-1. This means that from the first second to the second second, the ball has fallen 0.2m. Its position is now 99.9m - 0.2m = 99.7m.
In the third second, its velocity would increase again by 0.1ms-1 to 0.3ms-1. So its position is now 99.7m - 0.3m = 99.4m
This is exactly how we are going to program it. We will move the ball at very small time slices by changing its position and velocity at every iteration of a forever block. Think of one iteration on the forever block as a small time slice.
Changing and keeping track of the position and velocity of the ball at every time slice |
Increasing velocity at each time slice |
Step 3: Keep the ball afloat by blowing
The last thing we need to do is to make the ball float up as we blow into the microphone. The harder the blow, the louder is our puff and the faster the ball will float upwards. To do this, we will use the loudness sensor. The loudness sensor is found in the "Sensing" block group:loudness sensor |
loudness Stage Monitor |
Script to move the higher the louder we blow |
Finally, we add a block to constantly turn the ball just so that it looks more natural and nice.
And that is all. Have fun!
Coming up: next week, we will learn how control a sprite with the keyboard. Til then!
No comments:
Post a Comment