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

The "Collision Detection" 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 implements the ability for sprites to sense when they are touching something and perform a given action.


Transcript from the "Collision Detection" Lesson

>> This game isn't great right now. I don't even know what this is. It's just not great. It's not great because yes, Arnold can move left and right, that is true. That's the entirety of our game right now. That's it. That's all that's happening. So you can see why that leaves something to be desired.

I'm gonna put on Arnold at -100. And let's say, I'm gonna drop back to Wes's basketball court. And yeah, looks like that'll be where he starts. In most games, you interact with something at some point, right? Perhaps the floor is lava, perhaps there are ghosts chasing you, right?

Perhaps you're hitting a ball back and forth, perhaps you're running after some kind of prize. Perhaps you're just trying to make it through a maze and there are walls. There's usually something else in the game rather than you just wandering around in the void by yourself only on a left and right direction.

And that's where we add additional sprites and we can have a whole bunch of ways that they know about roughly where, things about each other. So let's go ahead and let's add a second sprite. Do we have any choices here?
>> Apple.
>> Something I can talk to like think a bat or a monkey, bat, ghost.

>> Cheesy puffs.
>> You wanna talk to a bowl of cheesy puffs, okay, we'll talk to bowl of cheesy puffs.
>> Yes, [LAUGH].
>> I don't know how this is gonna go, but here we are. Let's toss an apple in there as well, for good measure, cuz.
>> Apple's right there.

>> All right, sweet. So we've got that in place. And we can go ahead now and say, Arnold can see, roughly in the game loop, we'll say if, And this is where the sensing is. If touching, you can see if it's touching a certain color that could be useful.

It's like if it's looking for something purple, you can say touching color. You could also say if it's touching another sprite, which is kind of what we want in this case. So if it's touching, either the mouse pointer or sprite. We'll do, I got an idea. So if touching the cheesy puffs, Arnold's going to say, Yum for two seconds.

So now the game loop is gonna kind of go through, and it's going to say, okay, if we're touching the cheesy puffs, say yum. Otherwise, we've got the controls in place. Now I've got some logic to, I won't use the word game. Let's call it an experience, right?

We've got some logic for our experience right now. So we can go ahead and we'll move him. Yum. Let's say for two seconds he walks away. So we have that in place. And then we can say if we want to program a different sprite, let's say the apple, we could say, all right, when the green flag is clicked, we'll give it a game loop.

For most things that we're doing outside of making a cool little art thing or a story, we're probably gonna have a game loop. We'll say forever. And for the apple, we'll say if it's touching the mouse pointer, Say, Actually, let's do an if else this time, just for funsies.

Drag that, get rid of that. We'll go ahead and we'll say if else. If we're touching the mouse pointer, we will go ahead and say, Hello, I am an apple. Typically a good thing for an apple to say, right? Otherwise, if the mouse pointer is not touching it, I am lonely.

Right, and so as you can see, the code that controls Arnold is in Arnold, which we called sprite1. Let's call him Arnold instead. He deserves to have a name. There's no code in the cheesy puffs. The cheesy puffs only exist for Arnold to know about them. All right, and then the apple has its own code that also starts and executes whenever.

Now if we wanted the apple to be there, even if somebody drags it, we could do some setup code. But it's got its own code that runs independently, but both kick off when the green flag is pressed, so now you can see the apple says I am lonely.

I'm an apple. I am lonely. I'm an apple. I'm lonely. I'm an apple. It's either an apple or it's lonely, never both simultaneously. And then as Arnold gets close, he says yum when it gets to the cheesy puffs and then runs away he is no longer satisfied, right?

So we've got this idea of interaction. We've got this idea of separating the different pieces of code, so on and so forth, right? And so we're getting some of the initial pieces for a game. So I'm thinking we should probably make one, right? It's a good time to make a game.

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