Creating a landscape

As the final stage of this project we will create a hill for the targets to sit on. The landscape will have a diagonal slope; we have to change the parabola trajectory to shoot at different points on the hill. The finished game will look like the following screenshot:

Creating a landscape

Engage thrusters

We could manually draw landscapes as sprites. But if we want to create many different levels, it could take a lot of work. Instead, we will make a drawing tool to create the hill.

  1. First, create a copy of the cannonball by right-clicking on the sprite in the Sprites view and choosing duplicate, shown as follows:
    Engage thrusters
  2. Click on the i button on the copied sprite and change its name to drawing tool.
  3. Throw away the scripts in the copied sprite. Those are only useful for the cannonball, not for the drawing tool.
  4. Switch to the Costumes tab and change the color of the drawing tool to dark green using the color swatches. This is mostly to make it visually different from the cannonball, so that we don't confuse the two.
    Engage thrusters
  5. Go back to the Script tab so that we can create a new script for the drawing tool.
  6. We start the script again with a when <green flag> clicked block.
  7. Attach a pen up block to make sure the tool doesn't draw anything while moving to its starting point.
  8. Add a clear block. This might not look useful now, but like with the hide and show commands in the target sprite script, this will help clear the screen once we want to restart the game.
  9. Move the tool to its starting point with a go to x: () y: () block.
  10. Fill in 240 after x and 0 after y, so the tool starts at the right edge of the stage.
  11. Next, set the pen size to 5 using a set pen size to block. Again you might want to create a thinner or thicker line.
  12. Choose a green color for a natural-looking hill in the set pen color to … block.
  13. Place the drawing tool on the stage with a pen down block. We are now ready to start drawing the landscape.
  14. Then, we move the pen diagonally to the bottom edge with a go to x: -100 y: -180 block. Because the pen is down, it will draw a line between its start and end point.
Engage thrusters

To make full use of our newly drawn landscape, we have to set the targets down on them. But how do we do that on such an uneven surface? The solution is to slowly move the targets down and use another collision test to decide when the targets have reached the landscape and should stop moving. We add the following script to the target sprite:

Engage thrusters

The steps to perform this test are as follows:

  1. Click on the target sprite so you can view its scripts.
  2. Add a repeat until () block at the start of the clone script.
  3. Inside the repeat until loop place a change y by () block and fill in a negative number to make the targets move down.

    I filled in the number -4 for a fairly slow speed. You may take bigger steps, but then the targets could end up being stuck partly through the landscape. Not a big issue, but it might not look as nice.

  4. To stop the targets from moving when they reach the landscape, add a touching color ()? condition to the repeat until () block.
  5. Click on the color box and then point and click on the green line that you've drawn.

Computers are very precise about color. Keep this in mind if your color collision doesn't work. Most likely, the actual color of the object will be slightly different from the color that you checked for. You can't see it with the naked eye, but the computer can tell the difference based on the color number. The finished script should look like the following screenshot:

Engage thrusters

As a final step, change y: 0 to y: 180 in the other target sprite script. This will place the target at the top of the stage and make sure that it doesn't end up inside or underneath the landscape.

Now that we have another object on the stage, that is, the landscape, there is one more thing we have to do to finish the game. When the cannonball hits the ground, it should stop instead of moving straight through the landscape. This is similar to the addition we added after including the targets. Now we only need to add another collision check for the cannonball to respond to.

Engage thrusters

The following are the steps for this collision check:

  1. Get another () or () operator block.
  2. We will check for hitting the green landscape color, just like we just did with the targets.
  3. In the other slot, we will place the entire condition check, like we made earlier. So our latest () or () block will become the outermost block in the construction.
  4. Place the entire construction back in the repeat until () slot and we're done.

The cannonball will now respond to hitting the stage edge, a target, and the landscape.

Engage thrusters

Objective complete – mini debriefing

That's it! We should now have an automatic landscape complete with a functioning cannon, firing script, and targets.