Practice: Interactive Games

In this activity, we'll look at the basic game, then you'll add code to shoot when you click.

Before You Begin

You need to be familiar with trigonometry, vectors, and using Gamefroot to run this tutorial. If you need a refresher, consult the following resources:

Word to the wise: This tutorial comes with a lot of blocks already created. Be careful when creating new variables! You must be familiar with the way variables work in Gamefroot, or you might break something.

Vanilla Run

Play your game.

You should see a single monkey and a series of bloons. Right now, the monkey doesn't shoot. The bloons will successfully invade!

Over the course of this lesson, you'll give your monkey the tools it needs to fight off this invasion.

Add Controls

Now let's change the game's behavior.

Edit the script called Tower Controller.

Tower controller script in sidebar

You will see that the Tower Controller does a lot of things already. We've written a lot of code for you, taking care of advanced communication.

We're going to look at the section marked When the stage is pressed with pointer.

Stage pressed event in Blockly

This is an event block. It will run automatically when you press the area of the game (called the stage). It has two blocks right now. These blocks record where the press occurred. We use properties so this data is available in other functions. That will be useful in a later chapter.

Let's add a monkey attack to the click.

This script already has a list of monkeys, so we'll pull the first (and only) monkey from that.

  • After the other blocks, add the function attack with: monkey.
  • In the monkey slot, add the first monkey.
    • Add the in list [] get... block, and set it to first.
    • Socket in the monkeys list property.

Stage pressed event with completed monkey attack function

Presto! The loop now reads the list of monkeys.

Play Your Game

Now test your game. Once it has loaded, you should be able to press on the field. What happens?

You should see your monkey turn and throw a dart to the right every time you press. Pretty cool, eh?

But perhaps we want to shoot in other directions. Well, we can do that too! All we need is a little help from trigonometry.

The next section is more advanced. We're going to learn about coordinates and some trigonometric functions. It's all very important.

Save your game before continuing.

results matching ""

    No results matching ""