Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 00004 TextLCD lcd(PTC5, PTC6, PTC7, PTC0, PTC3, PTC4); // rs, e, d4-d7 00005 PwmOut bocina(PTB1); //SONIDOS DEL JUEGO 00006 AnalogIn pot (PTB0); //SELECCION DE MENU 00007 DigitalOut ledazul(PTC12); 00008 DigitalOut ledrojo(PTC13); 00009 DigitalOut ledverde(PTC16); 00010 DigitalOut ledamarillo(PTC17); 00011 DigitalIn start(PTD1); //BOTON DE INICIO 00012 DigitalIn pushazul(PTD3); 00013 DigitalIn pushrojo(PTD2); 00014 DigitalIn pushverde(PTD0); 00015 DigitalIn pushamarillo(PTD5); 00016 Serial pc (USBTX, USBRX); 00017 Timer debounce; 00018 00019 int valorpot = 0, dificultad = 0, punt_max = 0, punt = 0; 00020 //VALORES DE DIFICULTAD DEL JUEGO, VALORES DE PUNTUACION. 00021 float speed = 0, final = 0, fin_turno = 0; 00022 //VALORES Y BANDERAS DE JUEGO 00023 int says [200], player [200], cont_total = 0, move = 0; 00024 //VALORES Y REGISTROS DE JUGADOR Y COMPUTADORA 00025 00026 int menu_principal(void); 00027 void logo(void); 00028 void game_start(void); 00029 void juego(void); 00030 void time_select(int dif); 00031 void simon(float speed); 00032 void end_game(int punt); 00033 int get_number(void); 00034 void comred(void); 00035 void comblue(void); 00036 void comgreen(void); 00037 void comyellow(void); 00038 00039 int main() { 00040 while(1){ 00041 logo(); 00042 juego(); 00043 } 00044 } 00045 00046 /** 00047 * Esta funcion tiene como proposito reiniciar los valores 00048 * de las variables del juego a sus valores predeterminados 00049 * cada vez que es llamada, despues, ejecutar el ciclo del 00050 * juego. 00051 */ 00052 void juego(void){ 00053 pc.printf("REINICIANDO VALORES DEL JUEGO\n"); 00054 bocina.write(0.50f); // 50% duty cycle 00055 bocina.period_us(0); //CANCELAR SONIDO 00056 valorpot = 0; 00057 dificultad = 0; 00058 speed = 0; 00059 punt = 0; 00060 final = 0; 00061 cont_total = 0; 00062 dificultad = menu_principal(); //SELECCIONAR DIFICULTAD 00063 if(dificultad == 3){ 00064 pc.printf("\nDIFICULTAD FACIL"); 00065 }else if(dificultad == 2){ 00066 pc.printf("\nDIFICULTAD NORMAL"); 00067 }else if(dificultad == 1){ 00068 pc.printf("\nDIFICULTAD DIFICIL"); 00069 } 00070 time_select(dificultad); //AJUSTAR VELOCIDAD DEL JUEGO 00071 game_start(); //PRESENTACION DE VALORES PRE-JUEGO 00072 simon(speed); //INICIAR EL JUEGO 00073 end_game(punt); //PRESENTACION DE VALORES POST-JUEGO 00074 } 00075 00076 /** 00077 * Esta funcion es el juego principal, al iniciar cada turno, se genera un nuevo 00078 * valor aleatorio para la secuencia del juego, este es agregado a un arreglo, despues, 00079 * la computadora despliega el arreglo ajustandolo a la velocidad de la dificultad 00080 * elejida por el jugador. Cuando la seucencia es termnada, el jugador debe introducir 00081 * en los botones del juego el mismo patron previamente desplegado, al fallar, termina el 00082 * juego, al terminar una secuencia exitosamente, se aumenta su puntuacion. 00083 */ 00084 void simon(float speed){ 00085 int i = 0, prev = 0; 00086 while(final != 1){ 00087 fin_turno = 0; 00088 //JUEGA COM 00089 lcd.cls(); 00090 lcd.printf("SIMON SAYS... "); 00091 lcd.locate(0,1); 00092 move = rand() %4; //GENERAR NUEVO MOMIENTO DE JUEGO ALEATORIO 00093 move++; 00094 pc.printf("\nRANDOM: %d\n",move); 00095 if(move == prev){ //VERIFICAR QUE LOS MOVIMIENTOS DE MESCLEN ENTRE ELLOS 00096 if(move != 4){ 00097 move = move++; 00098 }else if (move == 4){ 00099 move = move--; 00100 } 00101 } 00102 prev = move; 00103 cont_total++; 00104 says[cont_total] = move; 00105 pc.printf("\nSIMON SAYS... "); 00106 for(i = 1; i <= cont_total; i++){ //DESPLIEGE DE LA SECUENCIA DE JUEGO 00107 pc.printf("%d",says[i]); 00108 if(says[i] == 1){ 00109 comred(); 00110 }else if(says[i] == 2){ 00111 comblue(); 00112 }else if(says[i] == 3){ 00113 comyellow(); 00114 }else if(says[i] == 4){ 00115 comgreen(); 00116 } 00117 wait(speed); 00118 } 00119 //JUEGA USR 00120 lcd.cls(); 00121 lcd.printf("\nPLAYER SAYS..."); 00122 pc.printf("\nPLAYER SAYS... "); 00123 lcd.locate(0,1); 00124 while(fin_turno != 1){ 00125 for(i = 1; i <= cont_total; i++){ //ANALISIS DE MOVIMIENTOS DE USUARIO 00126 player[i] = get_number(); 00127 pc.printf("%d",player[i]); 00128 if(says[i] != player[i]){ //CUANDO EL USUARIO FALLA... 00129 final = 1; 00130 fin_turno = 1; 00131 lcd.cls(); 00132 lcd.printf("THAT'S NOT WHAT"); 00133 lcd.locate(0,1); 00134 lcd.printf("SIMON SAID!!"); 00135 lcd.locate(0,2); 00136 wait(3); 00137 return; 00138 } 00139 } 00140 punt++; //AUMENTO DE PUNTUACION 00141 fin_turno = 1; //FIN DEL TURNO 00142 wait(1); 00143 } 00144 } 00145 } 00146 00147 /** 00148 * Esta funcion, espera a que el usuario presione uno de los botones del juego 00149 * ensendiendo el respectivo Led de este, haciendolo sonar, y regresando un 00150 * valor numerico respectivo para la matematica del juego. 00151 */ 00152 int get_number(void){ 00153 while(1){ 00154 if(debounce.read_ms()>200){ 00155 if(pushrojo == 1){ 00156 bocina.period_us(2500); //MUY BAJO 00157 ledrojo = !ledrojo; 00158 wait(0.2); 00159 bocina.period_us(0); //CANCELAR SONIDO 00160 ledrojo = !ledrojo; 00161 return 1; 00162 }else if(pushazul == 1){ 00163 bocina.period_us(2000); //BAJO 00164 ledazul = !ledazul; 00165 wait(0.2); 00166 bocina.period_us(0); //CANCELAR SONIDO 00167 ledazul = !ledazul; 00168 return 2; 00169 }else if(pushverde == 1){ 00170 bocina.period_us(1300); //ALTO 00171 ledverde = !ledverde; 00172 wait(0.2); 00173 bocina.period_us(0); //CANCELAR SONIDO 00174 ledverde = !ledverde; 00175 return 4; 00176 }else if(pushamarillo == 1){ 00177 bocina.period_us(1500); //MEDIO 00178 ledamarillo = !ledamarillo; 00179 wait(0.2); 00180 bocina.period_us(0); //CANCELAR SONIDO 00181 ledamarillo = !ledamarillo; 00182 return 3; 00183 } 00184 } 00185 } 00186 } 00187 00188 /** 00189 * Ensiende y suena el boton azul por parte de la computadora. 00190 */ 00191 void comblue(void){ 00192 bocina.period_us(2000); //BAJO 00193 ledazul = !ledazul; 00194 wait(0.2); 00195 bocina.period_us(0); //CANCELAR SONIDO 00196 ledazul = !ledazul; 00197 } 00198 00199 /** 00200 * Ensiende y suena el boton rojo por parte de la computadora. 00201 */ 00202 void comred(void){ 00203 bocina.period_us(2500); //MUY BAJO 00204 ledrojo = !ledrojo; 00205 wait(0.2); 00206 bocina.period_us(0); //CANCELAR SONIDO 00207 ledrojo = !ledrojo; 00208 } 00209 00210 /** 00211 * Ensiende y suena el boton verde por parte de la computadora. 00212 */ 00213 void comgreen(void){ 00214 bocina.period_us(1300); //ALTO 00215 ledverde = !ledverde; 00216 wait(0.2); 00217 bocina.period_us(0); //CANCELAR SONIDO 00218 ledverde = !ledverde; 00219 } 00220 00221 /** 00222 * Ensiende y suena el boton amarillo por parte de la computadora. 00223 */ 00224 void comyellow(void){ 00225 bocina.period_us(1500); //MEDIO 00226 ledamarillo = !ledamarillo; 00227 wait(0.2); 00228 bocina.period_us(0); //CANCELAR SONIDO 00229 ledamarillo = !ledamarillo; 00230 } 00231 00232 /** 00233 * Esta funcion termina el juego y compara la puntuacion obtenida 00234 * con la mayor almacenada, en caso de ser mayor a esta, se 00235 * muestra un mensaje y se cambia la puntuacion mayor. 00236 */ 00237 void end_game(int punt){ 00238 if(punt > punt_max){ //COMPARACION DE PUNTAJES 00239 punt_max = punt; 00240 lcd.cls(); 00241 pc.printf("\nNEW HIGH SCORE\nFINAL SCORE : %d\n",punt_max); 00242 lcd.printf("NEW HIGH SCORE!!"); 00243 lcd.locate(0,1); 00244 lcd.printf("FINAL SCORE : %d",punt_max); 00245 lcd.locate(0,2); 00246 wait(3); 00247 }else if(punt <= punt_max){ 00248 lcd.cls(); 00249 pc.printf("\nFINAL SCORE : %d\n",punt); 00250 lcd.printf("FINAL SCORE : %d",punt); 00251 lcd.locate(0,2); 00252 wait(3); 00253 } 00254 juego(); 00255 } 00256 00257 /** 00258 * Esta funcion ajuta la velocidad del juego dependiendo 00259 * del valor de dificultad previamente seleccionado. 00260 */ 00261 void time_select(int dif){ 00262 switch(dif){ 00263 case 3: 00264 speed = 1.0; //FACIL 00265 break; 00266 case 2: 00267 speed = 0.5; //NORMAL 00268 break; 00269 case 1: 00270 speed = 0.3; //DIFICIL 00271 break; 00272 } 00273 } 00274 00275 /** 00276 * Este proceso muestra la presentacion inicial del juego. 00277 */ 00278 void logo(void){ 00279 lcd.cls(); 00280 lcd.printf("LUIS VIZK"); 00281 lcd.locate(0,1); 00282 lcd.printf("DANI ONTIVEROS"); 00283 lcd.locate(0,2); 00284 wait(3); 00285 lcd.cls(); 00286 lcd.printf("SPOOKY THIEF"); 00287 lcd.locate(0,1); 00288 lcd.printf("~~SIMON~~"); 00289 lcd.locate(0,3); 00290 wait(3); 00291 } 00292 00293 /** 00294 * Esta funcion calibra el valor del potenciometro del sistema 00295 * para elejir entre 3 valores predeterminados de dificultad 00296 * para el juego, dependiendo del voltaje que se resive. 00297 * El valor de dificultad seleccionado es guardado cuando el 00298 * jugador presiona el boton de inicio. 00299 */ 00300 int menu_principal(void){ 00301 int opcion = 2; 00302 debounce.start(); //INICIAR TIMER 00303 while(start != 1){ 00304 lcd.cls(); 00305 lcd.printf("DIFFICULTY"); 00306 lcd.locate(0,1); 00307 valorpot = pot * 10; 00308 if(valorpot <= 3){ 00309 lcd.printf("HARD"); 00310 lcd.locate(0,2); 00311 opcion = 1; 00312 }else if(valorpot >= 4 && valorpot <= 9){ 00313 lcd.printf("NORMAL"); 00314 lcd.locate(0,2); 00315 opcion = 2; 00316 }else if(valorpot >= 10){ 00317 lcd.printf("EASY"); 00318 lcd.locate(0,2); 00319 opcion = 3; 00320 } 00321 if(debounce.read_ms()>200){ 00322 debounce.reset(); 00323 } 00324 wait(0.2); 00325 } 00326 return opcion; 00327 } 00328 00329 /** 00330 * Esta funcion muestra el valor de puntuacion mas grande 00331 * en el juego y luego pide al usuario precionar el 00332 * boton de inicio para comenzar el juego. 00333 */ 00334 void game_start(void){ 00335 lcd.cls(); 00336 debounce.start(); //INICIAR TIMER 00337 lcd.printf("HIGH SCORE : %d",punt_max); 00338 lcd.locate(0,1); 00339 wait(1); 00340 lcd.printf("PRESS START!!"); 00341 lcd.locate(0,1); 00342 if(debounce.read_ms()>200){ 00343 while(start != 1){ 00344 wait(0.1); 00345 } 00346 debounce.reset(); 00347 wait(1); 00348 lcd.cls(); 00349 lcd.printf("GAME ON!!",punt_max); 00350 pc.printf("\nINICIANDO JUEGO\n"); 00351 lcd.locate(0,1); 00352 wait(1); 00353 } 00354 }
Generated on Thu Aug 4 2022 03:24:57 by
1.7.2