lunes, 19 de mayo de 2014

Práctica 8

>

Código completo:
#include <allegro.h>
  /* Posiciones X e Y iniciales */
#define POS_X_INI 16
#define POS_Y_INI 10
#define INC_X_INI  1
#define INC_Y_INI  0
  /* Pausa en milisegundos entre un "fotograma" y otro */
#define PAUSA 350
  /* Teclas predefinidas */
#define TEC_ARRIBA KEY_E
#define TEC_ABAJO  KEY_X
#define TEC_IZQDA  KEY_S
#define TEC_DCHA   KEY_D
int posX, posY;  /* Posicion actual */
int incX, incY;  /* Incremento de la posicion */
  /* Terminado: Si ha chocado o comida todas las frutas */
int terminado;
  /* La tecla pulsada */
int tecla;
  /* Escala: relacion entre tama?o de mapa y de pantalla */
#define ESCALA 10
  /* Ancho y alto de los sprites */
#define ANCHOSPRITE 10
#define ALTOSPRITE  10
  /* Y el mapa que representa a la pantalla */
  /* Como usaremos modo grafico de 320x200 puntos */
  /* y una escala de 10, el tablero medira 32x20 */
#define MAXFILAS 20
#define MAXCOLS  32
char mapa[MAXFILAS][MAXCOLS]={
  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "X                      X     X",
  "X    F                 X     X",
  "X               F      X  F  X",
  "X     XXXXX            X     X",
  "X     X                X     X",
  "X     X        X       X     X",
  "X     X        X       X     X",
  "X              X       X     X",
  "X              X       X     X",
  "X              X       X     X",
  "X   F          X             X",
  "X              X             X",
  "X              X         F   X",
  "X      X            X        X",
  "X      X            X        X",
  "X      X       F     X       X",
  "X  F   X              X      X",
  "X      X              F      X",
  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
};
int numFrutas = 7;
  /* Nuestros sprites */
BITMAP *ladrilloFondo, *comida, *jugador;
typedef
  char tipoSprite[ANCHOSPRITE][ALTOSPRITE];
                         /* El sprite en si: matriz de 30x30 bytes */
tipoSprite spriteLadrillo =
   {{0,2,2,2,2,2,2,2,2,0},
    {2,1,1,1,1,1,1,1,1,2},
    {2,1,1,1,1,1,1,1,1,2},
    {2,1,1,1,1,1,1,1,1,2},
    {2,1,1,1,1,1,1,1,1,2},
    {2,1,1,1,1,1,1,1,3,2},
    {2,1,1,1,1,1,1,3,3,2},
    {2,1,1,1,1,1,3,3,2,2},
    {2,2,2,2,2,2,2,2,2,0}
   };
tipoSprite spriteComida =
   {{0,0,0,2,0,0,0,0,0,0},
    {0,0,2,2,0,0,2,2,0,0},
    {0,4,4,4,2,2,4,4,0,0},
    {4,4,4,4,4,2,4,4,4,0},
    {4,4,4,4,4,4,4,4,4,0},
    {4,4,4,4,4,4,4,4,4,0},
    {4,4,4,4,4,4,4,4,4,0},
    {4,4,4,4,4,4,4,4,4,0},
    {0,4,4,4,4,4,4,4,0,0}
   };
tipoSprite spriteJugador =
   {{0,0,3,3,3,3,3,0,0,0},
    {0,3,1,1,1,1,1,3,0,0},
    {3,1,1,1,1,1,1,1,3,0},
    {3,1,1,1,1,1,1,1,3,0},
    {3,1,1,1,1,1,1,1,3,0},
    {3,1,1,1,1,1,1,1,3,0},
    {0,3,1,1,1,1,1,3,0,0},
    {0,0,3,3,3,3,3,0,0,0}
   };
/* -------------- Rutina de crear los sprites ------------- */
void creaSprites()
{
  int i, j;
  ladrilloFondo = create_bitmap(10, 10);
  clear_bitmap(ladrilloFondo);
  for(i=0; i<ANCHOSPRITE; i++)
    for (j=0; j<ALTOSPRITE; j++)
      putpixel(ladrilloFondo, i, j,
        palette_color[ spriteLadrillo[j][i]  ]);
  comida = create_bitmap(10, 10);
  clear_bitmap(comida);
  for(i=0; i<ANCHOSPRITE; i++)
    for (j=0; j<ALTOSPRITE; j++)
      putpixel(comida, i, j,
        palette_color[ spriteComida[j][i]  ]);
  jugador = create_bitmap(10, 10);
  clear_bitmap(jugador);
  for(i=0; i<ANCHOSPRITE; i++)
    for (j=0; j<ALTOSPRITE; j++)
      putpixel(jugador, i, j,
        palette_color[ spriteJugador[j][i]  ]);
}
/* -------------- Rutina de dibujar el fondo ------------- */
void dibujaFondo()
{
  int i, j;
  clear_bitmap(screen);
    for(i=0; i<MAXCOLS; i++)
    for (j=0; j<MAXFILAS; j++) {
      if (mapa[j][i] == 'X')
        draw_sprite(screen, ladrilloFondo, i*ESCALA, j*ESCALA);
      if (mapa[j][i] == 'F')
        draw_sprite(screen, comida, i*ESCALA, j*ESCALA);
      }
 }
 /* ------------------------------------------------ */
/*                                                  */
/* -------------- Cuerpo del programa ------------- */
int main()
{
  allegro_init();              /* Inicializamos Allegro */
  install_keyboard();
  install_timer();
                               /* Intentamos entrar a modo grafico */
  if (set_gfx_mode(GFX_SAFE, 320, 200, 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;
  }
   /* ----------------------- Si todo ha ido bien: empezamos */
   creaSprites();
  dibujaFondo();
   /* Valores iniciales */
  posX = POS_X_INI;
  posY = POS_Y_INI;

   incX = INC_X_INI;
  incY = INC_Y_INI;
             /* Parte repetitiva: */
  do {
    dibujaFondo();
    draw_sprite (screen, jugador, posX*ESCALA, posY*ESCALA);
    terminado = FALSE;
    /* Si paso por una fruta: la borro y falta una menos */
    if (mapa[posY][posX] == 'F') {
      mapa[posY][posX] = ' ';
      numFrutas --;
      if (numFrutas == 0) {
        textout(screen, font,
          "Ganaste!", 100, 90, palette_color[14]);
        terminado = TRUE;
      }
    }
  
    /* Si choco con la pared, se acabo */
    if (mapa[posY][posX] == 'X') {
      textout(screen, font,
        "Chocaste!", 100, 90, palette_color[13]);
      terminado = TRUE;
    }
    if (terminado) break;
    /* Compruebo si se ha pulsado alguna tecla */
    if ( keypressed() ) {
        tecla = readkey() >> 8;
        switch (tecla) {
          case TEC_ARRIBA:
            incX =  0; incY = -1;  break;
          case TEC_ABAJO:
            incX =  0; incY =  1;  break;
          case TEC_IZQDA:
            incX = -1; incY =  0;  break;
          case TEC_DCHA:
            incX =  1; incY =  0;  break;
        }
     }
     posX += incX;
    posY += incY;
    /* Peque?a pausa antes de seguir */
    rest ( PAUSA );
  }
  while (TRUE);  /* Repetimos indefininamente */
                 /* (la condici?n de salida la comprobamos "dentro") */
   readkey();
  return 0;
 }
                      /* Termino con la "macro" que me pide Allegro */
END_OF_MAIN();


Práctica 7

Practica 7 by KatyLizethCruz

Práctica 6

POVI Practica 6 by Katy Cruz

Práctica 5

PRACTICA5-POVI by KatyLizethCruz