Você está na página 1de 9

Colegio De Educacin Profesional Tcnica Del Estado De

Veracruz
Conalep N.165
Lic. Jess Reyes Heroles
Modulo.
Programacin De Videojuegos
Psp:
Miguel ngel Ramos Grande
Alumnos:
Carlos Alberto Hernndez Gonzlez
Itzel Ponce Silva
Grupo Y Carrera:
604-Info
Practica 12
Propsito De La Prctica:
Crear un programa para el juego Simn Dice, juego clsico de
memoria auditiva donde se hacen reproducciones de sonido.









En esta prctica se pretende utilizar sonidos durante la ejecucin del programa, este
juego se llama simn dice, este consiste en repetir una secuencia de sonidos
(y luces) que nos iba proporcionando la computadora: primero mostraba una sola
nota; si la recordamos, se aada una segunda nota; si recordbamos estas dos se
aada una tercera, despus una cuarta y as sucesivamente hasta que cometamos
un error. Todo mediante ficheros con formatos MID.

Para poder empezar a realizarlo primero abrimos
Abrir Dev-C++
Damos clic en ARCHIV
Seleccionamos el men NUEVO
Enseguida damos clic en PROYECTO

2. - Se abrir la siguiente ventana y seleccionamos el men siguiente:

Y despus empezamos introduciendo el cdigo.
#include <stdlib.h> // Para "rand"
#include <ctype.h> // Para "tolower"
#include <allegro.h>

/* -------------- constantes ------------- */
#define ANCHOPANTALLA 320
#define ALTOPANTALLA 200
#define MAXNOTAS 300
#define RETARDONOTA 200
#define RETARDOETAPA 1000

#define TECLA1 'w'
#define TECLA2 'e'
#define TECLA3 's'
#define TECLA4 'd'

Aqu es donde re declaran los ficheros de sonido, los que se ocupara en la ejecucin
del cdigo.
#define FICHEROSONIDO1 "simon1.mid"
#define FICHEROSONIDO2 "simon2.mid"
#define FICHEROSONIDO3 "simon3.mid"
#define FICHEROSONIDO4 "simon4.mid"

/* -------------- Variables globales -------------- */
int
notaActual = 0, // Numero de nota actual
notas[MAXNOTAS], // Secuencia de notas
acertado; // Si se ha acertado o no

MIDI *sonido1, *sonido2, *sonido3, *sonido4;

/* -------------- Rutina de inicializaci?n -------- */
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;
}

/* -------------- Rutina de dibujar pantalla ------ */
void dibujaPantalla()
{
Aqu se comienza a dibujar los rectngulos de colores para el videojuego

// Borro pantalla
clear_bitmap(screen);

// Primer sector: SupIzq -> verde
rectfill(screen, 10,10,
ANCHOPANTALLA/2-10,ALTOPANTALLA/2-30,
makecol(0, 150, 0));
textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA/2-28,
makecol(0, 150, 0), "%c",
TECLA1);

// Segundo sector: SupDcha -> rojo
rectfill(screen, ANCHOPANTALLA/2+10,10,
ANCHOPANTALLA-10,ALTOPANTALLA/2-30,
makecol(150, 0, 0));
textprintf(screen, font, ANCHOPANTALLA/4*3, ALTOPANTALLA/2-28,
makecol(150, 0, 0), "%c",
TECLA2);

// Tercer sector: InfIzq -> amarillo
rectfill(screen, 10,ALTOPANTALLA/2-10,
ANCHOPANTALLA/2-10,ALTOPANTALLA-50,
makecol(200, 200, 0));
textprintf(screen, font, ANCHOPANTALLA/4, ALTOPANTALLA-48,
makecol(200, 200, 0), "%c",
TECLA3);

// Cuarto sector: InfDcha -> azul
rectfill(screen, ANCHOPANTALLA/2+10,ALTOPANTALLA/2-10,
ANCHOPANTALLA-10,ALTOPANTALLA-50,
makecol(0, 0, 150));
textprintf(screen, font, ANCHOPANTALLA/4*3, ALTOPANTALLA-48,
makecol(0, 0, 150), "%c",
TECLA4);

textprintf(screen, font, 4,ALTOPANTALLA-20, palette_color[13],
"Puntos: %d", notaActual*10); // Puntuaci?n
}


/* -------------- Rutina de reproducir notas ------ */
Para esto se utiliz un funcin que despus se manda a llamar en alguna parte del
programa
void reproduceNotas()
{
int i;

for (i=0; i<=notaActual; i++) {

if (notas[i] == 0) {
play_midi(sonido1, FALSE);
rectfill(screen, 10,10,
ANCHOPANTALLA/2-10,ALTOPANTALLA/2-30,
makecol(255, 255, 255));
}

if (notas[i] == 1) {
play_midi(sonido2, FALSE);
rectfill(screen, ANCHOPANTALLA/2+10,10,
ANCHOPANTALLA-10,ALTOPANTALLA/2-30,
makecol(255, 255, 255));
}

if (notas[i] == 2) {
play_midi(sonido3, FALSE);
rectfill(screen, 10,ALTOPANTALLA/2-10,
ANCHOPANTALLA/2-10,ALTOPANTALLA-50,
makecol(255, 255, 255));
}

if (notas[i] == 3) {
play_midi(sonido4, FALSE);
rectfill(screen, ANCHOPANTALLA/2+10,ALTOPANTALLA/2-10,
ANCHOPANTALLA-10,ALTOPANTALLA-50,
makecol(255, 255, 255));
}
rest(RETARDONOTA);
dibujaPantalla();
}
}

/* -------------- Rutina de comparar notas ------ */
Despus de que el usuario toque el mismo ritmo que el asignado por la computadora
se verifica que las haya hecho correctamente
int comparaNota(char tecla, int notaActual)
{
int i;

// Presupongo que no ha acertado y comparare 1 x 1
int seHaAcertado = 0;

if ( (tecla == TECLA1) && (notas[notaActual] == 0) ){
play_midi(sonido1, FALSE);
rectfill(screen, 10,10,
ANCHOPANTALLA/2-10,ALTOPANTALLA/2-30,
makecol(255, 255, 255));
seHaAcertado = 1;
}

if ( (tecla == TECLA2) && (notas[notaActual] == 1) ){
play_midi(sonido2, FALSE);
rectfill(screen, ANCHOPANTALLA/2+10,10,
ANCHOPANTALLA-10,ALTOPANTALLA/2-30,
makecol(150, 0, 0));
seHaAcertado = 1;
}

if ( (tecla == TECLA3) && (notas[notaActual] == 2) ){
play_midi(sonido3, FALSE);
rectfill(screen, 10,ALTOPANTALLA/2-10,
ANCHOPANTALLA/2-10,ALTOPANTALLA-50,
makecol(200, 200, 0));
seHaAcertado = 1;
}

if ( (tecla == TECLA4) && (notas[notaActual] == 3) ){
play_midi(sonido4, FALSE);
rectfill(screen, ANCHOPANTALLA/2+10,ALTOPANTALLA/2-10,
ANCHOPANTALLA-10,ALTOPANTALLA-50,
makecol(255, 255, 255));
seHaAcertado = 1;
}

return seHaAcertado;
}

/* -------------- 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));

En esta fila se coloc un if para cuando acert el jugador pase al siguiente nivel.
// 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));

textprintf(screen, font,
ANCHOPANTALLA/4, ALTOPANTALLA/2 + 20, palette_color[15],
"Partida terminada");
readkey();
return 0;

}

END_OF_MAIN();


Ahora debemos de ir introduciendo los cdigos
correspondientes para la prctica, es decir, todas las
libreras de sonido, funciones, etc.:
Procederemos a compilar el programa las veces que
sean necesarias, hasta que ya no nos marque error en
nuestra pantalla, as de esta manera el programa ser
de una mejor calidad:
Finalmente nuestro programa quedar de esta manera, a la hora ejecutarlo se podr a or los
sonidos definidos en el programa, tambin quedara de una manera vistosa con la mezcla de
colores que nosotros elijamos:






En esta prctica hemos podido aplicar sonidos en el formato MIDI, as de esta
manera nuestro proyecto o practica se hace de una manera ms vistosa o llamativa.
Aunque me marcaba errores pero le pregunte al mis compaeros y me dijeron que
era por el Windows 8 y fue cuando comprend y as que tena que hacerlo en el
Windows 7 para que pudiera para eso lo hice en otro lugar.

Você também pode gostar