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.

Files at this revision

API Documentation at this revision

Comitter:
AnnaBridge
Date:
Wed Apr 13 17:21:22 2016 +0000
Parent:
1:4d2aaad38c09
Commit message:
Added state checking to effectively disable button presses until we are ready for them

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 4d2aaad38c09 -r ab61446893c8 main.cpp
--- a/main.cpp	Wed Apr 13 16:28:08 2016 +0000
+++ b/main.cpp	Wed Apr 13 17:21:22 2016 +0000
@@ -29,17 +29,18 @@
    
    The start of the game will be preceeded by the following message:
    "Get Ready.."
-   and then a small animation showing shrinking boxes.
+   and then a small animation showing shrinking/expanding boxes.
    Once the game starts a random letter from A-Z will be displayed on the screen.
    If it is a consonant then you press the right button.
    If it is a vowel then you press the left button.
    You must press the corresponding button within the time period for the current level.
-   Each time you level up, the time to respond decreases. When you level up the shrinking
-   box animation will be displayed again.
+   Each time you level up, the time to respond decreases. When you level up the shrinking/
+   expanding box animation will be displayed again.
    
    The initial allowed time is defined as INITIAL_LIMIT.
    The amount by which the time allowed decreases at each level is defined by SPEEDUP.
    The point at which a level up occurs is defined as LEVEL_UP.
+   These can all be tweaked by updating the #defines.
     
 */
 
@@ -69,18 +70,25 @@
     LATE_PRESS
 } Letter_type;
 
+typedef enum
+{
+    BUTTONS_ENABLED,
+    BUTTONS_DISABLED
+} Button_state;
+
 Letter_type buttonPress = NONE;
 Letter_type type = NONE;
 unsigned long time_val;
 unsigned long limit;
 unsigned score = 0;
+Button_state b_state = BUTTONS_DISABLED;
 
 void level_up()
 {
     MicroBitImage outer("255,255,255,255,255\n255,0,0,0,255\n255,0,0,0,255\n255,0,0,0,255\n255,255,255,255,255\n");
     uBit.display.print(outer, 0, 0);
     uBit.sleep(200);
-    MicroBitImage middle("0,0,0,0,0\n0,255,255,255,0\n0,255,0,255,0\n0,255,0,255,0\n0,0,0,0,0\n");
+    MicroBitImage middle("0,0,0,0,0\n0,255,255,255,0\n0,255,0,255,0\n0,255,255,255,0\n0,0,0,0,0\n");
     uBit.display.print(middle, 0, 0);
     uBit.sleep(200);
     MicroBitImage inner("0,0,0,0,0\n0,0,0,0,0\n0,0,255,0,0\n0,0,0,0,0\n0,0,0,0,0\n");
@@ -104,6 +112,7 @@
     level_up();
     uBit.sleep(500);
     display_letter();
+    b_state = BUTTONS_ENABLED;
 }
 
 void game_over()
@@ -152,6 +161,7 @@
             // Increase speed
             limit -= SPEEDUP;    
             level_up();
+            uBit.sleep(500);
         }
         buttonPress = NONE;
         display_letter();
@@ -161,29 +171,38 @@
         game_over();            
         uBit.sleep(1000);
         start();
-    }
-    
+    }    
 }   
 
 void onButtonA(MicroBitEvent e)
 {
-    if (e.value == MICROBIT_BUTTON_EVT_CLICK)
+    if (b_state == BUTTONS_ENABLED)
     {
-        unsigned long delay = uBit.systemTime() - time_val;
-        buttonPress = VOWEL;
-        check_press(delay);
+        b_state = BUTTONS_DISABLED;
+        if (e.value == MICROBIT_BUTTON_EVT_CLICK)
+        {
+            unsigned long delay = uBit.systemTime() - time_val;
+            buttonPress = VOWEL;
+            check_press(delay);
+        }
+        
+        b_state = BUTTONS_ENABLED;
     }
 }
 
 void onButtonB(MicroBitEvent e)
 {
-    if (e.value == MICROBIT_BUTTON_EVT_CLICK)
+    if (b_state == BUTTONS_ENABLED)
     {
-        unsigned long delay = uBit.systemTime() - time_val;
-        buttonPress = CONSONANT;
-        check_press(delay);
-    }
- 
+        b_state = BUTTONS_DISABLED;
+        if (e.value == MICROBIT_BUTTON_EVT_CLICK)
+        {
+            unsigned long delay = uBit.systemTime() - time_val;
+            buttonPress = CONSONANT;
+            check_press(delay);
+        }
+        b_state = BUTTONS_ENABLED;
+    } 
 }