This week in Scratch Advanced we looked at two areas of scratch that are really useful to get the hang of if you want to build cool games.
Scratch comes with many blocks built in, but custom blocks let you actually create your own blocks that you can re-use over and over. Using custom blocks can make your scripts much tidier and shorter and easier to write. They are similar to broadcasts, but better because you can pass values to them to make them behave differently.
To create a custom block you go to the “more blocks” menu and add a block.
Give the block a name that describes what it is doing – in the example here I call it “jump”
You can see this is really handy and lets us add jump to our sprite easily without having to copy lots of code around. You can make it even better by adding “inputs” to the block.
To add an input expand the “options” on the block and click the input for the data type you want – in the case of jump, height might make sense as shown below.
Now we have a jump block that will let us use the block for a normal jump and a super jump – maybe after a powerup or whatever. You can see how handy this could be for anything you want to do over and over! If you find yourself writing similar code in more than one place for a sprite, it might be a good time to think about writing a custom block. One thing we didn’t mention on Saturday which is worth a look is the option for “run without screen refresh”. When this is pressed, a block will run very quickly and only update the screen when it’s done. This can be a way to speed up things like drawing programs. Try it out on the jump example above to see what it does. When it’s clicked, it will look like the sprite doesn’t do anything.. this is because he jumped up and right down again only updating when the block was finished – and he was right back where he started!.
Writing a Square Block
On Saturday, we used custom blocks to write a function to draw a square using a pen. Then we showed how easy it was to create nice patterns by just calling the block different ways.
The code we came up with for the block was:
We spent a fair bit of time thinking about X and Y coordinates in Scratch and how to figure out where to move your sprite to get the shapes you want. One example of how to use our new block is below. Lots of the clever scratchers came up with much cooler ones than this though!!