Coding Activity #5
Learn how to draw different shapes and make lasers in our Space Invaders game!
Getting Started
Beginner Coding
Intermediate Coding
Advanced Coding
Activity #5 - Space Invaders – Fun with Functions
In this activity, you will learn how to draw different shapes and then use that knowledge to make lasers in our Space Invaders game.
- Ontario curriculum alignment: Grade 7
- Best for: Intermediate coders of all grade levels!
Before you begin ...
This hands-on activity uses a free coding platform called "Scratch". Please make sure you've watched our "Introduction to Scratch" video, and that you're signed into your Scratch account. If you need help, click below. Otherwise, skip down to STEP 1 to get started!
New to Scratch? Click here!
Already have a Scratch account? Click here!
Open the Scratch activity
Click the button below to open the activity. It will open in a new window/tab which allows you to keep this page open at the same time and easily switch between tabs while you work through this activity.
- After you open the activity, click on the blue "See inside" button to access the activity.
- If you are logged into your Scratch account, then you can click on the green "Remix" button to save this activity to your personal Scratch account so you can save your work and also share it with others!
Watch this video
Using variables and functions for more efficient code
Level 1
In this level, we will learn how to draw different shapes and then use that knowledge to make lasers in our Space Invaders game. As we work on making our code efficient, it’s important to make the code easy to change, easy to read, and easy to write. That is why concepts like variables and functions make your code better and efficient!
Let’s look at our Pencil Sprite, for example. When you press the ‘P’ button on your keyboard, you’ll notice that it draws a spiral! You’ll notice that the code for the Pencil is very long as it increases its “move steps” each time before it turns. To make our code efficient, we will use Variables together with Loops (or repeat blocks).
Best practices for variables
- Always tell the variables what they should start off as.
- Use the variable in the program.
- Tell how the variable value will change (increase or decrease).
Your Task
Your first task is to use the variable called “Spiral Distance” provided to move a certain distance. Select the Pencil Sprite, then within the loop provided – every single time you move – the Spiral Distance increases. You will use the “change Spiral Distance by ___” block to achieve this task.
The Goal
Make the provided code below much shorter by using variables and loops. Feel free to keep the spiral the same length or even larger – your choice!
When you’ve completed this level, press ‘L’ on your keyboard followed by ‘Y’ for yes to move on to the next level. Watch the next video below for the next level.
Watch this video
Code your own laser to move in a spiral!
Level 2
Now it’s your turn to code a laser to also move in a spiral! We have already got you started with some code, but you will be coding the shape of the laser using your knowledge of making spirals. Unlike the Pencil drawing of the spiral, our laser sprite code needs to include a small set of movements happening over a period of time.
For example, if you want your laser sprite to move 40 spaces, you will repeat a 4 space movement 10 times. That way the laser sprite will look like a smooth motion across your screen. You will need to adapt your spiral code a little bit with the laser sprite now instead of the pencil sprite.
Follow the instructions below to get started at this level.
- Use the 2 repeat blocks provided (one handling the linear motion of the laser, the other is handling the spiral patterns) to build the code of the spiral laser.
- Use the created pink “Hit Check” function block within your code to help you check if your laser actually touched an enemy in the game. Also, use the purple “Show” block to help your laser to keep showing up so you don’t lose sight of where the laser is.
- Connect your full code to the pink “Define B Laser” function block to see your laser now move in a spiral. To test your code, press the ‘B’ button on your keyboard to see your laser spiral move!
When you’ve completed this level, watch the next video below.
Watch this video
Create a more powerful laser
Level 3
It’s now time to create an even more powerful laser in the Space Invaders game! Imagine your laser spiral zig-zagging through the enemies in the game in one efficient path.
Using the provided code that moves a new laser in an “S” pattern, your task is to change some of the bugs in the code so that when you press the ‘X’ button on your keyboard, it should move in a specific pattern.
Hint: The distances of the laser’s movement are incorrect.
Follow the steps below to get started on this task.
- Use the variables provided (width and height) to make our code easier to change along the rest of the code. Change the values of the “set width to ___” and “set height to ___” variable blocks.
- Look for repeating sections within the code and separate them out from the ‘X’ laser code. Place them within the pink “define Horizontal Move” and “define Vertical Move” function blocks.
- To test your code, press the ‘X’ button on your keyboard and see if you can take out all the enemies by just one shot. Good luck!
Bonus Challenges - Try Them!
- Compare the time that it takes you to take out all the enemies with your ‘X’ laser shot with your classmates or friends. See who has the best time score!
- Use the ‘M’ and ‘Z’ keys on your keyboard to create other repeating type shapes of laser movement such as steps, squares and more! Use variables and functions to make your code efficient.
#207 - 8501 - 162nd Street
Surrey, British Columbia
Canada V4N 1B2