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

The "Custom Blocks" 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 a falling loop with random x positioning for the apple sprite. Creating custom reusable code blocks to avoid building the same code multiple times is also covered in this segment.

Preview
Close

Transcript from the "Custom Blocks" Lesson

[00:00:00]
>> Let's say I wanna have a game where apples are raining from the sky, right? And we have a character that picks them up and catches them. Things I don't wanna do with my life is have to make a whole bunch of different apples, right? Now, we saw with before, we can use clones, right?

[00:00:21]
But that's still, I could do that, I could have an apple fall and then delete the clone, we can try that out. The other trick that you use a lot in games is when that apple's gone, when it's at the bottom, I can just reuse it, right? Something, something, garbage collection in JavaScript.

[00:00:42]
Yes, you can create a bunch of objects and have the garbage collector clean them up. Or you could just wait until it leaves the screen, move it back, something, something, infinite scroll, I don't know. So we can try that out. And so what we'll say is forever, change it by that.

[00:00:59]
And then what we wanna do is to, basically, if it hits the bottom, let's see, it looks like it's all the way down to 180. And sometimes going for strict equality is a problem, but we'll try it out. We can always adjust it if we need to. If, and then we're gonna say the y position is less than 180 cuz it's a negative number.

[00:01:26]
So you drag in that, we'll say less than, Equal, let's say less than 178 so we got a little bit of wiggle room in there. And we can go into motion and get the current y position. Right, if that is true, Then I kinda want you to do this initial setup piece again, so we'll grab that real quick.

[00:01:54]
We'll duplicate it, say that and that. So fall, and if you've fallen as far as you can fall, go back up at the top and do it again, right? And so now, Let's see. My code, y position, Change by velocity, Less than negative 178. My code did what I told it to, it falls, it falls.

[00:02:40]
There's a documentary with two Norse gods where he has to fall for half an hour repeatedly, this is basically the same concept. The movie's called Thor: Ragnarok. This is basically the same idea where it just keeps falling into the same hole, except this one the velocity starts over every time.

[00:02:57]
But we can use this mechanic to create the simulation, even, of multiple apples falling. Which is kind of what's happening, but because it's happening in the same place, it kinda feels a little weird. So what could we do to make it feel a little bit more natural? If it's raining apples, do they all come from the same place in the sky?

[00:03:20]
No, they come from all over the place, right? You've been in an apple storm before, right? So what can we do to simulate a little bit more like they're coming from all over the place, by also using the same apple?
>> Pick random position, so set x to pick random in there.

[00:03:41]
>> Yep, let's do that. Set x to pick random, let's do the same thing we did the tacos, somewhere between negative 160, and was this 220? Negative 220 and 220, so it's not all the way on the edge so we see it. We're gonna do that here. So here we'll go somewhere between these two and the 176.

[00:04:09]
So it falls in the middle the first time, and over here. Next thing you know, all right, it kinda feels like it's raining apples. I'm just gonna look at that for a few seconds. All right, here's the thing that's kinda bothering me is I kinda want that in the beginning, too.

[00:04:29]
But you notice it's getting real tedious, right? Let's say I want the first apple to be somewhere in that random one too. I could duplicate this again and put that there, and now it starts in a random place. But what if I wanna add even more logic? This is getting annoying, right?

[00:04:47]
I'm getting tired of duplicating things. Is there a way that we can make our own blocks?
>> Uh-huh.
>> Yeah, where?
>> Yeah, if you go down to My Blocks.
>> My Blocks?
>> And click Make a Block.
>> Make a Block, we're gonna call this one Reset Apple, or Put Apple at the Top, what do we like?

[00:05:10]
Drop Apple. Cool, and that's it, it doesn't need to take any arguments. So we'll define this idea of dropping an apple, where we'll take this code here off. And we'll say, we've got this new idea of dropping an apple. And now, when the game starts, get ready to drop an apple.

[00:05:34]
And every time that apple falls to the ground, we'll just run this code. Now, if we need to change any of the mechanics of an apple dropping, we simply can edit this right here. So now it runs, it calls that again. My randomness doesn't feel great, but it is, As it goes over and over and it keeps moving that apple around.

[00:05:58]
Awesome, so now anytime you find yourself doing the same thing over and over again, right? You can make your own block and you can have it set to drop the apple like that.

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