Check out a free preview of the full Get Kids into Coding with Scratch course
The "Game Movement Solution" 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 discusses common problems when using custom sprites and walks through some possible solutions to the game movement exercise.
Transcript from the "Game Movement Solution" Lesson
[00:00:00]
>> All right, so before we get to the solution, let's talk about some of the various failure modes that we saw during this. So for instance, one is to think, right now, the default direction of a sprite is 90, right? And so I made Dave point to about 90 degrees.
[00:00:22]
Now, there is a world, though, where for instance, you could theoretically, be like, yeah, I'm gonna make a little character, and if he's pointing up, you can see that we kind of get an effect like we had with Arnold in the very beginning, right? In this case-
>> The Arnold effect.
[00:00:41]
>> The Arnold effect as it's known. So you could grab it and just turn your sprite like this, you just wanna make sure you select all. The other thing you will notice, if you selected all, is in the costume, it's really subtle on my screen here, but there is little cross hair.
[00:00:59]
That is the center point of the sprite canvas. You can kind of see, it's probably a little clearer on your screens. And also what I've selected has a little plus sign. If you go, you wanna snap the plus sign to the center of your Dave, or whatever you've chosen to call your Dave.
[00:01:18]
And now that will be in the center of sprite. Also if you had some extra little things somewhere then the sprite will think it's bigger than it is. So just make sure that any extra stuff, so for instance, if I had a, I'll grab this real quick, a tiny little thing over here.
[00:01:31]
Dave is now this big, which is more than I want him to be. And so a white eyeball or something like that could be somewhat hidden. So we'll just go ahead and delete that for now. So you can point there as well. The other thing, as I said earlier, at one point, cuz it happens to me, you will write code in the wrong section.
[00:01:50]
And so what happens, in this case I wrote it all in Dave, but what happens if this was code meant for the taco, right? That is easily fixed. You grab the code, you drag it over, and it lives in taco. Now it also lives in Dave, this only copied it.
[00:02:05]
So you go to a place where you don't want that now, and drag it into the drawer, and it's gone. So you can move the code from one sprite to the other when you make a boo-boo. You just have to go move it over and then delete it.
[00:02:17]
Great, now I'm gonna do up and down. But before I do that, I'm gonna just kind of demonstrate that there are other ways or other options in which. Actually let's do up and down and I'll show you that there are other options and other mechanics that you could use.
[00:02:31]
Again, there's not a right answer, it's about what are you trying to build? So in this case, I will go ahead and I will do, let's pull one of these out. Pop that back in. We'll go ahead and duplicate. I got two more, so now I say when the up arrow is pressed, point up, When the down arrow is pressed, point down.
[00:03:00]
And you can put this all right back in, and you can start and you can stop. Now, if I don't change that from left arrow to down arrow, everything's all topsy turvy. Again, it does what I told it to do. Not what I wanted to do, but it did do what I told it to do.
[00:03:17]
And now Dave is moving around, he gets the taco, he doesn't even know he's on the taco though and we don't have any of that. So we do wanna program that kind of interactivity. But first, let's take an aside and talk about if we wanted a different mechanic for the movement, how we might do that, right?
[00:03:36]
Because again, depending on what you're trying to do, different code paths will work. I chose to have a move no matter what in a given direction. If you wanted to have to hold the key, then you put the move block inside every if statement. It's all depending on what effect you're trying to get, what the right answer is, and so far there is no right answer.
[00:03:53]
I'm gonna just detach this for a moment so I can put it back. I'll actually duplicate it for a second. Just put it over here. Now this code will never run because nothing kicks this code off, it's just hanging out there. It's not also gonna create a forever loop just in general, nothing starts this.
[00:04:12]
So it's just dead code hanging out, it's called kids. Ask your parents about it. Okay, what I'm gonna do is, instead of the right, and let me get rid of those, and I'm gonna create a slightly different mechanic. Well here we're pointing in the direction and we're saying go, but I could also get rid of these.
[00:04:34]
Well, let's say we wanna make a racing game instead, right? I could say, when the right arrow is pressed, turn one degree clockwise, you can change these numbers if it's too slow for you. And when the left arrow is pressed, turn one degree counterclockwise. Let's look at the different mechanic that we get.
[00:04:52]
You can see that maybe ten steps is a bit too much at this point, let's go down to one at this point. So now, as I hit right and left, he turns. All right, ten was too fast, this is why we play test. Ten was too fast, one is egregiously slow.
[00:05:10]
>> Try five.
>> Try five. Let's try five. Right, and so you could choose that you like this mechanic instead. Five also a little much, crashes in the wall pretty quickly. And again, so you know what you do, you play your game and you make adjustments. Two seems actually, I don't know, maybe one and a half might be the right answer.
[00:05:31]
Or maybe he needs to turn faster, right? And so a lot of these things, there's not necessarily a right answer, you play the game. Let's have him turn at four degrees instead of one degree. All right, now we got something going on here, right? So in this case now his turn radius is a little bit better, maybe I do want him to go a little bit faster.
[00:05:50]
You can adjust all the different pieces and you get different effects depending on what you're going for. So if you like this better, this'll work. If you liked the up, down, left, and right, that'll work too. You get to choose whichever one you want, I'm gonna go back to our regularly scheduled programming.
[00:06:05]
So now up, down, left, right, again, you can use whichever one you like more.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops