Now that we have enemies running around our game, we need to find a way to stop them. To do this, let's use the tried and tested way of shooting them to bits. In our last game, we already made something to shoot with—the cannon and the cannonball. We can easily reuse these sprites in this game and write new scripts for them.
To transfer sprites and scripts from one project to another easily, we can use the new Backpack feature. Look at the bottom of the Scratch window and you will see a narrow bar with the word Backpack on it and a small arrow pointing up. Click on the bar to open your backpack. If you haven't used this feature before (or aren't logged into your account), your backpack will be empty.
We will get the sprites from the previous project and place them in the backpack as shown in the following steps:
The backpack feature gives you easy access to often-used sprites and scripts. Over time, you will probably collect a standard set of objects to place in your backpack and carry with you at all times. Think of standard movements and keyboard control scripts; for example, if you build a series of games or animations, you might also reuse the same character sprites often.
Now that we have a cannon and some ammo, we can start doing something about those enemies running free. In this game, we will place cannons on the stage with a mouse click. The cannons will aim towards the cursor point. So contrary to most Tower Defense games, you will have to aim at the enemies manually by pointing the mouse at them. The cannons will fire cannonballs automatically.
Like with the targets in the Artillery Game and the enemies in this one, we will script a base object that spawns clones to multiply the cannons and cannonballs as shown in the following steps:
You can now place as many cannons as you want all over the stage. We will refine these scripts later to improve the game challenge. But let's script the cannonballs first so we can actually shoot enemies instead of just pointing at them menacingly.
We want to fire all the cannons we place. So the clones of the cannonball have to be placed at all cannon positions repeatedly. How do we turn one cannonball into many? And how do we keep track of what all of them will do? This might seem like a challenging problem, but actually it isn't too difficult. Moving the clones will be very similar to the Artillery Game example, minus the gravity. The challenge here is to start all clones at the right spot, this being the position of all the cannons. Because the number of cannons will increase as we place more on the stage, the number of positions to remember is variable. So the obvious conclusion is to use a variable to store the positions. But a regular variable won't suffice because that can only store one thing at a time. We will have an increasing list of things to store, so instead of using a regular variable, we will use a list to store the cannon coordinates.
We will create two lists for ease of use. One will store all the x coordinates in order. The other will store all the y coordinates. We will use these lists as a combined pair. Keep that in mind and be careful that you don't switch around the sorting order of either of them, because that will make a mess of your stored data and cause unpredictable results. The following are the steps required to create the lists:
cannon-x
.cannon-y
.These loops will make sure both lists are emptied when we start a new game. Now we are ready to fill the lists with new coordinates. We will store the cannon coordinates when a clone is created. Then we will use these saved coordinates to place cannonball clones at all the cannon locations. To do this, follow the given steps:
If the lists are shown on your stage (remember to select the box in front of the variable block), you can see how they are filled with coordinates as you place cannons on the stage. Try this a few times to get familiar with how lists are constructed and reset.
Now that we are properly saving the cannon coordinates, let's use them to create some cannonballs as shown in the following steps:
i
. This is a temporary variable that we will use as a counter to step through our lists. Using the letter i (and j, k, l, and so on for <> temporary variables) is a programming standard.0
to start counting from 0.1
to start counting up.1
of <cannon-x> block.10
is fine.10
steps block. You can leave the default number here as well.The clone script will cause the clones to face the mouse cursor. Then, they will start moving for a limited number of steps before being destroyed. This will cause the cannons to have a limited range. If you would like a longer or shorter range, you can change the default numbers. The repeat number determines how long the cannonball will be on the stage. The move number will determine how fast the cannonball moves. Keep in mind that very fast moving cannonballs might skip over enemy positions and therefore miss, even though it will look like the cannonball moves straight through them.
It's generally a good idea to collect all the initialization scripts that aren't tied to a specific object in the Stage object. That way you will always know where to find these variables and you don't have to search through many objects and scripts. You will see that in many cases, this is used to reset the global variables such as lives, points, power-ups, and in this case, object coordinates.
Lists are a type of variable that can contain more than one value. I compared a variable to a jar containing one thing. You can compare a list to a shopping list. The list can contain things that belong together, like all of the groceries we want to get from the shop. Not only do we want to get pickles but we also need bread, butter, cheese, tomatoes, and so on. Those all go on the list. When we get an item, we can strike it from the list; thus, deleting it. Oh, we also need onions. We can add that to the list. You can add and remove things from lists as required. You can even add things in between other things by writing between the lines. But in the case of a computer, all the things that follow will just move down a bit to make room for the things that were added.
There are two types of variables. They work essentially in the same way, but their scope is different. Scope is a word that means how far their influence reaches. You have global variables that can be seen and used by all objects in a project. In Scratch, you can set this with the For all Sprites option, which is the default. You can change this option to For this sprite only to change the variable to local. That means this variable is only known to the sprite that it was created for. You can't use this variable in other sprites. This can be useful if you have similar variables that need to have a different value for each object, like a personal speed value for each enemy we use in this project.
3.144.9.124