Check out a free preview of the full Get Kids into Coding with Scratch course

The "Falling with Velocity" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates implementing some simple game physics by creating a velocity variable to increase the speed of a falling sprite.


Transcript from the "Falling with Velocity" Lesson

>> The other thing that we have in a lot of games is some idea of what I will very loosely call physics, right? Something that makes the game feel right. So let's go ahead and make a new project. And in our new project, we're going to, let's make a new sprite as well.

We're gonna need something that falls from the sky.
>> Apple.
>> Apple, love it. Apple, big fans of the apples around here. Let's make this a little bit smaller for reasons that you will see in a little bit. And so what we wanna make the apple do is we wanna make it fall from the sky.

Let's get Arnold out of here for a little bit. Wanna make our apple fall from the sky, and you could theoretically do something like this. So, When you say when it's clicked, what we just wanna do is change forever. Or until it hits the ground, but forever in our case, cuz Scratch won't let anything go into oblivion, it'll just kinda stop it when it gets to the bottom.

So forever, what we wanna do is we wanna change why, By negative, 1 might be too slow, let's see. Little bit too slow, right? Let's also have something where it goes back to the beginning every time so I don't have to move it cuz I am lazy. And so we'll say, when the green flag is 6, go back up to the top and fall a little bit too slowly.

We can give it full at 10 maybe, again, it's negative if we want it to fall. And that works, right? But that's not how falling works in real life, right? Generally speaking, on earth, at least, we have this thing called velocity, right? Which is something starts falling, it's kinda like somebody who's probably speeds up as he goes along, right?

That's to say if you were to get in a car and put your foot on the gas pedal, the car starts at zero and gets progressively faster, right? You can throw something up in the air, when it hits the highest point, it will start to come down, but as it falls, it speeds up faster and faster and faster and faster and faster, right?

And so we can do something like that as well and kinda create mechanics in our game that feel a little bit more natural. To do that, we will need a variable, and let's change this, let's call it velocity. You can call it speed if you want to, it's up to you, whatever you wanna type.

>> Set variables.
>> What's that?
>> Set variable to 10 to start?
>> Sure, let me just check something real quick. I'm gonna make it, again, only for this sprite for reasons that I will need later on. Future me, it's gonna want it only for this sprite. Okay, so now you can see this one's only for this sprite cuz it's Apple: velocity, right, which means it's for the apple.

And so when the game starts, we'll set velocity to 0 cuz it's not falling just yet. And when we change it, we wanna change by the velocity. I could set it to 1, you get the point, right? Something, right, some base amount. We want -1. And it will fall, but we want this concept of it getting faster over time, right?

And so now that we can store the current speed and we can change it, what we can do is say, okay, I change it but then also change the velocity by another -1. So it starts out in this case as -1, then it's become -2, -3, -4, so on and so forth, right?

Could we implement the idea of terminal velocity, which is eventually when things are falling, they get to a certain max speed, they don't go any faster? We could do that, we might do it. I was like we won't, but I'm like maybe we will. So now, as you can see, it feels a little bit more natural where it hits the bottom, but it starts to speed up as it falls.

It looks all right, already a little bit more normal in this case. And if you're too fast, I could say -0.5, right, it'll still get that same effect, but at a slower rate. And that number will keep going up, but we don't really care. And so down it goes, over and over and over again.

But it does speed up along the way, right? And those mechanics work a lot, if you play a game where the character is running forward, and as soon as you take your finger off the right button, it comes to a complete stop, that doesn't feel natural and normal in games.

It's a lot of those things where we start to feel it over time in these games. So having some of those kind of natural physics that we come to expect, not only from real life but also from every other video game we've ever played in our lives. Trying to get some of those mechanics in here as well will make our games feel a little bit more normal and natural.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now