Build a simple shooter game

Ready, Aim, …

With a target in place, you'll need something to aim at the target. I found a free cannon sprite online because Scratch doesn't have any cannons bundled into its sprite library. Any sprite can work as a cannon, and you could draw your own. If you follow this project, you can download the cannon sprite online [1].

The script in Figure 4 is straightforward. After initializing the starting appearance, the script senses the right and left arrow keys to aim the cannon by turning the sprite clockwise and counterclockwise.

Figure 4: Script for aiming cannon.

Shoot the Octopus

The octopus represents the bullet sprite in my storyline, so I'll turn my attention there next. Figure 5 shows two scripts for the bullet sprite. The first script sets the initial sprite values, and several important things are happening.

Figure 5: Script for bullet sprite.

The player can select a power level for each shot. To enable this, you can create a variable named power. After you create the variable, a stage monitor appears. Right-click on the monitor and select the slider option. To make the slider control easier to use for the player, you can right-click on the slider and use the set min max option to set an allowable range of 1 to 10, which will restrict the player's power input. The final initialization sends the bullet to the cannon and then hides it.

The second script in Figure 5 controls the movement of the bullet (octopus). The cannon will shoot when the space key is pressed. To shoot, the bullet is pointed in the same direction as the cannon and is unhidden with the show block.

The reset timer block used in the script sets Scratch's built-in timer to zero. The timer continually counts, so after resetting, it will begin counting up. Inside the repeat until () block, the move () steps and change y by () blocks give the bullet its path.

The value in the power variable is passed to the move () steps block. The higher the value assigned to power, the further the bullet will go, because it moves more steps with each pass through the loop. The change y by () block uses the calculation -5 * timer to get an curved trajectory. As the bullet advances across the stage with the move (power) steps command, the change y by ((-5) * (timer)) command pulls the bullet down along a curve.

To demonstrate the effect of the timer value, you can calculate the value of one, two, and three seconds. At one second, the bullet drops (changes y position) by a value of -5. At 10 seconds, the drop is -10 and at three seconds, it's -15. That verifies what you see when you play the game. When the octopus is shot from the cannon, it will eventually fall to the ground unless it hits something else first – just as in real life.

In this game, the bullet continues to travel until it touches the edge of the stage, the target, or the wall. When one of those conditions is met, the bullet is reset to its starting position at the cannon. After the bullet hits one of the three objects, the script specifically checks to see whether the object it hit is the target. If the bullet hits the target, the game increments the health value and broadcasts a hit message.

To see what happens with the hit message, take a look at the wall sprite.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content