Fart Catcher

Power-Ups

At the moment you have just one type of collectible: a fart cloud that gives you one point when you grab it. On this card, you’re going to create a new type of collectible, in a way that will make adding other types of collectibles 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 collectible 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 fart:

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=1thenswitchcostumetofartCloud

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 fartCloud 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).

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’ve drawn a supersize fart cloud, but you can make whatever you like!

Next you need to 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=1thenswitchcostumetofartCloudiftype=2thenswitchcostumetosuperFart

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. On the next card, 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 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. By using this My blocks block, normal fart clouds still add a point, and the new power-up adds a life.

iftouchingPlayer Character?thenreact-to-playercollectable-typedeletethisclone

Using collectable-type to create different collectables 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 using the value that is stored in collectable-type at the time Collectable clone gets created.

One of the things that makes clones special is that they cannot change the values of any variables they start with. They 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. Let’s pick the number at random, to make a random collectable every time and keep things interesting.

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.

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