Get Kids into Coding with Scratch

Horizontal Movement Practice

Steve Kinney

Steve Kinney

Temporal
Get Kids into Coding with Scratch

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

The "Horizontal Movement 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:

Students are instructed to implement the hero sprite's horizontal movement. Steve then walks through a possible solution for adding horizontal movement and implements horizontal collision detection.

Preview
Close

Transcript from the "Horizontal Movement Practice" Lesson

[00:00:00]
>> I may give you the hypothesis on how to do left and right. I'm gonna give you a chance to try it out and then catch up to this before I keep talking and keep coding. Here's how left and right is gonna work. Almost the exact same, right?

[00:00:14]
Except we wanna check for X first cuz we let them go in one way and then shoot up, don't go into walls before we shoot you out of the walls. And so we wanna do a very similar thing. The only difference is we're gonna do the same thing for x.

[00:00:32]
But we wanna have the absolute value of velocity y plus the absolute value of velocity x. So I'll make those two variables for you and I'll show you what will pop in there when we have them in place. But the same logic, we just wanna do x first, do the xs, right?

[00:00:49]
And then do the y's, and this will end up being, let me make the two variables so you can see it. We'll do last x and we'll do velocity x. The one part I'll leave on the screen for you just as a helpful cheat code, is not that, not that at all.

[00:01:14]
So we're going to plus. And what the end result is gonna be is, okay, let's say we were supposed to move 5 steps forward and 10 steps up. So that's 15 altogether. Do spend 5 of them on the forward and 10 of them on the vertical. And then see when we should have stopped at any given point.

[00:01:36]
So effectively we'll have the x before the y, but let me just set up the end result piece cuz you will forget, cuz I will forget. Cuz this is about you and not about future me and what I need. So when you get the x happening first, and I will do it with you in a second, but this will give you a second to catch up and then try it out.

[00:01:55]
It will be the absolute value of both. Right, so basically we need a version in here as well that does x. And I would do the sliding motion from before, but you can just have it go normal if you want to. Up to you, how far you want to take it, but then we will do it all together in 10 minutes.

[00:02:19]
That way I can check on everyone, make sure everyone's got something working. All right, so we wanna get the x part working. So we can start off by having them move x and y first, right? That way it's easier test. So we can say if, Not there. [INAUDIBLE]

[00:02:46]
>> Yep, [LAUGH]. If the right arrow is pressed, right? We want to, no, go back in there. I just wanna move the entire thing, out of the way. If the right arrow is pressed, then let's go ahead and set the x velocity. We're gonna do the same drifting motion that we had before but I liked it cuz it felt good and I like to feel good.

[00:03:14]
We'll set that to, let's call it, 10. Not last X, we want velocity X to 10. If the Y key is pressed, don't, you know what, we'll do it down here, I'm not living like this. If the Y key is pressed, Y key, left arrow is pressed.
>> [LAUGH]

[00:03:44]
>> [LAUGH] I mean, I could have right be the right arrow and left be the Y key. That's a thing that I could do if I hate the person using my game. Let's go ahead, and we'll set the last x to -10. And then what we'll have is on every, take the same way we're slowing, maybe doing that up a top wasn't my best move.

[00:04:15]
But whatever, every time around, we will do that same thing we said before, we'll times it by 0.8. So we'll change. We'll set velocity x to, Operators, velocity x, I think I grabbed multiplication that time, we'll find out together. Is, 0.8. Do I want it before or after?

[00:05:11]
What do we got? Right now, we'll move them in there and we'll see how this doesn't work and then we'll make it work. Cuz right now he doesn't move at all. So we'll then say, Change x, By velocity x. Again, I'm removing that one in a second but right now, he doesn't move at all, so.

[00:05:37]
Okay, so right now he goes through walls. Interestingly, can't jump while in the walls because my logic works cuz he's touching red. Which sets his as 0. And he can kinda move through a floor. Okay, so what we wanna do is, when he goes face first into a wall, stop.

[00:05:59]
And the reason we're gonna do x before y, it gives you everything about if like Super Mario, or Sonic the Hedgehog, or what have you. Jump into a wall, they're still in the jump, they're just not moving forward, right? They continue to fall in and then they hit the ground, it's just one of the things that happens before the other.

[00:06:15]
So, we need something very similar to this. Now you can do two things, you can try to duplicate it, you will have to swap out a lot of these blocks. I will say that in the scratch add ons, there's an ability to right click and change blocks, which is super helpful.

[00:06:27]
But I'm not going to assume that you have that installed and as you watch me use power tools that maybe you can't install on your Chrome. But there is a set of Chrome extensions that makes this a little bit easier. Your choice, if you wanna play the game of rebuilding some scratch or pun unintended, or if you wanna kind of try to copy paste and swap stuff out.

[00:06:49]
I'll duplicate it and live with the, cuz that's probably what you probably tried to do as well. We wanted x to go first, so we'll do that first. All right, so then we wanna set less x, To the exposition. And then we wanna change x, there. And then we'll change x by that new velocity x, right?

[00:07:22]
So, go, see if you were allowed to go there at all. If not, change it to, we'll set it to x to less x, maybe rebuilding it from scratch was easier. We will set x to that last x. And we'll change the x velocity back to 0. All right, I think I got it, but now we wanna do the, since we wanna check for the total number of y and xs, we'll pop in this combination of the two.

[00:08:04]
Get some stuff out of the way. And I can also hit cleanup blocks. Okay, let's see if that did the trick. Have I missed something? He's a little fast at this point. So, we got something off here, jumping works. He can't still go through a wall, all right, let's check it out.

[00:08:32]
So we'll go through the steps, we will try to deal with the ceiling first. I don't think we wanna do it, all those in the same repeat block. All right, I got a, Little carried away in there. Let's see if that fixes it. No, he's still feisty. All right, let's see what we got, so, I said expedition to last expedition.

[00:09:11]
I use a wire in here to really ruin my day. We need that division, right? We have a division here, I didn't put it up here. So let's go ahead. Cuz otherwise, we're not doing it by small steps, we're doing it literally just too many times. Boom, it pushed through the wall.

[00:09:42]
It does stop at the wall, it looks like you can also still push through it. You know what it is? We're also moving by x, same problem I had before. At least I'm repeating, as I repeat the process, I'm repeating my mistakes, that feels good.
>> I know why he can't stick to walls, he just bites the walls so hard.

[00:10:08]
>> And he just chews through it like he's got zombie powers. There we go. Oof, oof, jump! Look at that! He stops, I mean, he's still got infinite jumping abilities, but-
>> If you hit the wall while you're in the air, will you stick to it?
>> No, I mean, see that?

[00:10:30]
The one place you might be able to pull it is that little tiny ledge where I drew poorly. [INAUDIBLE]
>> Where, I guess, that it's technically, it's all about our rules and where we allow him to stand and not stand. I would say that that's better fixed by better level design, not necessarily by my code.

[00:10:48]
But generally speaking, if he hits a wall, right? You'll see, if I can get him to hit that platform. See, he won't stop midair, he'll just stop. That's why we checked all the x first cuz then we set the x to 0, we don't. As soon as he hits a wall, we're done checking x, right?

[00:11:02]
And then we go back to falling. So he will he won't stop falling till he's done. In there as well.

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