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

The "Player Sprite Practice" 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 instructs students to add a playable sprite that falls to the ground and can walk left and right. Steve then walks through setting up the player sprite and demonstrates multiple options for detecting when a sprite touches the ground.

Preview
Close

Transcript from the "Player Sprite Practice" Lesson

[00:00:00]
>> What I want you to try out doing, is it would be nice if someone could catch the apples. I'm gonna choose, and you can choose your own character, you do whatever you want. I'm gonna go with Pico because he's very happy, and it is the energy that I wanna embody.

[00:00:21]
So we're gonna have Pico and we want Pico, if he is, for some reason, he doesn't have to be able to jump just yet. But if he is off the ground, we want him to fall to the ground might have to sense something there, right? We want him walk back and forth and ideally it'd be great if he could catch an apple, but if not, don't worry about that.

[00:00:38]
But we want a Pico that can fall to the ground on his own, but will stop falling to the ground when he's on the ground. Because these apples will just keep falling, right, so we need some way to make that happen. But I'm gonna let you try that out for a little bit and then we'll take a look at it together, and we'll see how we can from there.

[00:01:00]
So our mission was, we have these falling apples, right, We would like somebody to catch catches apples, so we're gonna add a character, I'm gonna go grab Pico, I like Pico, I like his energy. There's a different one that can do for stories, we're gonna do the walking one, Pico is a little big, I like a half-sized Pico.

[00:01:33]
So unlike the apples that fall into oblivion and then go away, the new ones come, Pico, we want it to fall to the ground, and we'll worry about jumping later. So at this point, we're just practicing our falling mechanic [COUGH] and eventually, we wanna have the right and left so that.

[00:01:50]
Pico can theoretically run around and grab these apples, the difference between Pico and the apple is that Pico has got to fall to something, right? Arguably the ground, because otherwise, if it's just the apple Pico can also join the vortex of falling apples, but that's not the mechanic I'm personally going for.

[00:02:09]
I mean you do you but so we'll say Pico starting position, we'll have him kind of start in the air a little bit, that way he does have to fall. And so we'll just go to that position immediately and let's start with the following mechanism and then we'll add the motion.

[00:02:25]
So we'll add a game loop once again as we are known to do and now, [COUGH] if we look at the apples, they're falling, and then they start over. Really, for Pico, what we wanna do is fall until you hit the quote-unquote ground. So we're gonna need to make ground at some point, but let's get falling working, and then we'll worry about this concept of ground, whatever that means.

[00:02:48]
So we'll go ahead and Pico's gonna need, like, so for the apple, we said the velocity was for that sprite only. Because otherwise, Pico and the apple's velocity, as the apple falls faster, so will Pico and then Pico will also then iterate by one. And so it means the two of them will impact each other, which is not what we want.

[00:03:08]
So, as you notice, if I go to the apple, I have velocity, if I go to Pico, I don't, because I did that on purpose. It's for the sprite, so I'll say, velocity, again, you can call it speed, call it whatever you want. So we'll start out by setting the velocity all right I did negative one before to make a point, but what was zero?

[00:03:30]
>> [INAUDIBLE] this Sprite.
>> This Sprite only.
>> Okay.
>> So we're doing it for this Sprite only and then same mechanic as we had before. We will change y velocity and then we will change velocity by negative 1. One of the things was implemented a terminal velocity, which is this concept of physics where things speed up and speed up and speed up and then that's it, right?

[00:03:58]
They hit a maximum velocity, so you could theoretically put just, it's as simple as putting an if statement here. If it's less than negative, whatever you wanted to be ADA was the terminal velocity of a cat, is that correct? Before, so then you can no longer do it by one if you wanted to implement terminal velocity in your game.

[00:04:17]
We don't have a lot of pixels here, so I'm not gonna worry about that too much for this mechanic in this game. And so that will do the trick where it'll fall, but clearly we would like to have some concept of ground. There's two ways to do this, I will show you both and you can pick whichever one you like more, which is we can make ground part of the stage.

[00:04:39]
So I can literally just go to backdrops here and what color should the ground be?
>> Yellow.
>> Obviously, yellow.
>> That's the color grass is.
>> Let's see, make it, like that shade, like a solid goldenrod color. Yeah, me too, so we'll do that, and we want in this case, no outline, and we're gonna go, and we're gonna make a.

[00:05:08]
Look at that, that's a nice background, right, so we can do it with the backdrop. And now what we can do with Pico is basically say, hey, continue this until, or unless, yeah, we can do repeat until, that'll work. That's one option or we can kind of just check every time.

[00:05:30]
So there's different tradeoffs to both which is like the repeat until we'll have to finish before we go back into the forever loop. Which for this version of the game is fine, but we'll kind of talk about some of the different options. So we have repeat until, and we can do sensing, and we'll say until touching color and then I grab this little picker right here.

[00:05:53]
Until touching that color, have them drop, so you ever see those games where first they kind of drop to the ground and then they can play, that will be this mechanic. I don't have right and left or any controls just yet, but we'll say start the loop and repeat it until he's touching yellow, which means nothing else will happen in that loop.

[00:06:08]
We won't do the game loop again until he's hit the ground, so let's try it out drops. Now one thing that you might suddenly notice is he drops and if we you can see he's a little low there.
>> I kind of like that.
>> So you could choose to like that mechanic, right?

[00:06:28]
If you drop him a lot more, he drops, this one he starts in the right place. Let's start him, at 180, and we'll start from there. So you can see he drops a lot more based on how far it has he was going, cuz the more he's dropping, the faster he is going, which means we overshoot and then we realize that we've hit the condition.

[00:06:51]
Now there's different ways to handle this, this is simple and easy. And so in this case, because, there's not really any jumping or anything like that, there's no platforms, right? We could choose to just paint the background, like something that would be okay with that. So kind of make it look like a road doesn't really matter where he lands on the road.

[00:07:08]
We could know that since there's only one ground, we know roughly where that ground is and where we wanted to stop, right? So we could say just repeat until Pico current why and then he won't drop any further as well, that'll work. There's a bunch of options that work for this game, the problem is that in a platformer, or something like that we have multiple grounds.

[00:07:32]
We're gonna need something better, but this will work for right now and we can do other things. So we can do something like we can repeat until, and we can update the velocity every time as long as it's greater than whatever it ought to be. And if it is not, then we can just set it to that, so we can say something like repeat until colo, touching yellow we're gonna use like an all right?

[00:08:03]
If the next step is gonna be too far, then we can just put him where he's supposed to be. So, we could say, if the current y position Current y position that's in motion So we need to add stuff, so the current position plus where it's about to go next is too far, we can just put them where it's supposed to be which looks like about, I like it right there.

[00:08:39]
That seems right, that's 198, so if y position plus The, Next velocity. So if that's negative 1, whatever plus another negative is gonna be less than this, what was it? Negative 198, you could do the logic the other way, put them where I'm supposed to go And we could just say, set y, that was negative 44, sorry, I was looking at the x.

[00:09:30]
Negative 44, set y to negative 44, otherwise do the thing we had previously. So fall, unless you're gonna fall too far, and then just end up where you're supposed to go. And now he falls where he is supposed to go. All right, that'll work, it doesn't really matter for this game, there's different ways to do it.

[00:09:55]
This will work for a very simple thing where there's only one floor, like I said before, in something like a platformer. This won't work because like we've hard coded in this negative 44 as the ground but there could be many grounds. It could be platforms, there could be moving floating islands, it could be a whole bunch of stuff, right?

[00:10:10]
But you're not gonna really need much more than that, in this case, all right, so that will work. We kind of choose what we wanna do in that situation, now we talked a little bit before about walking, and so we know how to do that right? And that was your kind of mission, we can say if, If the sensing here is.

[00:10:40]
We there this is this key is pressed, if the right arrow is pressed, point in the direction. And again, you could choose to have moonwalk, you do whatever you want, again, it depends on the game. You set a point that direction, and let us say move ten steps, and then we'll duplicate that, nope, I wanna duplicate all of that.

[00:11:04]
And we'll change it to the if, left arrow is pressed point direction negative 90. Put that in there as well, cool, and if we wanna make sure we have the right kind of left-to-right rotation style, just like we saw before, we can put that all together. We do need to do the next costume thing, although it looks like he floats through reality, which is fine.

[00:11:35]
So we're just grabbing looks for next costume, we'll do that after we walk. There he is, he is running around, so you do by testing color. The other thing you could choose to do, I guess we should only do the next costume if he actually moves, get off Do that in both cases here, that way he doesn't, so you can do it by color.

[00:12:10]
The other thing we could do is, you'll see a lot of times, this is just actually do sprite collisions, we saw that before with the enemies. And so what we might choose to do is just create a sprite that we call ground, backdrops, I'll just select all here, copy, paste.

[00:12:38]
So the only issue with this is that you couldn't do the fall to a given amount right but the nice part if you choose to go through the touching a different sprite we go through. Is that you can have it in front and so the apples look like they kind of vanished into nothing.

[00:12:52]
You can do the color of the sprite as well, are you gonna have a touching Sprite, these are all different options depending on the different mechanic. I like this effect a lot, the color is still gonna work because the backdrop is yellow, the sprite was yellow. I don't even need to worry about the sprite collision but now I like the idea that it's all the way in the front and the apples look like they're falling behind it.

[00:13:11]
So I like the visual effect in this case as well and can walk right and left. So we can't collect any of our apples yet though, which is kind of a bummer. So we should fix that.

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