micro:bit / microbit-letter-game

Dependencies:   microbit

Fork of microbit-letter-game by Anna Bridge

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;
+    } 
 }