Space Cat is on a mission to collect as many crystals as possible to fuel his spaceship! Using the Scratch platform, you can build a fun collecting game to help Space Cat with its mission. Collecting is a popular mechanic in games and can be easily coded in Scratch with the same basic logic. From weapons to badges, collecting can keep players striving to achieve more in your game.
In this game, the goal is to collect as many crystals as possible, but if you miss three crystals the game ends. This tutorial shows you how to code this game from start to finish and you can customize it with your own images and messages.
Code this game and see how many crystals you can collect!
Play Flying Space Cat game.
Table of Contents
What you need:
Scratch account: Create a free Scratch account
Starter files: Download the image we are using in our game. You can also use your own images to customize your application. Download starter file.
No coding experience is necessary for this tutorial. Beginner-friendly for kids ages 8 and up. Give it a try!
Step 1: Create a new Scratch project
Let’s start by creating a new project.
- Visit https://scratch.mit.edu/
- Login to your account
- Click on the “Create” button.
Now, we can code your Flying Space Cat game!
Hint: You can also remix this project and others to see the code.
Step 2: Add a scrolling background
To give the effect of moving through space, we created a simple scrolling background.
- Start by deleting the Scratch cat sprite that appears in every new project. Click on the cat sprite and the trash can icon.
- Click the “Choose a Backdrop” icon and add the “Galaxy” backdrop.
The stars will move while the backdrop stays in place.
- Click upload sprite and upload the stars.png file
- Change the size of the Stars sprite to 50 and position it on the screen
- Add the “When Green Flag Clicked” block from the “Events” category
- Use a “Forever” block with the “Go to Back Layer” block to make sure the stars show behind other sprites.
- Then, use a “show” block to make the stars visible
- Position them with the “go to” block and use the “glide” block to move it left for 10 seconds.
- “Hide” the sprite when it reaches the edge of the screen and the forever block will start this animation over again.
Hint: Add more images or use a different image than stars.png to customize your scrolling background.
Step 3: Add flying cat and move it
Let’s position our flying cat and add the logic to move it up and down with arrows.
- Select “Choose a Sprite”, find the Cat flying sprite, and click to add it
- Use the “Go to” block to position the sprite at x= -150 y=0
- Add music with the “Forever” and “Play Sound Until Done” block. We’re using the Dance Magic sound which you can add in the Sounds tab.
Now, add logic to the arrow keys so our cat moves up and down when we click.
- Under events choose the “when key pressed” block and select “up arrow”
- Underneath use a “change y by” block and enter 5 to move up
- Go back to events, choose the “When key pressed” block and select “down arrow”
- Underneath use a “change y by” block and enter -5 to move down
Hint: Change the music by adding more sound options in the “Sounds” tab. Then select them in the “play sound” block.
Step 4: Show random crystals and move them
We want random crystals to appear on the right side of the screen and move toward the left.
- Select “Choose a Sprite”, find the Crystal sprite, and click to add it
- Add the “When Green Flag Clicked” and a “Forever” block underneath it to repeat the movement of the crystals
- Go to the “variables” blocks and “Make a variable”, name it Y-Position, and uncheck it
- Inside the “forever” block “show” the crystal and set it to a random position on the right side using “go to x” with x=240 and y=y-position
- Then, glide 2 seconds to x=-240 and y=y-position
- Use “set” y-position to a random number between 100 and -100, so it shows up in a different position next time
- “Hide” the crystal then wait a random amount of time between 1 and 2 seconds to repeat the loop
Hint: Select a different sprite or draw your own sprite to customize your collection item.
Step 5: Add up losing crystals
When a crystal flies by us and we don’t catch it, we want to count it. When we reach 3 crystals, the game is over. Let’s add on to the code you started in Step 4.
- Go to the “variables” blocks and “Make a variable”, name it Crystal, and uncheck it
- Under “when green flag clicked” set the Crystal variable to 0
- Create an “if” block with the condition “touching edge”.
- Inside this “if” block, change the crystal variable by 1. Add it under the “hide” block. Now each time it touches the edge, we add one.
- Add another “if” block inside the first “if” block. Use the condition “crystal = 3”.
- Then we will broadcast a new message named “Game Over”
- Add a “stop all” block to stop the game once the number of crystals reaches 3.
Hint: You can give different names to your variables, just make sure you are using the right variable in your code.
Step 6: Increase score for gaining crystals
Every time our flying cat touches a crystal, we want to increase our score. In addition to increasing the score, we will also decrease the count of the crystal variable. We do this because even though we are hiding the crystal, it still touches the edge each time.
- Go to the “variables” blocks and “Make a variable”, name it Score. Keep it checked so it shows on your game screen.
- Add the “When Green Flag Clicked” and set the Score to 0
- Next, add a “Forever” block underneath
Inside the forever block, we will add to the game score every time our cat touches a crystal.
- Use an “if” block with the condition “touching cat flying” sprite
- Add the “start sound” block and select “magic spell” for a sound effect
- Then change the score variable by 1 and the crystal variable by -1
- Hide the crystal once it’s collected
Step 7: Show “Game Over” screen
Your game is almost complete! Let the players know when the game is over by showing a game over screen. This will trigger when you’ve missed three crystals.
- Select “paint” under sprites and rename this sprite Game Over.
- In the costumes tab, select the rectangle tool and click and drag to draw a rectangle
- Then, use the type tool with font “marker” and add the message “Game Over”
- Adjust the message on your game screen
- Go back to the “Code” tab
- Add the “When Green Flag Clicked” and hide block. That means this message will not show when the game starts
- Add the “receive broadcast” block and select game over. This is the message we sent from the crystal sprite when the crystal variable reached 3.
- Then, add the “stop all” block to end the game.
Hint: You can change the font, colors, and text for your Game Over message in the “Costumes” tab.
Your Flying Space Cat game is complete!
Click the green flag and give it a try. If you want to let other people see your project and remix it, click the “Share” button at the top.
See complete Flying Space Cat game.
Download Free Printable Scratch Coding Tutorials PDF
Get the Rocketship Landing game and Flying Space Cat Scratch tutorials in a printable format.
More Scratch Tutorials for Kids
How To Make a Game on Scratch
Ready to level up your child’s learning experience? Try the best online coding classes for kids.