Falling Stars

Power-Ups

At the moment you have just one type of collectable: a star that gains you one point when you grab it. On this card, you’re going to create a new type of collectable, and you’ll do it in a way that will make adding other types of collectables easy. Then you can invent your own power-ups and bonuses and really make the game your own!

I’ve already included some pieces to do this with the collectable-type variable and the pick-costume My blocks block. You’re going to need to improve on them though.

Let’s have a look at how the collectable works right now.

In the scripts for the Collectable sprite, find the when I start as a clone code. The blocks you should look at are the ones that give you points for collecting a star:

iftouchingPlayer Character?thenchangepointsbycollectable-valuedeletethisclone

and this one that selects a costume for the clone:

pick-costumecollectable-type

How does picking a costume work?

The pick-costume block works a bit like the lose block, but it has something extra: it takes an input variable called type.

definepick-costumetypeiftype=1thenswitchcostumetostar1

When the pick-costume block runs, what it does is this:

  1. It looks at the type input variable
  2. If the value of type is equal to 1, it switches to the star1 costume

Take a look at the part of the script that uses the block:

whenIstartasaclonepick-costumecollectable-typeshowrepeatuntilyposition<-170changeybycollectable-speediftouchingPlayer Character?thenchangepointsbycollectable-valuedeletethisclone

You can see that the collectable-type variable gets passed to the pick-costume block. Inside the code for pick-costume, collectable-type is then used as the input variable (type).

This means that the value of collectable-type decides which costume the sprite clone gets.

Add a costume for the new power-up

Of course, right now the Collectable sprite only has one costume, since there’s only one type of collectable. You’re about to change that.

Add a new costume to the Collectable sprite for your new power-up. I like the lightning bolt, but pick whatever you like.

Next, tell the pick-costume My blocks block to set the new costume whenever it gets the new value for type, like this (using whatever costume name you picked):

definepick-costumetypeiftype=1thenswitchcostumetostar1iftype=2thenswitchcostumetolightning

Create the power-up code

Now you need to decide what the new collectable will do! We’ll start with something simple: giving the player a new life. In the next step, you’ll make it do something cooler.

Go into the My blocks section and click Make a Block. Name the new block react-to-player and add a number input named type.

Type in the name for the block

Click OK.

Make the react-to-player My blocks block either increase the points or increase the player’s lives, depending on the value of type.

definereact-to-playertypeiftype=1thenchangepointsbycollectable-valueiftype=2thenchangelivesby1

Update the when I start as a clone code to replace the block that adds a point with a call to react-to-player, passing collectable-type to it.

iftouchingPlayer Character?thenreact-to-playercollectable-typedeletethisclone

By using this new react-to-player My blocks block, stars still add a point, but the new power-up you’ve created adds a life.

Using collectable-type to make different collectables appear at random

Right now, you might be wondering how you’ll tell each collectable the game makes what type it should be.

You do this by setting the value of collectable-type. This variable is just a number. As you’ve seen, it’s used to tell the pick-costume and react-to-player blocks what costume, rules, etc. to use for the collectable.

Working with variables in a clone

For each clone of the Collectable sprite, you can set a different value for collectable-type.

Think of it like creating a new copy of the Collectable sprite with the help of the value that is stored in collectable-type at the time the Collectable clone gets created.

You might be wondering whether changing the value of collectable-type will turn all the collectables on the Stage into the same type. That doesn’t happen, because one of the things that makes clones special is that they cannot change the values of any variables they start with. Sprite clones effectively have constant values. That means that when you change the value of collectable-type, this doesn’t affect the Collectable sprite clones that are already in the game.

You’re going to set the collectable-type to either 1 or 2 for each new clone that you make. To keep the game interesting, pick between the numbers at random to make a random collectable every time.

Find the repeat until loop inside the green flag code for the Collectable sprite, and add the if...else code shown below.

repeatuntilnotcreate-collectables=trueif50=pickrandom1to50thensetcollectable-typeto2elsesetcollectable-typeto1waitcollectable-frequencysecondsgotox:pickrandom-240to240y:179createcloneofmyself

This code gives a 1-in-50 chance of setting the collectable-type to 2. After all, you don’t want to give the player the chance to collect an extra life too often, otherwise the game would be too easy.

Now you have a new type of collectable that sometimes shows up instead of the star, and that gives you an extra life instead of a point when you collect it.