Get Kids into Coding with Scratch

Game Movement Exercise

Steve Kinney

Steve Kinney

Get Kids into Coding with Scratch

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

The "Game Movement Exercise" 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 creates the initial game loop and instructs students to implement the ability for the sprite to move up and down on the stage.


Transcript from the "Game Movement Exercise" Lesson

>> We do need to get some motion working for our friend. So what I'm gonna do is I'm gonna do the basic setup of the left and right. And then I'm gonna have you just take a second to get up and down working. And then we'll look at it together, but I'll get us started with left and right.

Now, important note, I'm really excited to start programming my hero. But if you look up here, there is a taco, which means, what am I writing code for?
>> The taco.
>> The taco, so if I click on Dave, now I can see that I'm writing code for Dave.

So we'll do the basic setup again just to kind of show you. When the green flag is clicked, you can see that right now, the go to x and y is set to 0, 0. Like I said before, it is wherever Dave currently is. I like that in this case, so I'm keeping it, cool.

So we'll say, when the game starts, we will go to x and y. I don't know, I mean, I could point them in a common direction, sure. Whatever else you think that really needs to happen for this game, you can do right here as well. I think this is probably good enough for now, if we need more, we'll add to it.

And then we'll kick off the game loop, like I said before. I'm gonna say, if the right key is pressed, we're gonna go right. So I think this is gonna work basically the same way where we point Dave in a direction. We don't need to worry about the rotation, cuz he should turn.

He's not a left-right character, he's top down, so the eyes will go the way we want them to go. So if the right key is pressed, then what we'll do is we'll just point him in the direction of 90. I will leave it to you if you want the Pac-Man mechanic of holding on the key or not.

I'm gonna say that you press once and Dave just starts going. If you wanted Dave to move as you hold the key, then you could put the move block in here. I'm gonna assume for this moment that I want the Pac-Man dynamic, that's just how I feel right now.

I don't even know what I wrote in the notes, but that feels good, I want that. And if I change my mind, guess what I can do? I can change the code. So that's fun, so do that, I'll duplicate this, pop that right there. We'll say left, Is -90, unless, can he go right and left?

A pro tip, every little thing, you probably just wanna test it. Cuz what you don't wanna do is get really involved doing all four directions, and then find out life is hard. So for instance, does anyone wanna see my mistake right now? He points in different directions, but all the talking about him, I'm gonna be moving later and not in that block, he doesn't move anywhere.

So let's go fix that. And we'll say, I'm gonna have him move no matter what. If you wanted to move only when the arrow is being held down, then you can just put it inside these two if blocks, right? So Dave can go left and right, which is really sad, because the taco is also up, right?

And so we need to deal with that. But I will leave that to you for a few minutes to just get the hang of that, and then we'll rejoin and look at some different options 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