Space Game Step-by-step

Jump into scratch and join us in making a Space shooter game!

To start off, we’ll need to open a new scratch project, we can do this by going to the following webpage: 

Next we’ll want to remove the cat sprite that scratch gives us, do this by clicking on the bin icon on the sprite 

Now we can begin to add the assets we’ll be using for our game. Starting with the background, for mine I’ll be using the background “Stars” but you can use any that you like. Add your background by clicking on the circle in the bottom right corner of your screen and finding one that you like. 

Next we’ll add the buttons and text we’ll need for our menu screen. To add these click the circle with a cat face in it next to the button we previously pressed. For now we’ll just add a simple button. 

We now need to edit the button to show that it starts the game do this by clicking on the button in the sprite list on the right, then going to the costumes tab in the top left 

Add text to the costume using the option 

Now we can begin to add code to this button. 

Add , and . Change “message1” to “Start”.

This will trigger the rest of the game to load in and for the game to begin. 

To make sure the Start button is there after playing a game, we’ll add a and a below it. The code for the button should look like this 

Player (Ship)

Now we need a sprite for the player to control, once again go to the cat button in the bottom right corner and this time add something to use as the player. For mine I’ll be using a dragonfly as it reminds me of a sci-fi spaceship.

We’ll need to resize and potentially rotate the sprite, I set the size of my ship to 25 and rotated it so that it was facing right. 

Now for the code. We begin by making sure the ship doesn’t show up on the start screen by adding a and a beneath it.

Next add , and . Change “x:” to -200 and “y:” to 0. This will make sure the player starts where we want them to when the start button is pressed. We’ll next need to let the player control the sprite.

We can do this by adding three , one for moving up, another for moving down and the final one for firing lasers. Change ‘space’ to any button on the keyboard that you like, I’ll be using ‘W’ for up, ‘S’ for down and ‘space’ for fire. The up and down arrows are a good alternative.

A quick way of getting the ship to move would be to just add to each the up and down commands, this will look something like the image to the right.

However doing this will make the game feel clunky to play. This is because scratch will only allow one button to be pressed at a time, meaning the ship won’t be able to move and fire at the same time. To get around this, we can use to run a loop until the button is released. 

We start this block of code by adding a under the , then adding a in the middle of the loop. This will repeat the motion until something happens. Now we’ll add into the diamond box of ‘repeat until’ like this and finally into the box of ‘not’.  The final code block will look something like this.

This works by looping the movement until the code detects that ‘W’ is no longer pressed. 

We can copy this block for the other direction of movement, and simply change ‘change y by’ to -10.

The other we added it for later, so we’ll ignore it for now. The code for the ship should look something like this at the moment. 


Now we can start to make the lasers, go to the cat button in the bottom right corner of your screen and find something suitable for a laser. I’ll be using the ‘Sun’ sprite and setting its size to 10. I’ll name this laser to make finding it easier later on. Just like the ship, we’ll make sure it doesn’t appear on the title screen by adding a and a .

We’ll need to be able to fire multiple lasers to make the game fair, so instead of putting our instructions on a we’ll use . This lets us copy our laser sprite as many times as we need, if we used then we would have to either wait for the laser to reach the edge of the screen before using it again, or firing would move our laser back to the ship every time it was pressed making it hard to hit any targets.

Under we need to add a, , and a within it, this ensures it keeps moving across the screen. Set the to ‘ship’ and to 25. 

Now we’ll make sure we can remove the laser when it has reached the end of the screen, or hit a target. We need to do this to make sure we don’t load too many sprites into the game and cause it to slow down or break. We can do this by adding another and this time placing a below that. This will hold our instructions until a certain event happens. The events we need are when touching a target, or touching the side of the screen, so we need to add a to allow us to put both in. Fill both spaces with a and set the first to ‘edge’, the second one we’ll need to come back to later. Below that add a and set the time to 0.01, this is just to make sure the laser doesn’t disappear before the target does, and finally add to the bottom. The code for the laser should look something like this at this stage

Now we can go back to the ship and add code to the block we left blank earlier. Copy the same repeat until block from the other key press blocks but instead of we want to add and change ‘myself’ to ‘laser’ and also add and change the time to 0.5. This is just to add some difficulty to the game, and let us add power ups as the game goes on.


We can finally add targets for the player to shoot. Once again click the cat button in the bottom right corner, and find something you like. In my game I’ll be using a frog. Make sure the sprite you choose is facing left by changing its direction. You may need to use the rotation or flip options to get it to work. Finally name it something appropriate so you can use it later in the code for the laser, I’ll name mine target.

Now we can get to coding our target, we want more than one to appear so we’ll be using and again. We don’t want them all to start at the same place, so we’ll be adding a . Change ‘x’ to 250, and in the box for ‘y’ add a Change the values in these boxes to ‘-140’ and ‘140’, this is what makes sure the sprite starts on the right side of the screen, but in a different place each time. Under that add a and a same as we did for the laser. But this time set the ‘change x by’ to -10, this will make the target move towards the player. The last thing we need to add to the target for now is instructions for reaching the left side of the screen, or getting hit by a laser. Starting with when the target is hit by the laser we’ll add another but below this one we’ll add and , then change the ‘mouse pointer’ to ‘laser’. Next we need to add a variable to keep track of our score. Go to variables and click , name this variable ‘score’ and make it available to all sprites, do this again for a variable named ‘health’, we’ll be using that in the next bit. Now add and select the ‘score’ variable. Finally we need to make sure we remove the target, we do this the same as we did with the laser with a

We can duplicate this entire bit of code by right clicking on the top block, this will save us a lot of time as it’s almost exactly the same for if the target hits the side. We need to swap the out for a and add a to the left box, and change the right box to ‘-230’, then change ‘score’ to ‘health’ in the and the logic is done. We now just need to make sure that it doesn’t appear on the title screen, and add code to make the targets spawn. Same as for the bullet, add and a .

The code for making them spawn needs to start when the game starts, so we’ll add a , under that we’ll make sure the targets stop appearing when we run out of lives with a and we’ll add a and put the variable ‘health’ in the left box, as well as set the right box to 0, this will look like this . Inside the loop add and . The last thing to do here is deciding how hard we’ll make the game. I’ll use a and change the left box to ‘0.2’ and the right box to ‘1.5’ but you can choose anything you’d like. Choosing 0 will make the game basically impossible without changing the wait time in the laser, but see how long you can last. 

The last few things we need to clean up are our starting score and health, as well as the hit detection on the laser. Click on the laser to view its code again, and where we left the second ‘touching mouse pointer’ change that to ‘touching target’. For our starting health and score we need to go back to the start button. Under but above add two , change one to health and the other to score. On the one labelled health change the ‘0’ to ‘10’ and we’re done! Later we’ll look at how to add power ups and other features you’d like but for now enjoy your game. Completed code for each sprite is below for you to compare with if anything goes wrong.

Start button: 




Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.