simon says

Dependencies:   4DGL-uLCD-SE Joystick SDFileSystem mbed wave_player

Files at this revision

API Documentation at this revision

Comitter:
elirobelo
Date:
Mon Mar 13 22:07:26 2017 +0000
Parent:
0:aa7531dac907
Commit message:
Final Simon Says

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Mar 09 21:53:59 2017 +0000
+++ b/main.cpp	Mon Mar 13 22:07:26 2017 +0000
@@ -3,66 +3,195 @@
 #include "uLCD_4DGL.h"
 #include "wave_player.h"
 #include "Joystick.h"
-
-
-Nav_Switch myNav( p9, p6, p7, p5, p8);
+ 
+ 
+Nav_Switch myNav( p25, p22, p23, p21, p24); // U, D, L, R, Fire
 uLCD_4DGL uLCD(p28, p27, p29);
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
-
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+ 
 int arrowVec[20];
-
-
 int arrow;
-
+int correct;
+ 
 void mainMenu() {
     uLCD.baudrate(BAUD_3000000);
     while(1){
-        uLCD.locate(3,2);
-        uLCD.puts("Simon Says");
-        uLCD.locate(3,6);
+        uLCD.text_string("Simon Says", 4, 3, FONT_12X16, BLUE);
+        uLCD.locate(6,8);
         uLCD.puts("Start");
         if(myNav.fire()) {
             uLCD.cls();
             return;
         }
-}
-}
-void createArrows(int lev) {
-
-    for (int i = 0; i < lev; i++) {
-        if (arrowVec[i] == 1) {
-            uLCD.triangle(15, 50, 35, 45, 35, 75, RED);
-            uLCD.rectangle(35,50,85,70,RED);
-        } else if (arrowVec[i] == 2) {
-            //uLCD.triangle(int x1, int y1 , int x2, int y2, int x3, int y3, RED);
-            uLCD.rectangle(35,50,85,70,BLUE);     
-        } else if (arrowVec[i] == 3) {
-            //uLCD.triangle();
-            uLCD.rectangle(35,50,85,70,GREEN);
-        } else {
-            uLCD.rectangle(35,50,85,70,WHITE);
-        }
     }
 }
 
+void createArrows(int lev) {
+ 
+    for (int i = 0; i < lev; i++) {
+        if (arrowVec[i] == 1) {
+            uLCD.cls();
+            uLCD.printf("     Level %d", lev); 
+            uLCD.text_string("LEFT", 7, 14, FONT_12X16, RED);
+            uLCD.triangle(25, 60, 55, 40, 55, 80, RED);
+            uLCD.line(55, 50 , 55, 70, BLACK);
+            uLCD.rectangle(55,50,95,70,RED);
+            uLCD.line(55, 50 , 55, 70, BLACK);
+            //wait(.2);
+        } else if (arrowVec[i] == 2) {
+            uLCD.cls();
+            uLCD.printf("     Level %d", lev); 
+            uLCD.text_string("RIGHT", 6, 14, FONT_12X16, BLUE);
+            uLCD.triangle(95, 60, 65, 40, 65, 80, BLUE);
+            uLCD.line(65, 50 , 65, 70, BLACK);
+            uLCD.rectangle(25,50,65,70,BLUE); 
+            uLCD.line(65, 50 , 65, 70, BLACK);  
+        } else if (arrowVec[i] == 3) {
+            uLCD.cls();
+            uLCD.printf("     Level %d", lev); 
+            uLCD.text_string("UP", 8, 14, FONT_12X16, GREEN);
+            uLCD.triangle(44, 55, 84, 55, 64, 25, GREEN);
+            uLCD.line(54, 55 , 74, 55, BLACK);
+            uLCD.rectangle(54, 55, 74, 95,GREEN);
+            uLCD.line(54, 55 , 74, 55, BLACK);
+        } else {
+            uLCD.cls();
+            uLCD.printf("     Level %d", lev); 
+            uLCD.text_string("DOWN", 7, 14, FONT_12X16, WHITE);
+            uLCD.triangle(44, 65, 84, 65, 64, 95, WHITE);
+            uLCD.line(54, 65, 74, 65, BLACK);
+            uLCD.rectangle(54, 25, 74, 65,WHITE);
+            uLCD.line(54, 65, 74, 65, BLACK);
+        }
+        wait(0.5);
+    }
+}
 
+void beep() {
+    FILE *wave_file;
+    wave_file=fopen("/sd/wavfiles/chime.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file); 
+ }
+ 
+void buzz() {
+    FILE *wave_file;
+    wave_file=fopen("/sd/wavfiles/buzzer.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file); 
+}
+ 
+ 
+void inputScreen() {
+    uLCD.text_string("Input your answer", 1, 1, FONT_12X16, WHITE);
+}
+
+void endGame(int level) {
+        uLCD.cls();
+        uLCD.text_string("Game Over", 5, 1, FONT_12X16, BLUE);
+        uLCD.locate(1,5);
+        uLCD.puts("You got to level");
+        uLCD.locate(8,7);
+        uLCD.printf("%d", level);
+        uLCD.text_string("Fire to Restart", 2, 11, FONT_12X16, GREEN);
+        while(1) {
+            if(myNav.fire()) {
+                uLCD.cls();
+                return;
+            }
+        } 
+}
+ 
 int main() {
-    bool game = true;
+      bool game = true;
     int level = 0;
     //int arrowVec[20]; 
-    //void mainMenu();
+    mainMenu();
     
     
     while(game) {
         level++;
-//        for (int i = 0; i < level; i++) { 
-//            arrow = (rand() % 4) + 1;
-//            arrowVec[i] = arrow;
-//        }
-        arrowVec[0] = 1;
+        
+        for (int i = 0; i < level; i++) {
+            arrow = (rand() % 4) + 1;
+            arrowVec[i] = arrow;
+        }
+        
         createArrows(level);
-        game = false;    
+        uLCD.cls();
+        inputScreen();
+        
+        for (int j = 0; j < level; j++) {
+            int decision = 0;
+            while (decision == 0) {
+                if(myNav.fire()){
+                }
+                else if(myNav.left()) {
+                    decision = 1;
+                    if(arrowVec[j]==1) {
+                        correct = 1;
+                        beep();
+                    } else {
+                        correct = 2; // Game should end
+                        buzz();
+                    }
+                }
+
+                else if(myNav.right()) {
+                    decision = 1;
+                    if(arrowVec[j]==2) {
+                        correct = 1;
+                        beep();
+                    } else {
+                        correct = 2;
+                        buzz();
+                    }
+                }
+
+
+                else if(myNav.up()) {
+                    decision = 1;
+                    if(arrowVec[j]==3) {
+                        correct = 1;
+                        beep();
+                    } else {
+                        correct = 2;
+                        buzz();
+                    }
+                }
+
+
+                else if(myNav.down()) {
+                    decision = 1;
+                    if(arrowVec[j]==4) {
+                        correct = 1;
+                        beep();
+                    } else {
+                        correct = 2;
+                        buzz();
+                    }
+                }
+
+                if (correct==2){
+                    // end game
+                    game = false;
+                    endGame(level);
+                    game = true;
+                    level = 0;
+                    correct = 0;
+                }
+            }
+        }
+        if(level==21){
+            game = false;
+            endGame(level);
+            level = 0;
+            game = true;
+            correct = 0;
+        }    
     }
     
 
-}
+}
\ No newline at end of file