SO FAR, YOU’VE been using Scratch’s built-in blocks and they’re great, but Scratch also lets you make your own blocks that you can personalize and customize to your particular needs. In this adventure, you use custom blocks and a handy tool called the Backpack to put on a fashion show!
If you can dream it up, you can probably create it by using Scratch’s built-in blocks. But, sometimes, your programs can get pretty complicated, with many different blocks all put together in a very precise way.
When that happens, a great way to stay organized and make programming easier is to create your own custom blocks.
Let’s take another look at the arrow key sprite control script that you created in Adventure 4, which is shown in Figure 9-1.
That’s a lot of blocks to just make a sprite move around when you press the arrow keys. Wouldn’t it be great if there were a block like the one shown in Figure 9-2 that you could use to do the same thing as all of the blocks in Figure 9-1?
Well, there can be, and you can make it yourself! We show you how to create custom blocks in the next section.
You’ve learned about and used a lot of different blocks so far, in each of the different categories in the Block Palette. There’s one very special category of the Block Palette where you can create an unlimited number of custom blocks. This is called the More Blocks Palette. To make your first custom block, follow these steps.
Click the More Blocks category in the Block Palette.
You see two buttons in the More Blocks Block Palette, as shown in Figure 9-3.
Click Make a Block in the More Blocks Block Palette.
The New Block pop-up window opens, as shown in Figure 9-4.
Click OK to create your new block.
A new stack block with the name
appears in the More Blocks Block Palette, and a hat block named Draw Circle
define Draw Circle
appears in the Scripts Area. Both of these are shown in Figure 9-6.
Next you need to define what the Draw Circle
block will do. Everything under the custom block’s hat block will run every time you use the custom stack block.
Follow these steps to define the Draw Circle
block and make it actually draw a circle.
pen down
block from the Pen Block Palette to the Scripts Area and snap it to the define Draw Circle
block.repeat ()
block from the Control Block Palette and snap it to the bottom of the pen down
block.repeat ()
block to 180.move () steps
block from the Motion Block Palette and snap it inside the repeat ()
block.move () steps
block to 1.turn clockwise () degrees
block from the Motion Block Palette and snap it to the bottom of the move () steps
block. Change the value in the turn clockwise () degrees
block to 2.
Your define Draw Circle
block script should now look like Figure 9-7.
Draw Circle
custom block from the More Blocks Block Palette to the Scripts Area. Double-click the Draw Circle
block in the Scripts Area.
Scratch the Cat moves and draws in a circle.
Double-click the Draw Circle
block in the Scripts Area.
Scratch the Cat draws another circle, starting in the new location.
Your new Draw Circle
block is pretty awesome. Anywhere where your sprite is on the Stage, when the Draw Circle
block runs, it causes the sprite to draw a circle.
But it can only draw one size of circle, in one color. Wouldn’t it be great if you could draw different circles with this same block? You can! All you need to do is modify your custom block so that you can tell it what kind of circle to draw. Here’s how:
Click the More Blocks Block Palette.
You see your custom block, Draw Circle
.
Right-click the Draw Circle
block and select Edit, as shown in Figure 9-8.
The Edit Block pop-up window opens, as shown in Figure 9-9.
Click the arrow next to Options in the Edit Block pop-up window.
You see the custom block options, as shown in Figure 9-10.
Click the oval icon to the right of the words Add Number Input.
An oval appears in the image of your custom block inside the pop-up window, as shown in Figure 9-11.
Take a look at your block definition in the Scripts Area now. You see that it has a new block called size
next to the name of the block, as shown in Figure 9-12.
You also notice that your custom stack block now has a place in it for entering a number, as shown in Figure 9-13.
Try changing the number in the Draw Circle ()
block in your Scripts Area to a larger number, such as 4, and then double-click it.
Did you see what happened? No? That’s right! Nothing changed.
In order for the number in the Draw Circle ()
block to do anything, you need to use your new variable size
inside of your script.
define Draw Circle ()
hat block. Drag the size oval to the move ()
steps
block and snap it inside of the oval between move
and steps
.
Your custom block definition should now look like Figure 9-14.
Change the value of the number input in the Draw Circle ()
stack block to 3 and double-click it.
Scratch the Cat draws a larger circle on the Stage, as shown in Figure 9-15.
After you have a custom block, you can use that same custom block with any sprite by dragging the custom block definition onto the new sprite. Follow these steps to try it out:
Click Choose Sprite from Library in the Sprite Pane.
The Sprite Library opens.
define Draw Circle ()
hat block in the Scripts Area and drag it onto the image of your new sprite in the Sprite Pane.Click your new sprite in the Sprite Pane.
You see a copy of the define Draw Circle ()
block in the Scripts Area, and the Draw Circle
block in the More Blocks Block Palette, as shown in Figure 9-16.
Draw Circle ()
block from the More Blocks Block Palette to the Scripts Area. Double-click Draw Circle()
.
Your new sprite draws a circle of the size that you specify in the number input.
Now you know how to share custom blocks between sprites in the same program. But, what if you want to share a custom block (or any script, for that matter) between two different projects?
That’s where Scratch’s Backpack comes in.
The Backpack in Scratch is an area where you can store custom blocks and scripts that you create in one program so you can use them in other programs. To access the Scratch Backpack, you need to be signed into your account. If you haven’t already created an account, please refer to Adventure 1 for more information.
While you’re working within Scratch, the Backpack is always waiting there at the bottom of the screen, as shown in Figure 9-17.
When you click the up arrow on the Backpack bar, the Backpack Pane expands to show a new blank area of the Scratch Project Editor, as shown in Figure 9-18.
Here’s how to use the Backpack to carry your Draw Circle ()
custom block, or any other custom block, with you into a new program.
Click and drag your define Draw Circle ()
block definition from the Scripts Area into the Backpack.
A copy of the define Draw Circle ()
block definition appears in the Backpack, as shown in Figure 9-19.
Click the up arrow to expand the Backpack Pane.
You see the define Draw Circle ()
custom block definition inside your Backpack.
define Draw Circle ()
custom block definition from the Backpack and drop it into the Scripts Area in your new program.Open the More Blocks Block Palette.
You see your custom Draw Circle ()
block.
Draw Circle ()
block to the Scripts Area.Draw Circle ()
block in the Scripts Area to make your sprite draw a circle.The Backpack and custom blocks are great tools for helping you to get more done with less coding—which is one of the most important skills for a programmer to learn!
Now it’s time to put together everything you’ve learned about custom blocks to organize a Scratch Fashion Show. In this program, sprites use a custom block to walk or dance toward the camera, showing off their different costumes on the way.
Click the Make a Block button.
The New Block pop-up window appears.
set size to () %
block from the Looks Block Palette to the Scripts Area.set size to ()%
to 20.Fashion Walk
block from the More Blocks Block Palette to the Scripts Area. Double-click the Fashion Walk
block in the Scripts Area.
Scratch the Cat shrinks down to 20% of her normal size.
Drag a go to x: () y: ()
block from the Motion Block Palette to the Scripts Area and snap it to the set size to ()%
block.
Because you just positioned Scratch the Cat, the block already contains the correct values for x and y.
show
block from the Looks Block Palette to the Scripts Area and snap it to the bottom of the go to x: () y: ()
block.repeat ()
block from the Control Block Palette and snap it to the bottom of the show
block.repeat ()
block to 20.wait () secs
block from the Control Block Palette and snap it inside the repeat ()
block.wait () secs
block to 0.1.change size by ()
block from the Looks Block Palette and snap it to the bottom of the wait () secs
block.change size by ()
block to 5.change y by ()
block from the Motion Block Palette and snap it to the bottom of the change size by ()
block.change y by ()
block to –5.next costume
block from the Looks Block Palette and snap it to the bottom of the change y by ()
block.glide () secs to x: () y:()
block to the Scripts Area and snap it to the bottom of the repeat ()
block.glide () secs to x: () y: ()
block.hide
block from the Looks Block Palette to the Scripts Area and snap it to the bottom of the glide () secs to x: () y: ()
block. Drag a wait () secs
block to the Scripts Area and snap it to the bottom of the hide
script.
Your Scripts Area should now look like Figure 9-21.
Fashion Walk
custom block on the Stage and watch as Scratch the Cat walks toward the camera and then glides off to the right!Now you’re almost done! The next steps are to add several more sprites to the Stage and give them all the same Fashion Show
custom block.
define Fashion Walk
custom block definition hat block. Drag it to the Sprite Pane and drop it on the Dan sprite.define Fashion Walk
custom block definition to the Breakdancer1 sprite in the Sprite Pane.Fashion Walk
custom block.when green flag
clicked block from the Events Block Palette and snap it to the top of the Fashion Walk
custom block, as shown in Figure 9-22. Drag a broadcast ()
block from the Events Block Palette and snap it to the bottom of the Fashion Walk
stack block.
You use broadcast events to tell the other sprites to start their fashion walks.
broadcast ()
block and choose New Message.when green flag clicked
block from the Events Block Palette to the Scripts Area.hide
block from the Looks Block Palette to the Scripts Area and snap it to the when green flag clicked
block.when I receive ()
block from the Events Block Palette and drop it in the Scripts Area.when I receive ()
block to Dan.Fashion Walk
block from the More Blocks Block Palette and snap it to the when I
receive ()
block.broadcast ()
block from the Events Block Palette and snap it to the bottom of the Fashion Walk
block.broadcast ()
block and add a new message of Breakdancer1.In the next steps, you make the break dancer walk!
when green flag clicked
block and drop it onto the thumbnail of the Breakdancer1 sprite in the Sprite Pane.when I receive ()
and drop it onto the thumbnail of the Breakdancer1 sprite in the Sprite Pane.Click the thumbnail image of the Breakdancer1 sprite in the Sprite Pane.
If the sprite’s scripts are overlapping, drag them to rearrange them in the Scripts Area.
Disconnect the broadcast () block
from the bottom of the Fashion Walk
custom block and drag it out of the Scripts Area.
Because this is the last sprite to do its fashion walk, it doesn’t need to broadcast to any other sprites.
when I receive ()
block to Breakdancer1.Check out custom blocks created by other people that you can use in your own programs at the Block Library at http://blocklibrary.weebly.com/
.
Achievement Unlocked: Custom Blocks
18.219.189.247