FECHA: 31- MAYO-2014 Practica 12: Reproducir sonidos. Juego SimeonDice usando lenguaje de programacin PROPOSITO: Elabora una aplicacin de videojuego SimeonDice usando lenguaje de programacin para generar el juego clsico de memoria auditiva donde se hacen reproducciones de sonido.
Desarrollo Lo primero que debemos hacer es iniciar el programa de DevC, al abrir el programa, elegimos el men de Archivo, damos clic en Nuevo y seleccionamos la opcin de Proyecto. Ahora debemos de elegir el tipo de proyecto a emplear, en este caso, ser Allegro apliccation estatic, elegimos este y le asignamos el nuevo nombre a nuestra practica o proyecto y por ltimo damos clic en Aceptar. que nos va proponiendo la computadora: primero ser una sola nota; si la recordamos, se aade un segunda nota; si recordamos estas dos se aade una tercera, despus una cuarta y as elaboracin del juego basndose en el siguiente secuenciaDeNotas = vacia repetir generar nuevaNota al azar anadir nuevaNota a secuenciaDeNotas reproducir secuenciaDeNotas fallo = FALSO desde i = 1 hasta (i=numeroDeNotas) o hasta (fallo) esperar a que el usuario escoja una nota si notaEscogida != nota[i] entonces fallo=VERDADERO finDesde hasta fallo puntuacion = numeroNotasAcertadas * 1
CODIGO UTILIZADO.-
Cdigo. #include <stdlib.h> // Para "rand" #include <ctype.h> // Para "tolower" #include <allegro.h>
/* -------------- Variables globales -------------- */ int notaActual = 0, // Nmero de nota actual notas[MAXNOTAS], // Secuencia de notas acertado; // Si se ha acertado o no
MIDI *sonido1, *sonido2, *sonido3, *sonido4;
/* -------------- Rutina de inicializacin -------- */ int inicializa() { allegro_init(); // Inicializamos Allegro install_keyboard(); install_timer();
// Intentamos entrar a modo grafico if (set_gfx_mode(GFX_SAFE, ANCHOPANTALLA, ALTOPANTALLA, 0, 0) != 0) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message( "Incapaz de entrar a modo grafico\n%s\n", allegro_error); return 1; }
// e intentamos usar midi if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "") != 0) { allegro_message("Error inicializando el sistema de sonido\n%s\n", allegro_error); return 2; }
sonido1 = load_midi(FICHEROSONIDO1); if (!sonido1) { allegro_message("Error leyendo el fichero MID '%s'\n", FICHEROSONIDO1); return 3; }
sonido2 = load_midi(FICHEROSONIDO2); if (!sonido2) { allegro_message("Error leyendo el fichero MID '%s'\n", FICHEROSONIDO2); return 3; }
sonido3 = load_midi(FICHEROSONIDO3); if (!sonido3) { allegro_message("Error leyendo el fichero MID '%s'\n", FICHEROSONIDO3); return 3; }
sonido4 = load_midi(FICHEROSONIDO4); if (!sonido4) { allegro_message("Error leyendo el fichero MID '%s'\n", FICHEROSONIDO4); return 3; }
// Preparo nmeros aleatorios srand(time(0));
// Volumen al mximo, por si acaso set_volume(255,255);
// Y termino indicando que no ha habido errores return 0; }
/* ------------------------------------------------ */ /* */ /* -------------- Cuerpo del programa ------------- */
int main() {
int i; char tecla;
// Intento inicializar if (inicializa() != 0) exit(1);
dibujaPantalla(); textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA/2 - 40, makecol(255, 255, 255), " S I M E O N D I C E "); textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA/2, makecol(255, 255, 255), "Pulsa una tecla para jugar"); readkey();
do { // Parte que se repite hasta que falle
dibujaPantalla(); // Dibujo la pantalla de juego
// Genero nueva nota y reproduzco todas notas[notaActual] = rand() % 4; reproduceNotas();
acertado = 1; // Presupongo que acertar
// Ahora el jugador intenta repetir i = 0; do { tecla = tolower(readkey()); // Leo la tecla if (tecla == 27) break; // Salgo si es ESC acertado = comparaNota(tecla, i); // Comparo i ++; // Y paso a la siguiente } while ((i<=notaActual) && (acertado == 1));
// Una nota ms if (acertado == 1) { textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA/2, makecol(255, 255, 255), "Correcto!"); notaActual ++; } else { textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA/2, makecol(255, 255, 255), "Fallaste!"); }
rest (RETARDOETAPA);
} while ((acertado == 1) && (notaActual < MAXNOTAS));