Skip to main content

Microbit: Snap the Dot

Objective

Roboteers will code a gem game using sprite and accelerometers.

Essential Keywords

  • Inputs
  • Outputs
  • While Loops

Attention (5 Min.)

In this lesson we will develop a new game using buttons as inputs and LEDS as outputs!

Learners Guidance 

This game will be quite simple, but leaves space open for future improvements and additions. It will involve a sprite that moves back and forth across the screen. The goal of this game is to press a button when the LED lines up with a given target.

Make a sprite variable

Create a new variable called sprite. Drag a set sprite to into the on start on the workspace.

Create a sprite

Pull out a create sprite block and put it in set sprite to replacing the 0. A sprite is a single pixel that can move on the screen. It has an x and y position along with a direction of motion.

Move the dot

The sprite starts in the center facing right. Put a move block into the forever to make it move. Notice how it moves to the right but does not bounce back.

Bounce

Grab a if on edge, bounce block to make the sprite bounce on the side of the screen. Also, add a pause block to slow down the sprite.

Test and download

Use the simulator to find the best speed. If you have a micro:bit, press Download to try it out on the device.

Button handling

When A is pressed, we test if the sprite is in the center or not.

Use a on button pressed block to handle the A button. Put in a if block and test if x is equal to 2.

Score and game over

Finally, pull out an add score and a game over block to handle both success (sprite in the center) and failure (sprite not in the center).

Congrats! You have now built a working game!

The full code should look like this:

Explore (25 - 30 Min.)

Time to customize! See If you can add score variable that displays the score after you press the button at the correct time! Furthermore, try to add different types of goals that need you to press different buttons when the LED is over them!

4. Share (Min.)

Peer review!

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.

6. Evaluate

Students reflect on their performance. Give positive and constructive feedback on each student

Essential Questions 

What kind of input and outputs does this game use?

How could you make it so that the B button also works to click the sprite?