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

The "Adding Enemies" 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 an enemy that chases the player's sprite around the stage and ends the game if the player's sprite touches it. Creating clones of the enemy that also chase the player is also covered in this segment.

Preview
Close

Transcript from the "Adding Enemies" Lesson

[00:00:00]
>> So now, we've got this dynamic of increased difficulty, cuz if this game didn't get any harder and there was no way to lose, sure a score is useful, but all of a sudden now you're like, what's the highest score I can get? Cuz there's a way that you can lose, right?

[00:00:15]
There's a certain amount of engagement. Sometimes we need life to be hard in order for it to be fun, right? So, we've got a decent dynamic, but I heard what's the idea, what if we add enemies, right? So, what if not only were you chasing a taco, but there was someone chasing you.

[00:00:36]
I might take out the speed for a little bit until we get to the point where you do every five, because there's a lot going on at that point in the game. We'll put this aside for a moment. We'll put it back in a second. Cool, so, now the game won't go faster, but the edges are still problematic.

[00:00:56]
So, we need an enemy, we need an antagonist here. I don't wanna make a new sprite right now, so what I'm gonna do is I'm gonna duplicate Dave. And that's gonna duplicate Dave's code. I actually don't want Dave's code. I just wanted Dave's sprite. What is Dave's mortal enemy's name?

[00:01:20]
>> Evad.
>> Evad?
>> Evad, cuz it's Dave backwards.
>> Okay, I like it, Evad. And so, Dave is going to evade Evad. I'm gonna say that 42 times fast. And now, it's gonna be problematic if Dave and Evad look exactly the same. So, go to costumes. Red?

[00:01:44]
>> Yeah.
>> Red works bad guys, right? Let's try those down. Yeah. There's one thing that needs to change here, is if we go to this tool here, you actually bend it. Yeah.
>> He's angry. [LAUGH]
>> I bend those circles a little bit. There we go.
>> Now, he's angry.

[00:02:18]
>> Yeah, now he's angry. Evad is angry. All right, so now I've got an angry Evad who should chase Dave as Dave is chasing the taco. So, we've got to figure out this logic. And let's go ahead and see. So, let's move Evad up to this corner so that they're not starting right next to each other.

[00:02:40]
He needs some setup space. Otherwise, this could be like the last place he was when the game starts. So, we'll move him up there. So when the game starts, we'll do our setup, which is go over there, Evad. And then we'll put a game loop in. And,
>> Make him glide to a random position.

[00:03:01]
>> So he just teleports?
>> No, just glide to a random position.
>> Make it for 0.1.
>> Let's try it out.
>> Make it for 0.1 seconds.
>> All right, let's try it out.
>> And they'll be zooming around there.
>> Let's try it out. Glide to random position 0.1 seconds.

[00:03:17]
>> Yes, 0.1.
>> All right.
>> Now just push go.
>> That's terrifying.
>> [LAUGH]
>> [LAUGH] I'm getting.
>> Okay, then make it slower.
>> All right, slower. Yeah, this is why we played it.
>> Make it 0.5.
>> 0.5.
>> Okay, that's-
>> Okay, maybe even one second wouldn't be terrible.

[00:03:43]
It's just kinda hard cuz-
>> He's unfairly fast.
>> Yeah, [LAUGH]. So [LAUGH] we might adjust it, also, he is erratic. [LAUGH] All right, let's try this one out. Let's say, point towards Dave.
>> No. Right, and then we'll move 10.
>> So, he has to eat tacos to run away.

[00:04:13]
>> Okay, he's a little bit slower than Dave. Steve makes one wrong move and game over for Dave. All right, maybe Evad needs to move five. All right, the edges are bad and Evad
>> Five plus the score.
>> We'll get there. [LAUGH]
>> Five plus the score, make him move along with Dave.

[00:04:38]
They keep up the speeds for
>> I think eventually let's try something else first though. Actually, we can mix the having enemy and speeding it up, or what if the number of enemies increased?
>> Yeah.
>> Right, so first you got one Evad, but then there's like a gang of Evads chasing Dave down, right?

[00:05:03]
And so at that point, we need to figure out a way to do some of these mechanics as well. And so, how we're gonna do this, I'd say we've got the taco, let's have the taco. We'll do it like, again, I agree that we need to do this maybe every few points, cuz otherwise it's going to get out, or the game will just end fast.

[00:05:25]
The only thing that a sprite can do that affects another sprite is we can do this thing called create a clone, right? Which is basically instantiate another version of that sprite. That was adult words.
>> Okay, let's create a clone.
>> So with the taco, as we're going to increase the score, let's also say that we're going to create a clone, not of the taco, but of Evad.

[00:05:56]
So as you can see, I have a problem here. Which is we create clones, but what do they do?
>> Stand.
>> Just stand.
>> They just stand there, right? Also, I never put the logic in where touching Evad is bad, is still just the edges that ends the game.

[00:06:18]
So we need to do two things, one we need to make it so that Dave evades Evad, and the edges and the game goes faster, and it's really upsetting. So, I guess I could do this in two angles. Honestly it's up to you, we can say. If Dave is touching Evad, the game ends, or we go to Evad and say if he's touching Dave, the game ends.

[00:06:47]
It's up to you. I feel like since the edge logic is already in here as well, I might as well put it in here that makes more sense to me. So if we are touching Evad, we have not evaded Evad, we will also stop all. And now we will test to make sure that works.

[00:07:16]
Okay, cool. We ran into a Evad and we died. Yeah.
>> When you make a clone of something, it switches its name, so if you were to touch Evad, his clones would be like Evad one. So then, that name would no longer work.
>> Let's find out.
>> At least from my knowledge.

[00:07:37]
>> Still die, the clones count. We've got enemies, but they're kind of stone enemies, right? They don't really do anything which, I mean, adds an element, what you could do if we wanted to. Let's say we were done coding, we didn't wanna change anything, right? You could have a second costume for Evad, that was like an Evad statue that doesn't move, or something along those lines.

[00:08:05]
>> Could also make it where it's a statue. And then when you touch the statue Evad, then he is regular Evad.
>> You could do all that too, you could do that as well. Let's talk about why Evad isn't moving, which again falls into our principle of that's not what we wanted, but that is what we told the computer to do.

[00:08:26]
So here we are, which is we said, when the green flag is clicked, start this loop for Evad, right? There's no more green flags being clicked. Thereby those clones have no code, right? Cuz that green flag event never happened, right? So, there's a bunch of ways that we can solve for this, a bunch of events.

[00:08:51]
We need some event to kick this off. If we go into events, we've got a few options. When the green flag is clicked, okay? We can make the player have to hit the space key to get the Evads moving. Doesn't seem like they're gonna do that. We can make them click on the sprite.

[00:09:07]
Doesn't seem like they're gonna do that. When the backdrop changes, maybe there's a mode where after 10 points the backdrop changes, and then all the Evads come to life. I don't hate that. When loudness is greater than 10, well, there's no sound in this game. Messages, we haven't got to yet.

[00:09:25]
There's this other one down here, when I start as a clone.
>> [LAUGH]
>> Well, that seems good. When I start as a clone, I'm gonna say, do the same thing, okay? Chill, you could already see what the problem's going to be here. So when I start as a clone, so the clone starts in the same place, and they have the same logic.

[00:09:56]
And so the game has gotten harder, it's just like they're stacked on top of each other. He's just getting chunkier.
>> Even that, that happened with the first few, I don't even notice it anymore.
>> He seems chunky now.
>> So, we need to give it slightly different logic, and this again is not a programming issue, this is just a gameplay issue.

[00:10:18]
>> Make the clone teleport to a random position.
>> I tried that, it goes really poorly. Cuz all of a sudden it pops up and the game ends and you're like, that's not fair, right? Let's try it, we can do it, but you'll be as angry as I am.

[00:10:30]
[LAUGH]
>> Make it go into the corners.
>> Yeah, let's do let's do random, let's try both.
>> Make it at the edge.
>> Let's do both, let's try that random one. That illustrate my point a little bit too well, that the random position that I went to was directly over my player and then I died.

[00:10:49]
So, we know we don't like that mechanic. I thought I was gonna do that several times before we saw that, we're gonna have it go to the corner, which is a good idea, and we'll see [SOUND]. Where does that go to? Go to, and let's say 180, that's backwards mode.

[00:11:13]
240, 180 that should be the upper right corner, right? Oops. I didn't put the forever block back on, my code did what I wanted it to, or it did what I told her to do, does that what I wanna do? So, this will work. I guess I ate two tacos at that point.

[00:11:37]
The only issue that I noticed with this, this actually works great, eventually they will converge and we'll have the same problem. It's just this game's too hard at this point [LAUGH] can't get far enough.
>> How about if they're touching each other? Then they teleport to the edge again.

[00:11:53]
>> Let's try it. So then, what we'd have to?
>> Make-
>> Well the problem is if two of them touch, they teleport at the same time, they'll be touching each other again, they get stuck. Let's try it out.
>> What if when they touch each other, they bounce off each other?

[00:12:08]
>> Yes.
>> Like this.
>> Yeah, like ricochet off each other. But they're also pointing towards Dave every time. So, we change the direction or the other direction, and then they go immediately back to pointing Dave. These are all options, right? And these are where we play-test it, we can try the teleporting.

[00:12:28]
I have an option, it's not any better than anyone else's option. This is the option I decided on at some point after way too long doing this.
>> Do it to the side for a move to the side instead of straight forward.
>> Let's say if they're touching another Evad, we go back to the origin point.

[00:12:48]
The original, 240, let's try it out, see how it feels. All right, so if touching another, can't touch yourself, okay, so we don't have that option.
>> It's touching the tacos and the taco teleports away.
>> I like all these mechanics, and like definitely we'll have some time to explain that.

[00:13:09]
>> And you can play a eating sound.
>> Eating.
>> Here's what I'm gonna try. I'm gonna have them just behave a little bit differently than the main one, cuz that's the nice part. It's like these are two different kind of code paths, which is okay. Forever point towards Dave.

[00:13:28]
However, this is most of the thing demonstration sensing if, let's go to sensing, I got it. So, we're gonna use the operator. So, we got less than and greater than two as well, right? So, we'll say if The distance to Dave is let 's say less than a 100, move towards him, otherwise wait.

[00:13:54]
So that way they are like around the field waiting, and Dave gets close to one of the clones, then
>> They're chasing.
>> Then I'll chase him, otherwise I'll stand and wait so it'll be spread around, and only when it gets close enough. I've chose 100, I don't know what the right answer is here, which is why we're gonna need to play-test.

[00:14:12]
>> Do 50.
>> 50? All right, so 50. So, only move if we're close. Cuz I'm never gonna get that close to the corner, because the taco's never going there. 50 seemed a little small. Let's just have it do right where the other one started, Like we had before.

[00:14:40]
Let's try that out now. Okay, so, we get taco, too close to that edge, I don't know if I love that edge mechanic. All right, so now he's waiting, he's watching, 50 doesn't seem close enough. Let's try 100. And it might not even be 100, it might be 200.

[00:14:56]
>> Maybe.
>> Okay, yeah, let's try 200. And again, it might be 150, who knows?
>> Can we have the taco clones right on them?
>> 200 seems to be too much.
>> Can the taco clones were Evad is?
>> You got close enough to get them away.

[00:15:22]
Now we got a game going, Mark.

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