Arcade game
Electronic Arcade Games became popular in the 1970s - your parents and grandparents may have played one of the earliest ones called Space Invaders. Here is a video of it - it might seem rather funny now but in 1978 it really was good!
You, the defender, move along the bottom of the screen. You can fire missiles to an alien that moves along the top of the screen. The alien is dropping bombs on you. If you hit the alien then you score points, and if you get hit by a bomb you lose a life!
You know that the display on the microbit is quite small - there are only 5 LEDs across and 5 LEDs down - but we can still make quite a good game.
This project will show you how to build up the game. There are some challenges for you too - can you add sound effects and scoring?
The alien
We will start by coding the alien. It is going to move across the top of the screen and back.Make a new project on https://makecode.microbit.org/ then add these code blocks:
The green blocks use a new library which you will find under the Advanced heading.
A sprite is just an LED on the micro:bit, but on a computer with a bigger display, a sprite would be a more complex object like this.
Here is what the code does.
When we start the micro:bit we:
create the sprite called alien, at x=0, and y=0 (that’s the top left of the display), and
- Set a time delay called alienDelay to be 1 second (but we use the value in milliseconds, and 1 second is 1000 milliseconds).
- Wait for alienDelay
- Then move the alien by 1
- If the alien reaches the edge of the display, then bounce (change direction)
Add the bombs
Now let's add the bombs! The bomb gets dropped by the alien randomly, and it falls down the screen.First, we add another variable called bombDelay to the on start block:
Then we add a bomb sprite to the forever block:
In the code we have a random block, so that sometimes we drop a bomb. When we do that, we set the bomb to blink then move it down the display in 4 steps. When it reaches the bottom we delete the bomb.
Here’s what you might see on the simulator - a bomb dropping from the alien at the top of the screen.
Now the defender!
The defender sprite moves one step along the bottom row of the display when you press button A. When you press button B a missile sprite is launched. Here’s the code for adding the two sprites. First, the on start block:Next, we need two code blocks, one for moving the defender when we press button A:
The other code block is for firing the missile when we press button B:
The slightly tricky thing here is to make sure the missile sprite moves up the display. To do this, the code changes the sprite’s direction by 270 degrees. Then we move it in 4 steps.
So far so good - you should test the code and make sure the defender and missile sprites work.
But we need to know if we have hit the alien! We can do this by using the “is missile touching alien” block.
My code above shows an “explosion” icon on the display for a short time.
We should also detect if the defender gets hit. This is a similar block, but it needs to go in the forever block.
Your challenges!
There are lots of things you could do to improve the game - here are some suggestions:- The alien could move at random times - hint, use the pick random code block (in Math) in the first line of the forever block.
- You could add sounds - for example when a bomb hits the defender. You can play around in makecode to get game sounds - try this:
- The defender could move left or right - using button A for left and B for right. You still need to launch your missile somehow, so you could use the “on button A+B pressed” block.
- Or you could use the tilt measurement in the microbit to launch the missile - see the Spirit Level project.
- You can easily add scoring, for when your missile hits the alien. The Game code blocks include set score and change score code.
- Similarly, you can add lives, so you could lose a life when the defender gets hit. Have a look at the set, add and remove life codes.
Comments
Post a Comment