In this add-on, you’ll program a power-up to enable your hero to jump higher and stay in the air longer.
A power-up will act a lot like a bug.
It’ll appear randomly and slide across the screen, then it’ll make something happen when it touches the hero.
Since they’re similar, start by copying the bug sprite!
Right click the bug sprite in the sprites menu, then click duplicate.
Rename the power-up sprite by clicking the “i” button in the sprites menu.
Your enemies and your power-ups shouldn’t look the same, so click the costumes tab, and add a new costume to make the power-up look different.
Pick either “choose costume from library” or “paint new costume.”
Next, delete the old bug costume, and go back to the scripts tab.
You may not have seen this code before, so take some time to understand everything that’s happening because you’ll have to tweak it later.
The first block stack puts the original sprite in the right place and hides it, then the loop at the end makes the clones at random times.
What you see in the game are the clones.
You can see in the next stack where the clones show, then move across the stage.
Tinker with the times in the “glide” block and the “pick random” block – consider making the wait time longer so the power-up is rarer.
Click the hero sprite to make the code that will power your hero up!
The value that controls how high the sprite jumps is in the “set y speed” block.
To make it jump higher, make this value bigger.
Make another variable that will change when you get the power-up.
Call the new variable “jump speed,” then drag it into the “set up/down speed” block.
At the top of the block stack, above the motion loop, set jump speed to 15, just like the number it replaced.
Drag out two more “set variable” blocks, and select “jump speed” in both.
In the first one, type a value bigger than the original.
Tinker around with this – the example uses 20.
If you click the flag, then click this block and the up arrow, the hero will jump to heroic heights every time!
But, its energy should wear out eventually.
Snap a “wait” block and the other “set variable” block below the first “set variable” block.
Into the second “set variable” block, type the original value – 15 in this example.
Tinker with the “wait” value to change the amount of time the hero keeps the special power.
Add a new “if” block, and snap it somewhere just inside the motion loop.
It may want to nest further inside or wrap around other code, but don't let it do that.
When you get it positioned, you should see an empty “if” block with one yellow bar beside it.
The condition is a “touching” block.
Select the power-up sprite.
Then, if it’s touching, broadcast a message to make the power-up happen!
Snap in a “broadcast” block, make a new message, then add a “when I receive” block to the top of the new power-up stack.
Select the message you just created.
Click the flag and wait until the powerup comes along.
If it works, great!
Go back and tinker with all the values until you get the power-up working the way you like.
Now, it’s your turn.
Duplicate the bug, and change its appearance to make it look like a power-up.
Add a “jump speed” variable.
Set and change the variable at appropriate times.
Make the sprite increase its ability when it touches the power-up using the “broadcast” and “if” blocks.