Micro:bit – Catch Game Part II

Uncategorized

The game should now look as follows, it is already fully playable:

181008_CatchGameFull1

Replacing the buttons with the accelerometer

An important remark first, do it like a pro …

When developming the program, do it like a pro. Professionals develop and test their software on simulators and with special test software. We do not have the later, but we have a simulator. You can check your changes immediatly when you launch your program on the simulator at the left. Do it!

First we want to replace the buttons with values supplied by the accelerometer. Then we only need to tilt the micro:bit from left to right or bottom to top so to control the movement of our sprite. This is much more convenient. Have a look again at the code we implemented last week. We need to replace the two blocks with “on button A/B pressed” by something new comprising out of a block to change the position of the sprite, getting the values of the accelerometer and these two embedded in a “forever” loop.

The values provided by the accelerometer vary between -1023 and +1023. That’s a lot, we only have 5 x 5 LEDS on our micro:bit. So if we do nothing, an even so small movement will always make our sprite move from one border to the other. So we need to transform this big range down to something feasable for our display. We do this by dividing the value coming from the accelerometer by a big number, matching the single moves we need to. Try to find a suitable value.

Another problem is the forever loop we will embed our acceleration block into. It is providing values many times per second, so when we move our micro:bit even a tiny bit, it will record many movements and each of them will relocate our sprite again to the border of the display. We can avoid this if we place a pause block after taking a value from the accelerometer. Find a suitable value again.

Try this out by yourself before looking at the solution. Then compare your solution with the one below.

181008 CatchGameAccelerometerBasis

Completing the Game

Now we can finish the game. What is still missing to get a real game?

Here are my suggestions:

  1. We need to allow the player to do the game many times. So each time we need to delete the old target and create a new target.
  2. Get a timer that counts down, so we have a fixed time to play.
  3. We need to count the points and at the end show the score.

The following program does that all. but I suggest that you try to figure out yourself what to do first. Most of the blocks you need, you can find in the category “Games”

181009_CatchGameFinal1

TIPP: This game may work great on the simulator, but depending on your choice of values for the pauses and the transformation of values coming from the accelerator, it may not run properly on the micro:bit. In case you have problems, adjust the values for pauses, and more important for the acceleratior. Then you will get the game running on the micro:bit.

micro:bit Curriculum: Variables

  • Why do we use data, in particular in science and mathematics?
  • What type of data do we have around us?
  • How do we track those data in real live? Which operations do we use in CS?
  • Difference between a constant and a variable
  • Which data types can constants and variable hold in Javascript? (numbers, booleans, text, objects and functions)
  • What operations do we have available to calculate with those variables? They are different depending on their type!

Let’s make a scorekeeper and then see how we can use variables to count values coming from our sensors on board of the micro:bit

Make a scorekeeper

Everything counts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.