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

The "Collecting Apples & 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 implements the ability to collect apples and adds falling enemies for the player to avoid.

Preview
Close

Transcript from the "Collecting Apples & Adding Enemies" Lesson

[00:00:00]
>> And so I guess this is very similar to what we had before. We would say if now if the apples touching pico, we probably also want to say that's the point but it should vanish if he caught it, right? So we can go back to this apple.

[00:00:13]
And what's cool about this is we already gave ourselves a reusable code block, right? We have a concept here about how to move it back. And so we don't have to do it. Because imagine we did it once for the setup. We did it twice when I hit the bottom.

[00:00:28]
We did it three times. You can see how over time this gets out of control, you want to change one place. Now you would have had to change the three places, but we don't have to do that. So what we can do is say, If touching Pico, Then what we want to do is, we want to go back to dropping the apple.

[00:00:59]
We could, theoretically we could say, we can make a for all Sprite score, So we could, change score by one, so we can take that code, and we can build on top of it, and it will still all work. So we can change the score by one. We can drop the apple again.

[00:01:19]
We could even, if we really wanna get fancy with ourselves, we could broadcast, Apple caught. Right? And then we could have them say yum or something like that. So we can go back over to Pico and say,
>> Yum, 4.5 seconds. Yum
>> 4.5, we'll try it. I have this feeling that's gonna be shockingly too long.

[00:01:47]
>> No, just point, 0.5
>> 0.5, all right, I'm here for that. That's the correct number. Yum. Yum period. Yum, yum.
>> Maybe we yum question mark?
>> Yum?
>> Yum?
>> [LAUGH]
>> Yum. All right, so we've got some some mechanics here. You know what this game needs?

[00:02:10]
>> Sound? Nope, wrong answer. [LAUGH] Does not eat sound.
>> You need to make your recording of you saying, yum.
>> Yum, yeah, that is a really great exercise when you get home at your father, all night, [LAUGH] and for the next several days. So, what we could do is, there's sometimes the concept of we had for EVAD, right?

[00:02:41]
We wanted to make a clone, right? We could say that okay, maybe we want to increase the number of enemies every time, we get one of these bombs but maybe they don't or anytime we get one of these apples. But maybe we don't want them to start off at all, right?

[00:02:55]
And so that's a thing that we can do as well let's make one more sprite. What should Pico avoid?
>> Apple's hitting the ground.
>> Watermelons.
>> I don't know if I have watermelons.
>> No, no, no, no avoid collecting.
>> Guitars.
>> Fine, good global.
>> Globals falling from the sky?

[00:03:19]
>> How about griffin.
>> Hedgehogs, look at these, look at this. Yeah, we're doing hedgehogs.
>> Make them spinning around real fast.
>> I just like this view. Yeah, this is correct and right and it really works for a visual aesthetic with a Goldenrod color up, Caleb, so I'm into it.

[00:03:35]
These hedgehogs are a little bit too big.
>> [LAUGH]
>> Make it make it like spinning around.
>> I like this idea. Give me one second.
>> A little bit bigger.
>> [LAUGH]
>> You can make it spin.
>> Maybe it's 30.
>> There it is. Okay, and so maybe we don't want any of these originally.

[00:03:59]
So the trick you'll see a lot of times is, for a lot of times we've used clones but in this case we want the idea of I have what we might in some programming languages have as a class. I wanna make more of these but this one in and of itself I'm never going to use.

[00:04:14]
So what I might do in that case is, I'll say, when we start the game Hide my actual hedgehog, right?
>> It needs to have a chance needs to be picked random one to 10 and if it's 10.
>> Random hedgehog attacks, cloudy with a chance of hedgehogs?

[00:04:42]
>> Like clone of itself 12 times.
>> What we could say is, Let's see whenever we say when apple cart is received, I'm going to create a clone of myself, right? When I start as a clone, then we'll do very similar logic to what we had with the apples.

[00:05:10]
>> Except do it a little bit faster.
>> I'm into it.
>> There will be hedgehogs falling for us.
>> We'll drag that to move some of the code over. When I start as a clone. Go to one of these places, sweet, and then, let's, when it starts to clone, that should happen.

[00:05:35]
It should fall. Nothing should happen if it touches Pico yet, though. Let's try it out. Pico, you need to get an apple. Let's see. Set velocity negative one. We don't have a falling mechanism, right. Yap, not for the hedgehog.
>> Not for the hedgehog. So we'll copy that over too.

[00:05:53]
>> Make edgehog defies gravity.
>> Yeah, copy, copy.
>> It's like gravity defying hedgehog.
>> That's what I said.
>> Grab the apple.
>> Go ahead.
>> Grab the apple stuff and then-
>> We just want it forever it, right? For now, Hey, forever. We just don't have a game loop in here.

[00:06:10]
Ever this will fall once, right. I kind of like the two of you just get every time we get an apple random hedgehog tries to attack. Alright, it's not showing up though, so let's see.
>> Just start again.
>> Yep, I have hidden secret hedgehogs, right? So we didn't want to begin with that idea of a hedgehog.

[00:06:28]
>> Ninja hedgehogs.
>> Ninja hedgrhogs, so we do need to show them.
>> You should have when you get too high of a score, then they become invisible, so it's a lot harder.
>> You can't tell if you're touching them. I like the idea of ninja hedgehogs.
>> No, make them more like-

[00:06:44]
>> Boy.
>> Make them get lighter.
>> [LAUGH]
>> But we have a problem.
>> [LAUGH]
>> You don't have enough hedgehogs.
>> Are you sure we'll have enough hedgehogs?
>> I don't know.
>> It looks like a thundercloud of hedgehogs [LAUGH].
>> It's way too many hedgehogs.

[00:07:07]
>> You should have, if you get hit by a hedgehog, you get it.
>> Let's look at this code. It makes total sense, right? Which is, we set the velocity to -1, okay, we do that over and over again. And that is, as far as I can tell, that could possibly be the same hedgehog, moving at such an erratic rate that it has the appearance of a storm of hedgehogs, without actually being multiple hedgehogs.

[00:07:27]
>> I don't think you added the falling.
>> Yeah, we didn't change the Y yet.
>> So it's a magical ninja hedgehog. It's like a wizard ninja hedgehog.
>> It's like a wizard ninja hedgehog, because now We just have one that'll just hang out. Yep, see, now they just hang out.

[00:07:42]
They do get a little out of control every time.
>> [LAUGH]
>> Looks like the porcupine, I mean the hedgehog, for pooping apples on you. [LAUGH] And then he eats them, yum.
>> Yum.
>> Yum.
>> Yum. All right so we set the velocity negative 1 but we gotta move in by the velocity so we'll change that Will change We've got to move by negative one.

[00:08:22]
>> All the clones are gonna have that message receipt or that broadcast.
>> Each one.
>> Receipt message, right? That's why they're exponentially growing.
>> Yeah, so we might need to move this logic. So I was like off roading a little bit you get to watch me live figure stuff out and about I can't you know I don't hate this mechanic [LAUGH].

[00:08:41]
Like this game is going to end very shortly. Like I think he needs some kind of like blasting mechanism yeah each one Each one receives this apple call, which means each one triggers a new one. So, I might have to move that logic. Luckily, I can trigger the clone for when the apple touches Pico.

[00:08:57]
>> Hey, doc, you're at the bottom, plotting your demise.
>> Yeah. So we've got a bunch of these here. So, you'll notice that they didn't reset. So, if I actually set the
>> Whoo [INAUDIBLE]
>> More.
>> More, don't make more hedgehogs.
>> Yeah, it's not like there is a clone maximum at some point like this will like hit its natural conclusion and it will not let like any more get created is another part as well.

[00:09:39]
Yeah, I think we've hit the maximum hedgehog at some point. So we're gonna have various we can, we might want to move this because this is not working for us
>> Okay, make it make it delete this clone when it hits the ground
>> That'll work right like but then all existing ones will still make a new one.

[00:09:57]
>> No.
>> Right so we'll we'll get a happy medium between the two mechanics
>> Already been deleted.
>> No but if like they'll delete but if there are two that will make four
>> 4 will make 8. When they hit the bottom, they're gone. So yeah, it's about like, you almost don't want to get too many apples until the hedgehogs have cleared out.

[00:10:14]
I like it. Let's try it out. So forever, and then we'll say...
>> Delete, like, when touching.
>> If and then you get the color of the ground. [SOUND] Yeah, so we can say if touching the ground. Let's do a little bit.
>> When you got a cold in the actual, you can code in stop all touches the head hedgehog.

[00:10:39]
>> Yeah, or yeah
>> Or you can make pico when when you get smushed. Like he gets smashed by the hedge hog you can-
>> I can I gotta go home go to costumes-
>> You can go home go to the costumes you can make him switch costume I haven't.

[00:10:57]
>> A lattened one.
>> Yes.
>> Yeah, there's a tonne of things we can do.
>> Ones that been exploded that's what I did for everyone.
>> Or you can make him hurt. You can make-
>> You can go out.
>> Or you can make him dead.
>> You can put needles in him.

[00:11:19]
>> Yeah, I can have different costumes that he switches to in various states of pain and agony.
>> [LAUGH]
>> Yeah, that will do it. Let's show that deleting the clone too, because there might be cases where you might want to delete clones. So we'll say if that Y position gets less than 60.

[00:11:33]
It will get deleted. Normally I'd like to make them behind the background but I like to just show it happened, right? He is not accelerated false. Yeah, he's going to velocity. He's got a velocity, right and so he vanishes. And so now but if there were two when I called another apple if I can catch another apple, each clone does get that message so we might want to move that logic.

[00:11:57]
We'll get rid of this and we'll move that logic back to the apple where we could just basically say create clone of something else. The only thing that a sprite can do that really affects any other sprite is cloning. So here now we should fix that mechanic. So now it's two, it's three, now we've got more of a sane number.

[00:12:22]
Now, as they drop, they will vanish. So it all depends on what mechanic you're going for, but sometimes it's about, there's unintensive consequences from our code, depending on where we put it. And like, this is why you kind of need to like play test and debug and see all those kinds of things.

[00:12:37]
>> Yeah, make them accelerate. To make them have to make new packs or just make them fall.
>> So we said the boss is zero, yeah, we're not changing x, y velocity Initially was that let's say hedgehog.
>> You say velocity.
>> Or I do it in every loop.

[00:13:01]
>> You did set velocity instead
>> I did set instead of change. I coded what I told it to and not what I said to do. Change velocity by negative one. Because all we're just saying, be negative 1, be negative 1, be negative 1, they're like cool, be negative 1, it's cool.

[00:13:22]
Alright, so now I should have a faster hedgehog. Yeah, look at that hedgehog fall. So every time we catch an ash, there is some hedgehog that might, ooh.
>> There should be three hedgehogs falling.
>> So we can do the repeat by three, right? And so we can do that, that would have to go in the apple, right?

[00:13:36]
So there's lots of extensions we can do.

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