Simple letter game testing dexterity and using several features of the microbit, such as button events, the display and the system timer

Dependencies:   microbit

Fork of microbit-letter-game by Anna Bridge

Simple letter game

Overview

This is a simple game which demonstrates a number of built in microbit features and some general programming ideas.

The basic idea of the game is to test hand eye coordination based on visual stimulus. After a start up sequence, a random letter from A-Z is displayed on the microbit display. The player must then determine if the letter is a vowel or a consonant and press an associated button accordingly (left for vowel, right for consonant). There is a pre-determined time limit during which the correct button must be pressed. Each time the player reaches the next level this time limit reduces. The player scores 1 point for each correct press. Once an incorrect button is pressed or the time limit exceeded, the game is over and the players score is displayed.

Start up sequence

The start up sequence consists of a scrolling message: "GET READY ...", followed by an animated shrinking then expanding box.

Levelling up

The next level is reached when the player scores a predetermined number of points. To indicate a levelling up the shrinking and expanding box animation is displayed.

Game over

Once the game is over , a scrolling message: "GAME OVER. SCORE = " is displayed.

Game mechanics

There were a number of areas to be considered when designing the game,

  • How to display a random letter
  • How to get a button press and disable presses at certain times
  • How to measure a response time
  • How to write text to the display
  • How to animate a picture on the display
  • How to allow easy configuration of difficulty level.

Random letters

The microbit has a function to print a single character:

    uBit.display.printChar(character)

Character can be a single quoted value, e.g. 'A' or the ASCII code of a character, e.g. 65 (ASCII code for 'A').

Thus to display a random character in the range A-Z we can generate a random number in the range 65 - 90 and then call the above function with that value.

The microbit has a random number generator:

uBit.random(x)

This produces a random number in the range 0 to x. However we want a value in the range 65 to 90 , thus:

char letter = uBit.random(26) + 65;
uBit.display.printChar(letter);

Getting button presses

There are a couple of ways of getting button presses:

  • Checking for a button press synchronously
  • Using the Message bus to asynchronously identify button presses

There are 3 button types

  1. buttonA (left button)
  2. buttonB (right button)
  3. buttonAB (both buttons at the same time)

Synchronous button presses

This can be achieved by calling the function

ubit.buttonA.isPressed()

This will check if button A is currently being pressed. This is not a very practical way of checking for presses as it would involve writing a blocking loop which waits for one of the buttons to be pressed. E.g.

While (!ubit.buttonA.isPressed() && !ubit.buttonB.isPressed()
{
    ubit.sleep(100);   // put the microbit to sleep for 100 milliseconds
}

Asynchronous button presses

This is a better programming model approach. This allows the microbit operating system to worry about whether a button has been pressed or not. All we have to do is write a function that we want to be called if a button is pressed and tell the operating system to call that. On the microbit this is done by using what is called the 'messageBus'. E.g.

uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA);

This tells the operating system to look for any events (MICROBIT_EVT_ANY) coming from the peripheral identified by MICROBIT_ID_BUTTON_A and if seen to call the function onButtonA. This is called registering onButtonA as an interrupt service routine (ISR). When any event is detected as originating from button A (this could be a press, a hold, a double press etc) the current piece of code being executed is interrupted and onButtonA is called. Once onButtonA has finished, the original piece of code will continue running from the point at which it was interrupted.

Download repository: zip gz

Files at revision 2:ab61446893c8

Name Size Actions
[up]
main.cpp 6646 Revisions Annotate
microbit.lib 71 Revisions Annotate