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.
We will check when a cannonball and an enemy collide and take proper action accordingly. The steps to do it are as follows:
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.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:
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.
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.
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:
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.
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:
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:
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.3.145.186.83