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.

main.cpp

Committer:
AnnaBridge
Date:
2016-04-13
Revision:
0:c0ff9a7daaac
Child:
2:ab61446893c8

File content as of revision 0:c0ff9a7daaac:

/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/* The letter game....

   This simple game demonstrates the use of:
   1) The random function
   2) Events on the message bus to handle button presses
   3) Driving the display for both characters, numbers and animations.
   4) The system timer
   
   The start of the game will be preceeded by the following message:
   "Get Ready.."
   and then a small animation showing shrinking 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.
   
   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.
    
*/

#include "MicroBit.h"
#include <stdio.h>

void level_up();
void start();
void game_over();
void display_letter();
void check_press(unsigned long delay);
void onButtonA(MicroBitEvent e);
void onButtonB(MicroBitEvent e);


MicroBit uBit;

#define INITIAL_LIMIT  2000 //ms
#define SPEEDUP         200 //ms
#define LEVEL_UP         20  // level/speed up each time score accumulates by this amount

typedef enum 
{
    NONE,
    VOWEL,
    CONSONANT,
    LATE_PRESS
} Letter_type;

Letter_type buttonPress = NONE;
Letter_type type = NONE;
unsigned long time_val;
unsigned long limit;
unsigned score = 0;

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");
    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");
    uBit.display.print(inner, 0, 0); 
    uBit.sleep(200);
    uBit.display.print(middle, 0, 0);
    uBit.sleep(200);
    uBit.display.print(outer, 0, 0);    
    uBit.sleep(200);
    uBit.display.clear();
    uBit.sleep(300);
}

void start()
{
    score = 0;
    limit = INITIAL_LIMIT;
    uBit.display.clear();
    uBit.display.scroll("GET READY...", 100);
    uBit.sleep(500);
    level_up();
    uBit.sleep(500);
    display_letter();
}

void game_over()
{
    char val[5];
    uBit.display.clear();
    uBit.display.scroll("GAME OVER!", 100);
    uBit.display.scroll("SCORE = ");
    sprintf(val,"%u", score);
    uBit.display.scroll(val, 100);
}


void display_letter()
{
    // random number from 65 - 90 ie ASCII A-Z
    char letter = uBit.random(26) + 65;
    
    switch(letter)
    {
       case 'A':
       case 'E':
       case 'I':
       case 'O':
       case 'U': 
          type = VOWEL;
          break;
          
       default:
           type = CONSONANT;
    }

    uBit.display.printChar(letter,500);
    //uBit.sleep(limit);
    uBit.display.clear();
    time_val = uBit.systemTime();
}

void check_press(unsigned long delay)
{
    if (buttonPress == type && delay <= limit)
    {
        score++;            
        if ((score % LEVEL_UP) == 0 && limit > SPEEDUP)
        {
            // Increase speed
            limit -= SPEEDUP;    
            level_up();
        }
        buttonPress = NONE;
        display_letter();
    }
    else
    {
        game_over();            
        uBit.sleep(1000);
        start();
    }
    
}   

void onButtonA(MicroBitEvent e)
{
    if (e.value == MICROBIT_BUTTON_EVT_CLICK)
    {
        unsigned long delay = uBit.systemTime() - time_val;
        buttonPress = VOWEL;
        check_press(delay);
    }
}

void onButtonB(MicroBitEvent e)
{
    if (e.value == MICROBIT_BUTTON_EVT_CLICK)
    {
        unsigned long delay = uBit.systemTime() - time_val;
        buttonPress = CONSONANT;
        check_press(delay);
    }
 
}


int main()
{
    
    // Initialise the micro:bit runtime.
    uBit.init();
    
    // Set up button event handlers
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButtonB);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA);
    
    start();
    while(1)
    {            
        uBit.sleep(100);
    }

    // If main exits, there may still be other fibers running or registered event handlers etc.
    // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
    // sit in the idle task forever, in a power efficient sleep.
    release_fiber();
}