Build a simple shooter game

Lead Image © kudryashka, 123RF.com

Ready, Aim, Fire

We show how to use Scratch sprites to shoot at various targets.

In this project, I show how to shoot an octopus along a parabolic (curved) path by using Scratch's built-in timer and a simple calculation. As the cannon continues to hit the target, the game gets more difficult by building a taller wall between the cannon and the target. Figure 1 shows the game.

Figure 1: The game in action.

The actual sprites used for this game do not matter too much, but I will use four sprites total: a cannon, a bullet, a wall, and a target. In my setup, the bullet is an octopus and the target is a character from the list of fantasy sprites included with Scratch. The wall can be drawn in Scratch's Paint Editor.

Building a Wall

In the game, the wall separates the cannon from the target, and it will get taller as the target is hit by the bullet. To handle the visual effect of the game, you can add a new sprite using the Paint New Sprite button in the Scratch interface.

In the Paint Editor that opens, use the rectangle tool to draw a solid rectangle. A bit of trial and error will get a good-sized wall. You can rename the sprite "wall" and change the name of the first costume to something memorable, such as "small wall."

Create two more costumes for the wall sprite with the names "medium wall" and "tall wall." An easy way to do this is to copy and edit the first costume. You can use the rectangle tool to draw another section of wall to add on to the existing piece. As the names suggest, you will have one sprite with three different costumes that correspond to wall heights. Figure 2 shows the wall sprite with the list of costumes.

Figure 2: Wall sprite with costumes.

Creating a Target

Figure 3 shows the two scripts assigned to the target sprite. In the first when green flag clicked stack, the script initializes some starting values, such as size, location, and direction. The forever loop moves the target four steps at a time but keeps the sprite between the wall and the edge of the stage.

Figure 3: Scripts for target sprite.

Many introductory Scratch projects show how to move a sprite across the width of the stage using the if on edge, bounce block from the Motion palette. However, in this game, the target's movement will be restricted by the wall. If the target touches the wall, the sprite will do a 180-degree turn, using the calculation direction - 180, and the new direction is passed to the point in direction () block. The same direction calculation is performed if the target touches the edge of the stage.

The script, as written, will move the target horizontally across the stage. If you want to move the sprite up and down, for example, you can change the initial starting direction of the sprite to 0 instead of 90, and the rest of the script in Figure 3 would move the target vertically on the stage instead of horizontally. The movement of the sprite will add a level of difficulty to the game. To make the game easier, remove the forever loop with the motion.

The second script shown in Figure 3 (on the right) tracks the health of the target. I'm building health into the target because the game assumes that eating octopus is good for you, even if the target tries to resist eating it. The health variable is created in the Data palette and is created for all sprites.

The script initializes the variable to a starting value of 0 and then waits until health equals 5. This is the "game over" check. When health = 5, the sprite resizes and repositions itself on the stage. The stop all block ends all running scripts, effectively ending the game.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content