A game using accelerometer to move a sprite that needs to catch a gem to score points.
Micro Bit: Gem Game
Roboteers will code a gem game using sprite and accelerometers.
Attention (5 Min.)
In this lesson we will develop a new game using accelerometer!
This game will be quite simple, but leaves space open for future improvements and additions. It will involve a player sprite that will move in the direction that the Microbit is tilted towards. The goal of this game is to move towards and collect gems. Once a gem is collected, a new one will spawn in a random location on the screen.
Part 1: Introduction and Loading Screen
Let's begin by creating a landing animation when the game starts up. This will make it so that the player has time to prepare for the game.
This will give a player a nice countdown to prepare for the game.
Next, let's create the player sprite after the countdown ends. This can be done by setting variables as sprites as you previously learned how to do. We can just add these to the end of our countdown code.
Similarly, we create a sprite for the gem.
Part 2: Moving the Player Sprite
Next, we need to make it so that the the sprite moves when the MicroBit is tilted. We do this inside of a forever loop since we always want the player to be able to control the sprite. Gravity is a force that is always acting down towards the floor. If you imagine hanging a bob from the center of the microbit and tilting the microbit, the angle that the string forms with the board will change. Thus, the force that the microbit experiences due to gravity also changes. This change in force is measured by the accelerometer and is called acceleration. We can use this acceleration to know how the microbit is tilted and move the character in that direction.
Part 3: Collecting Gems
The next step is to check if the player sprite and the gem are occupying the same space on the grid. We would call this collecting a gem. We can do this by using the "is touching" block inside of an if statement. If this is true, then we can play a simple animation, delete the gem, and generate a new gem at a random location. This code is added to the bottom of the player motion code since we always want to be checking if the player is touching a gem.
Congrats! You have now built a working game!
Explore (25 - 30 Min.)
Time to customize! See If you can add score variable that displays the score after you collect a gem! Furthermore, try to add obstacles or danger led's that make you lose the game if touched!
4. Share (Min.)
Students can share their work with peers for constructive feedback and then use this feedback to revise and improve their work.
5. Closure (Min.)
Review content and summarize key points.
Students reflect on their performance. Give positive and constructive feedback on each student
How does the game know which way the microbit is tilted?
Why is some code within a forever loop?
Alternitavely, follow this video guide!
Part 1: Moving the Player
Part 2 Chasing Gems
Complete Game Here: