Fighting back

So we can now place cannons that shoot cannonballs in the direction of the mouse cursor. But we still aren't hitting any enemies. Let's quickly fix that problem.

Engage thrusters

We will check when a cannonball and an enemy collide and take proper action accordingly. The steps to do it are as follows:

  1. Inside the cannonball, clone repeat, and place an if … then condition block to check collisions.
  2. Check for collisions by using the touching <enemy> block.
  3. When a cannonball touches an enemy, first wait for 0.05 seconds by using the wait ... secs block. This is to ensure that the enemy has time to respond to the collision as well.
  4. Then we add a delete this clone block to delete the clone.
    Engage thrusters

So cannonballs will not only expire after having traveled a certain distance, but also when they hit an enemy. You could omit this step, but that would make the game a bit too easy because cannonballs can move on and hit multiple enemies, as was the case in the Artillery Game before we fixed that.

The enemies have to respond to being hit by a cannonball as well. So let's add that condition to their script. The condition to check for the enemy is actually the same as for the cannonball, but in reverse order. So the easiest way of adding it is to copy the script segment and change the variable pieces as shown in the following steps:

  1. Click-and-drag the if statement from the cannonball to the enemy sprite in the Sprites view.
  2. Click on the enemy sprite in the Sprites view to look at the enemy scripts. You will see the segment you dragged has been copied to the enemy scripts.
  3. Create a new when I start as a clone script.

    You can't use the other one, because that will be busy gliding the clones from point to point. These glide blocks are effectively worked through one at a time. So the script will pause for a short while at each step. Adding a repeated condition check would disrupt the fluid movement of the enemy sprites.

  4. Add a forever loop.
  5. Place the copied segment inside the forever loop to check continuously.
  6. Now change the object to be checked to cannonball.
  7. You can remove the wait … secs block because the enemy is allowed to respond immediately when it touches a cannonball.
  8. Save the project.
    Engage thrusters

Objective complete – mini debriefing

Test your game and see how it works. The bare basics of the game are now in place. Enemies move along a given path and you can place cannons that shoot cannonballs at them to stop the horde from reaching their destination. You might also note that this game is way too easy. Cannonballs are spawned at a rapid rate and cannons can be placed anywhere on the field in large numbers. This makes it impossible for the enemies to reach the other side of the map intact, unless you decide to do nothing.

The rest of the project will focus on adding more to the basic scripts to make the game more challenging. We will slow down the spawning of cannonballs, create more enemies, and limit where and when you can place cannons.

Classified intel

At this point, cannons can be placed anywhere, including on the road. This can make the game too easy and it also looks a bit messy. Let's limit where you can place cannons. We will build a few gun platforms and place them within in the level. Cannons can only be built on these platforms, so the player has to start thinking about the cannon's range and optimal placement. It also prevents the player from putting cannons in the middle of the road. Follow these steps to create a platform:

  1. Click on the paintbrush icon to create a new sprite.
  2. Select the Rectangle tool and choose a medium gray color.
  3. Draw a vertical rectangle.
  4. Check the shape as it appears on the stage to determine the right size.
  5. Pick a lighter color of gray and fill the rectangle with the Fill tool.

You can imagine this to be a raised concrete platform when looked at from the top. You can embellish the graphic if you wish, but it won't be necessary for the game. Remember to keep your drawings clear so the player can easily understand what the drawing represents.

Classified intel

Rewriting the placement script is fairly simple. We will relocate the script that we already wrote from the cannon sprite to the platform sprite as shown in the following steps:

  1. View the cannon's Scripts tab.
  2. Drag the entire when this sprite is clicked script to the platform in the Sprites view.
  3. Click on the platform sprite to see its scripts. You will find a copy of the when this sprite is clicked script there. If not, you have probably misplaced the script. Try again in this case.
  4. Make sure that the script creates a clone of <cannon> and not of <myself>. We don't want the platforms to start copying themselves. Correct this if needed.
  5. After you have copied and edited the script, delete the original script in the cannon by right-clicking on it and selecting delete.
  6. Next, right-click on the platform sprite in the Sprites view and select duplicate to create a second platform.
  7. Drag and place both platforms to the correct place on the grass between the bends in the road. They should be placed as shown in the following screenshot:
    Classified intel

Test your script and see how you can now only place cannons on the two platforms and not on the grass or the road.

Slowing down the rapid firing of cannonballs just requires a small addition to the script. Remember how everything in "computertime" goes really fast, but you can slow it down with a wait command as shown in the following steps:

  1. Have a look at the cannonball's Scripts tab.
  2. See how the forever loop in the <green flag> script has no wait block in it at all. Cannonballs will therefore be created as fast as your computer can manage.
  3. Place a wait … secs block just inside the forever block and fill in 0.1. This will slow down the creation of cannonballs a bit. You can increase the number for an even slower spawn rate and a more challenging game.
Classified intel
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.77.158