simon says

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

Committer:
elirobelo
Date:
Mon Mar 13 22:07:26 2017 +0000
Revision:
1:6a079fb5542e
Parent:
0:aa7531dac907
Final Simon Says

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elirobelo 0:aa7531dac907 1 #include "mbed.h"
elirobelo 0:aa7531dac907 2 #include "SDFileSystem.h"
elirobelo 0:aa7531dac907 3 #include "uLCD_4DGL.h"
elirobelo 0:aa7531dac907 4 #include "wave_player.h"
elirobelo 0:aa7531dac907 5 #include "Joystick.h"
elirobelo 1:6a079fb5542e 6
elirobelo 1:6a079fb5542e 7
elirobelo 1:6a079fb5542e 8 Nav_Switch myNav( p25, p22, p23, p21, p24); // U, D, L, R, Fire
elirobelo 0:aa7531dac907 9 uLCD_4DGL uLCD(p28, p27, p29);
elirobelo 0:aa7531dac907 10 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
elirobelo 1:6a079fb5542e 11 AnalogOut DACout(p18);
elirobelo 1:6a079fb5542e 12 wave_player waver(&DACout);
elirobelo 1:6a079fb5542e 13
elirobelo 0:aa7531dac907 14 int arrowVec[20];
elirobelo 0:aa7531dac907 15 int arrow;
elirobelo 1:6a079fb5542e 16 int correct;
elirobelo 1:6a079fb5542e 17
elirobelo 0:aa7531dac907 18 void mainMenu() {
elirobelo 0:aa7531dac907 19 uLCD.baudrate(BAUD_3000000);
elirobelo 0:aa7531dac907 20 while(1){
elirobelo 1:6a079fb5542e 21 uLCD.text_string("Simon Says", 4, 3, FONT_12X16, BLUE);
elirobelo 1:6a079fb5542e 22 uLCD.locate(6,8);
elirobelo 0:aa7531dac907 23 uLCD.puts("Start");
elirobelo 0:aa7531dac907 24 if(myNav.fire()) {
elirobelo 0:aa7531dac907 25 uLCD.cls();
elirobelo 0:aa7531dac907 26 return;
elirobelo 0:aa7531dac907 27 }
elirobelo 0:aa7531dac907 28 }
elirobelo 0:aa7531dac907 29 }
elirobelo 0:aa7531dac907 30
elirobelo 1:6a079fb5542e 31 void createArrows(int lev) {
elirobelo 1:6a079fb5542e 32
elirobelo 1:6a079fb5542e 33 for (int i = 0; i < lev; i++) {
elirobelo 1:6a079fb5542e 34 if (arrowVec[i] == 1) {
elirobelo 1:6a079fb5542e 35 uLCD.cls();
elirobelo 1:6a079fb5542e 36 uLCD.printf(" Level %d", lev);
elirobelo 1:6a079fb5542e 37 uLCD.text_string("LEFT", 7, 14, FONT_12X16, RED);
elirobelo 1:6a079fb5542e 38 uLCD.triangle(25, 60, 55, 40, 55, 80, RED);
elirobelo 1:6a079fb5542e 39 uLCD.line(55, 50 , 55, 70, BLACK);
elirobelo 1:6a079fb5542e 40 uLCD.rectangle(55,50,95,70,RED);
elirobelo 1:6a079fb5542e 41 uLCD.line(55, 50 , 55, 70, BLACK);
elirobelo 1:6a079fb5542e 42 //wait(.2);
elirobelo 1:6a079fb5542e 43 } else if (arrowVec[i] == 2) {
elirobelo 1:6a079fb5542e 44 uLCD.cls();
elirobelo 1:6a079fb5542e 45 uLCD.printf(" Level %d", lev);
elirobelo 1:6a079fb5542e 46 uLCD.text_string("RIGHT", 6, 14, FONT_12X16, BLUE);
elirobelo 1:6a079fb5542e 47 uLCD.triangle(95, 60, 65, 40, 65, 80, BLUE);
elirobelo 1:6a079fb5542e 48 uLCD.line(65, 50 , 65, 70, BLACK);
elirobelo 1:6a079fb5542e 49 uLCD.rectangle(25,50,65,70,BLUE);
elirobelo 1:6a079fb5542e 50 uLCD.line(65, 50 , 65, 70, BLACK);
elirobelo 1:6a079fb5542e 51 } else if (arrowVec[i] == 3) {
elirobelo 1:6a079fb5542e 52 uLCD.cls();
elirobelo 1:6a079fb5542e 53 uLCD.printf(" Level %d", lev);
elirobelo 1:6a079fb5542e 54 uLCD.text_string("UP", 8, 14, FONT_12X16, GREEN);
elirobelo 1:6a079fb5542e 55 uLCD.triangle(44, 55, 84, 55, 64, 25, GREEN);
elirobelo 1:6a079fb5542e 56 uLCD.line(54, 55 , 74, 55, BLACK);
elirobelo 1:6a079fb5542e 57 uLCD.rectangle(54, 55, 74, 95,GREEN);
elirobelo 1:6a079fb5542e 58 uLCD.line(54, 55 , 74, 55, BLACK);
elirobelo 1:6a079fb5542e 59 } else {
elirobelo 1:6a079fb5542e 60 uLCD.cls();
elirobelo 1:6a079fb5542e 61 uLCD.printf(" Level %d", lev);
elirobelo 1:6a079fb5542e 62 uLCD.text_string("DOWN", 7, 14, FONT_12X16, WHITE);
elirobelo 1:6a079fb5542e 63 uLCD.triangle(44, 65, 84, 65, 64, 95, WHITE);
elirobelo 1:6a079fb5542e 64 uLCD.line(54, 65, 74, 65, BLACK);
elirobelo 1:6a079fb5542e 65 uLCD.rectangle(54, 25, 74, 65,WHITE);
elirobelo 1:6a079fb5542e 66 uLCD.line(54, 65, 74, 65, BLACK);
elirobelo 1:6a079fb5542e 67 }
elirobelo 1:6a079fb5542e 68 wait(0.5);
elirobelo 1:6a079fb5542e 69 }
elirobelo 1:6a079fb5542e 70 }
elirobelo 0:aa7531dac907 71
elirobelo 1:6a079fb5542e 72 void beep() {
elirobelo 1:6a079fb5542e 73 FILE *wave_file;
elirobelo 1:6a079fb5542e 74 wave_file=fopen("/sd/wavfiles/chime.wav","r");
elirobelo 1:6a079fb5542e 75 waver.play(wave_file);
elirobelo 1:6a079fb5542e 76 fclose(wave_file);
elirobelo 1:6a079fb5542e 77 }
elirobelo 1:6a079fb5542e 78
elirobelo 1:6a079fb5542e 79 void buzz() {
elirobelo 1:6a079fb5542e 80 FILE *wave_file;
elirobelo 1:6a079fb5542e 81 wave_file=fopen("/sd/wavfiles/buzzer.wav","r");
elirobelo 1:6a079fb5542e 82 waver.play(wave_file);
elirobelo 1:6a079fb5542e 83 fclose(wave_file);
elirobelo 1:6a079fb5542e 84 }
elirobelo 1:6a079fb5542e 85
elirobelo 1:6a079fb5542e 86
elirobelo 1:6a079fb5542e 87 void inputScreen() {
elirobelo 1:6a079fb5542e 88 uLCD.text_string("Input your answer", 1, 1, FONT_12X16, WHITE);
elirobelo 1:6a079fb5542e 89 }
elirobelo 1:6a079fb5542e 90
elirobelo 1:6a079fb5542e 91 void endGame(int level) {
elirobelo 1:6a079fb5542e 92 uLCD.cls();
elirobelo 1:6a079fb5542e 93 uLCD.text_string("Game Over", 5, 1, FONT_12X16, BLUE);
elirobelo 1:6a079fb5542e 94 uLCD.locate(1,5);
elirobelo 1:6a079fb5542e 95 uLCD.puts("You got to level");
elirobelo 1:6a079fb5542e 96 uLCD.locate(8,7);
elirobelo 1:6a079fb5542e 97 uLCD.printf("%d", level);
elirobelo 1:6a079fb5542e 98 uLCD.text_string("Fire to Restart", 2, 11, FONT_12X16, GREEN);
elirobelo 1:6a079fb5542e 99 while(1) {
elirobelo 1:6a079fb5542e 100 if(myNav.fire()) {
elirobelo 1:6a079fb5542e 101 uLCD.cls();
elirobelo 1:6a079fb5542e 102 return;
elirobelo 1:6a079fb5542e 103 }
elirobelo 1:6a079fb5542e 104 }
elirobelo 1:6a079fb5542e 105 }
elirobelo 1:6a079fb5542e 106
elirobelo 0:aa7531dac907 107 int main() {
elirobelo 1:6a079fb5542e 108 bool game = true;
elirobelo 0:aa7531dac907 109 int level = 0;
elirobelo 0:aa7531dac907 110 //int arrowVec[20];
elirobelo 1:6a079fb5542e 111 mainMenu();
elirobelo 0:aa7531dac907 112
elirobelo 0:aa7531dac907 113
elirobelo 0:aa7531dac907 114 while(game) {
elirobelo 0:aa7531dac907 115 level++;
elirobelo 1:6a079fb5542e 116
elirobelo 1:6a079fb5542e 117 for (int i = 0; i < level; i++) {
elirobelo 1:6a079fb5542e 118 arrow = (rand() % 4) + 1;
elirobelo 1:6a079fb5542e 119 arrowVec[i] = arrow;
elirobelo 1:6a079fb5542e 120 }
elirobelo 1:6a079fb5542e 121
elirobelo 0:aa7531dac907 122 createArrows(level);
elirobelo 1:6a079fb5542e 123 uLCD.cls();
elirobelo 1:6a079fb5542e 124 inputScreen();
elirobelo 1:6a079fb5542e 125
elirobelo 1:6a079fb5542e 126 for (int j = 0; j < level; j++) {
elirobelo 1:6a079fb5542e 127 int decision = 0;
elirobelo 1:6a079fb5542e 128 while (decision == 0) {
elirobelo 1:6a079fb5542e 129 if(myNav.fire()){
elirobelo 1:6a079fb5542e 130 }
elirobelo 1:6a079fb5542e 131 else if(myNav.left()) {
elirobelo 1:6a079fb5542e 132 decision = 1;
elirobelo 1:6a079fb5542e 133 if(arrowVec[j]==1) {
elirobelo 1:6a079fb5542e 134 correct = 1;
elirobelo 1:6a079fb5542e 135 beep();
elirobelo 1:6a079fb5542e 136 } else {
elirobelo 1:6a079fb5542e 137 correct = 2; // Game should end
elirobelo 1:6a079fb5542e 138 buzz();
elirobelo 1:6a079fb5542e 139 }
elirobelo 1:6a079fb5542e 140 }
elirobelo 1:6a079fb5542e 141
elirobelo 1:6a079fb5542e 142 else if(myNav.right()) {
elirobelo 1:6a079fb5542e 143 decision = 1;
elirobelo 1:6a079fb5542e 144 if(arrowVec[j]==2) {
elirobelo 1:6a079fb5542e 145 correct = 1;
elirobelo 1:6a079fb5542e 146 beep();
elirobelo 1:6a079fb5542e 147 } else {
elirobelo 1:6a079fb5542e 148 correct = 2;
elirobelo 1:6a079fb5542e 149 buzz();
elirobelo 1:6a079fb5542e 150 }
elirobelo 1:6a079fb5542e 151 }
elirobelo 1:6a079fb5542e 152
elirobelo 1:6a079fb5542e 153
elirobelo 1:6a079fb5542e 154 else if(myNav.up()) {
elirobelo 1:6a079fb5542e 155 decision = 1;
elirobelo 1:6a079fb5542e 156 if(arrowVec[j]==3) {
elirobelo 1:6a079fb5542e 157 correct = 1;
elirobelo 1:6a079fb5542e 158 beep();
elirobelo 1:6a079fb5542e 159 } else {
elirobelo 1:6a079fb5542e 160 correct = 2;
elirobelo 1:6a079fb5542e 161 buzz();
elirobelo 1:6a079fb5542e 162 }
elirobelo 1:6a079fb5542e 163 }
elirobelo 1:6a079fb5542e 164
elirobelo 1:6a079fb5542e 165
elirobelo 1:6a079fb5542e 166 else if(myNav.down()) {
elirobelo 1:6a079fb5542e 167 decision = 1;
elirobelo 1:6a079fb5542e 168 if(arrowVec[j]==4) {
elirobelo 1:6a079fb5542e 169 correct = 1;
elirobelo 1:6a079fb5542e 170 beep();
elirobelo 1:6a079fb5542e 171 } else {
elirobelo 1:6a079fb5542e 172 correct = 2;
elirobelo 1:6a079fb5542e 173 buzz();
elirobelo 1:6a079fb5542e 174 }
elirobelo 1:6a079fb5542e 175 }
elirobelo 1:6a079fb5542e 176
elirobelo 1:6a079fb5542e 177 if (correct==2){
elirobelo 1:6a079fb5542e 178 // end game
elirobelo 1:6a079fb5542e 179 game = false;
elirobelo 1:6a079fb5542e 180 endGame(level);
elirobelo 1:6a079fb5542e 181 game = true;
elirobelo 1:6a079fb5542e 182 level = 0;
elirobelo 1:6a079fb5542e 183 correct = 0;
elirobelo 1:6a079fb5542e 184 }
elirobelo 1:6a079fb5542e 185 }
elirobelo 1:6a079fb5542e 186 }
elirobelo 1:6a079fb5542e 187 if(level==21){
elirobelo 1:6a079fb5542e 188 game = false;
elirobelo 1:6a079fb5542e 189 endGame(level);
elirobelo 1:6a079fb5542e 190 level = 0;
elirobelo 1:6a079fb5542e 191 game = true;
elirobelo 1:6a079fb5542e 192 correct = 0;
elirobelo 1:6a079fb5542e 193 }
elirobelo 0:aa7531dac907 194 }
elirobelo 0:aa7531dac907 195
elirobelo 0:aa7531dac907 196
elirobelo 1:6a079fb5542e 197 }