Get Kids into Coding with Scratch

Increasing Speed & Edge Detection

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 "Increasing Speed & Edge 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 demonstrates increasing the player's sprite movement speed when the score increases and restricting where the taco can move. Ending the game when the player hits the edge of the stage is also covered in this segment.

Preview
Close

Transcript from the "Increasing Speed & Edge Detection" Lesson

[00:00:00]
>> One thing you can do is if you don't wanna see it anymore, you can see that checkbox, that's the worst score in the left-hand side. You can uncheck that and then you won't see it on the screen either. So as you can see, as I get the taco, the score goes up, I feel great.

[00:00:15]
I'm totally paying attention to you all and not playing my game. Life is good, awesome. Cool, so we've got that in place. And this game has a score. We talked a little bit about making it harder, right, something, right? We will eventually have it be a long snake that has a tail and stuff along those lines.

[00:00:39]
It's just a little involved, and also math that we're not ready for this early in the morning, right? We'll have fun with math later, don't worry. By the way, so we can do this, and we'll take two different options. Maybe we won't do them both simultaneously, maybe we will, see how we feel as the day goes on.

[00:00:56]
One way the game here get harder or what are some ways the game to get harder?
>> We could add something that's chasing you.
>> Enemies, yeah.
>> You could make the snake go faster.
>> Yes, let's do both of those. Let's do both. Let's do faster first because it's the simpler of the two, and then we'll add some enemies and talk about some of the nuances there as well.

[00:01:22]
And so what's kinda cool is I have a score, right? So I know how the game is going, right? The score is zero. And so right now, Dave moves ten steps no matter what, right? What could I do if I wanted the game to get harder as he ate more tacos, which is not how tacos affect me personally?

[00:01:46]
>> As you collect more tacos, if you click five tacos, you speed up a bit.
>> Yeah, yeah, I can do it at five, and we'll talk about how to do that as well. But even for the very beginning, I can say, hey, move the score plus ten, right?

[00:02:01]
We can see what actually in a little bit with the enemies, we'll see how to do it every so often, right? Let's start simple. We can say move whatever the score is plus ten. So when the score is zero, he moves ten steps. When the score is 1, he moves 11.

[00:02:15]
You can see how this game goes, right? So what we can do here is we go to operators, and this is where I told you we get to have fun with math. Luckily, our math right now is addition, right? I feel good about that. There might be an absolute value in there at some point.

[00:02:30]
Division might show up. We'll be okay. And so what I'll do is I'll pop this in here, and I'll say move. We're going to my variables. I will say whatever the score, get back in there, whatever the score is, plus ten. So now, Subtly, David's just getting a little faster as time goes on.

[00:03:00]
Okay, I like the idea of maybe we do it every five. [LAUGH] This is unhinged. My Lord. [LAUGH] And there's no way to lose this game if touching the edges made you lose. Okay, well, so we can see our code works at the very least. It got progressively faster as time went on.

[00:03:27]
It'd be great if there's some way to lose. Now, there is the ability. There's a sensing, which is if we're touching the edge, right, then we could have it set so that if touching edge, stop all, right? The problem we have that we had to fix to do that, which I'm about to do, let's do it, is that the taco could end up in a random positioning.

[00:03:50]
If the taco ends up right here, Good luck not dying trying to get that taco. Do you know what I mean? And so we would need to find a way to make sure that the taco ended up somewhere that we could actually get it. So we could do that, let's do that.

[00:04:10]
And so we say go to a random position, and in this case, it's not really what we want. Which is we could, Pick a random number. Now we know that it's for 240 in each direction, and then 180 in each direction. So what we could do is we could theoretically say maybe not all the way on the edges.

[00:04:35]
So instead of anywhere from negative 240 to 240 and negative 180 to 180, because they may be 160 and 220 so that it will never be all the way to the edge. And again, you would do play testing, you would adjust this. So at this point, what we can do is we can change this go to random position.

[00:04:56]
Let's get rid of these for a moment. Snap those off, yeah. And I'll take that one out, too. And what we'll do is we'll say, go to, And we'll grab this pick random. So x, like I said, 240 in each direction, so we'll say -220 to positive 220.

[00:05:23]
And then we'll pop another one in, and we'll say somewhere between -160. And if these are still too close, you just adjust the numbers, right, and pop those in. And we do need this twice, so I'm gonna duplicate it. Later, we'll see how to not have to duplicate code all the time because it's annoying and I'm very lazy, but we're not there yet.

[00:05:48]
Okay, so I think this will work now. Maybe one, no, it's not all the way to the edge. There's still an element of danger here. I could make it go more to the center, but this feels a no point, is it? Boy, still not an easy game, but [LAUGH] okay.

[00:06:14]
So now we wanna say if Dave is touching the edge, he dies. I don't know why, but that's a game. I would say I don't make the rules, but we do, in fact, make the rules in this situation. So in our game loop, we will say, if, we'll put this afterwards cuz it doesn't really matter where, but feels right at the end.

[00:06:36]
So if touching, yep, if touching edge, Then we will stop all, which is under control. Cool, so now if we're touching the edge, the game ends. So now this game has some risks to it, right? It gets harder and there's a way that it goes poorly. Very quickly, yeah, we need to get to the point where we can have to do it every five.

[00:07:09]
[LAUGH] So now you see, it went to the stop sign as soon as I hit the edge.

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