Simple Game Mechanics in Scratch:
Shooting Projectiles

Many obstacles can be removed by firing things at them.

As we continue looking at simple game mechanics, let us take a glance at one of the most popular mechanics: Shooting. Many classic games such as Asteroids, Space Invaders, Missile Command, and Super Mario Bros have used some form of shooting as a game mechanic, from lasers to fireballs, projectiles are a classic mechanic, so today I threw together a simple turret in Scratch to serve as an example for how to put together projectiles and add shooting to your games.

This code is stored inside the projectile, not the launcher.

Just like how Jumping requires both a character to jump and some sort of solid ground or platform to jump off of, Shooting requires both a projectile to be fired and something to fire from. Above is the code for the balls launched from my cannon, while below is the code for the turret itself:

This code is for the turret that fires the projectiles.

As you can see, the turret has a much simpler code than the balls that it shoots. Whatever type of launcher you program needs to be able to move enough to aim your projectiles, but all the code to actually shoot is stored in the projectile itself.

Projectiles make use of Cloning, which is another powerful tool within Scratch. Cloning allows Sprites to create multiple copies of themselves and give each copy a different set of instructions. This allows for code between sprites to be simpler and more streamlined. For example: A Player Sprite could be programmed to take damage from touching an "Enemy" sprite. Without Cloning, the code for the Player Sprite would have to have an incredibly long list of potential types of enemies to check for. With Cloning however, a variety of different enemy types can all be programmed as different clone types within a single enemy sprite. If you want to have a lot of something like background characters, enemies, or obstacles and have those game objects be mostly the same but with slight differences, cloning is the way to go.

It is important to note that cloning always requires at least two scripts: One to create the clone, and one to tell the clone what to do once it is created. There are a lot of ways to program unique clone behaviors using variables to make the clones different from one another. My game "Space Octopus Attack!" uses cloning in a variety of different ways, from the torpedoes that the spaceship shoots, to the different types of enemy and enemy projectiles, to special effects like explosions, energy shields, and the spaceship's thrust.