Random Shooting Game with Scratch

Hitting targets from random places over a period of time is an event that we have encountered in many games. Here we will create the logic and algorithm of this event. And we can use this algorithm when necessary. In this article we will do the block-based encoding of the game with Scratch. In the next article we will do the same thing with the Python programming language. Scratch makes it easy to encode easily by creating a smooth algorithm without dealing with the language structure. But unfortunately we do not allow us to do detailed operations with other programming languages. For example, you cannot adjust the display and window settings as desired. Scratch is already a resource to teach algorithm development. To design broader programs it is necessary to learn languages ​​such as Python.

The scenario of the game: The enemy (cowboy), which we must shoot, appears on the screen at regular intervals and shoots at us after a while. Our goal is to shoot him before it hits us. Every time we get out of the screen in a random place and we have to hit it on time. If we do not shoot him, he will shoot us and  we will die when our health finish. When we hit him in time, our score is increasing and we will be not diminished. Lets design this game step by step.

Full of the game can be reached from the following link:

Step 1: Preparing the Target Sprite and Encoding:

First, open a blank project in Scratch and add a target pointer sprite as below. Target pointer will have two costumes,  first is normal view, second is fired view. You can draw this costumes manually, or you can download from internet.

After creating target pointer sprite, we can do the following encodings:

With these codes:

  • When the game starts (when the flag is clicked), we enable the target puppet to appear and follow the mouse pointer.
  • When the mouse is clicked, we enable the target puppet to switch to the firing costume and switch to the first view when we do not click.
  • When the game is over (we willl release it in other steps), we keep it hidden.

Stage 2: Preparing and Coding the Enemy Sprite

Like the target pointer, we will create an object that we have identified as an enemy sprite and it will have two costumes. First costume is normal position, second costume is dead position.

After creating the enemy sprite, create the variables fire, health, point, enemy_x, enemy_y. Just set the health and point variables to be visible. Then do the following encodings.

With these codes:

  • When the game starts (when the flag is clicked), we enable the enemy puppet to go through the normal clothing (costume1). Also we give the value of can variable 15. If you want the game to last longer or shorter, you can give other values ​​to the variable.
  • We also ensure that the score variable is reset at the beginning of each game.We use the ates variable to set the enemy timing for firing. 2 seconds after the display of the enemy character, the fire variable will be set to 1 and in this case the fire will be released. Fire news will also launch enemy fire animation.
  • If we can click on the enemy in the first 2 seconds after the enemy character appears (ie shoot), the ates variable will be set to 0 and therefore the fire will not be released, so the enemy will pass through the costume (costume2) before it can fire. The score will also increase. When the game is over, the enemy character will be hidden.

Step 3: Encode the enemys fire animation

 We create a fire sprite, to create the effect of firing and to make sure that your life is reduced. It will appear two seconds after the enemy appears. Whether you want to draw it yourself or download it from the internet. We created sprite with drawing. Then do the following encoding.

With these codes:

  • We hide enemys fire animation sprite named "Pat", when the flag is clicked and the game is over.
  • "Pat" sprite  will appear after enemys "enemy_fire" broadcast  and reduce health variable.
  • If health value becomes zero "game_over" broadcast will be released.

Step 4: Setting scenes and last encodings

Two scenes were used in our game. Lets create them as follows. We give the names 1 and 2 for the convenience of encoding these scenes.

Finally we will do last encodings at scene like follows:

With these codes:

  • When the game is started (when the flag is clicked), scene 1 appears and show the health and point variables.
  • When the game is over, game over scene appears and variables will be hidden. Also, if there are still running codes, we stop them all.


  • You can put factors such as level, duration, by improving the game instance. For example, as the level increases, more and more different types of enemies can emerge faster.
  • With various rewards and animations, you can make the game more beautiful. (For example, you can create an animations about the emergence of the enemys tables or objects placed at random places.
  • You can share your comments and more advanced versions of this game in the comments below.