# include <iostream.h>
# include <constrea.h>
# include <stdio.h>
# include <string.h>


# define IZQUIERDA -1
# define DERECHA 1
# define QUIETO 0
# define VALOR_NAVE 5
# define SI 1
# define NO 0
# define ANCHO_PANTALLA 80
# define ALTO_PANTALLA 24
# define TECLA_DERECHA  77
# define TECLA_IZQUIERDA  75
# define TECLA_ESPACIO   32
# define TECLA_ESCAPE   27

void Iniciar();
void LeerTeclado();
void DibujarNave();
void DibujarJugador();
void DibujarMisil();
void DibujarExplosion();
void MoverNave();
void MoverJugador();
void MoverMisil();
void MostrarPuntaje();
void Terminar();

struct {
  
int x;
  
int y;
  
int direccion;
  
char imagen[9];
  
int ancho;
  
int alto;
  
int contador;
       }
nave,jugador;

struct{
      
int x;
      
int y;
      
char imagen;
      
int contador;
       }
misil;


int bDisparo;
int bExplosion ;
int puntaje;
int tecla;
int bSalida;
int bGanador;
int bPerdedor;

main ()
{
  
Iniciar();
  
DibujarJugador();
  
DibujarNave();

  
while(!bSalida)
   {
     
LeerTeclado();
     
MoverJugador();
     
MoverNave();
     
MoverMisil();
     
DibujarJugador();
     
DibujarNave();
     
DibujarMisil();
     
DibujarExplosion();
     
MostrarPuntaje();
   }
  
Terminar();

  
getch();
  
return 0;
}


void Iniciar()
{
  
_setcursortype(_NOCURSOR);
  
clrscr();
  
bSalida=0 ; bDisparo=0 ; bExplosion =0 ; puntaje =0 ;
  
nave.x=10; nave.y=7; strcpy(nave.imagen," oOoOo ");  nave.direccion=1;
  
nave.ancho=9; nave.alto=1; nave.contador=0;
  
jugador.x= 40; jugador.y=20;
  
strcpy(jugador.imagen," ><<+>>< ");
   
jugador.ancho=9; jugador.alto=1;jugador.contador=0;
  
misil.imagen= '|'; misil.contador=0;
  
bGanador=NO;bPerdedor=NO;
}

void LeerTeclado()
{
    
if(kbhit())
     {
  
tecla=getch();
  
switch(tecla)
   {
  
case TECLA_DERECHA:
     
jugador.direccion=DERECHA;
     
break;
  
case TECLA_IZQUIERDA:
     
jugador.direccion=IZQUIERDA;
     
break;
  
case TECLA_ESPACIO:
     
bDisparo=SI;
     
gotoxy (misil.x, misil.y);
     
printf (" ");
     
misil.x= jugador.x+(jugador.ancho)/2;
     
misil.y= jugador.y - 1 ;
     
break;
  
case  TECLA_ESCAPE:
     
bSalida=SI;
     
break;
   }
    }
 }

void DibujarNave()
{
  
gotoxy(nave.x,nave.y);
  
printf("%s", nave.imagen);
}

void DibujarJugador()
{
  
gotoxy(jugador.x,jugador.y);
  
printf("%s",jugador.imagen);
}


void DibujarMisil()
{
 
if (bDisparo==SI)
  {
    
if(misil.y>2)
     {
    
gotoxy (misil.x,misil.y);
    
printf( "%c",misil.imagen);
     }
   }
}
void DibujarExplosion()
{
   
if(bExplosion == SI)
    {
       
gotoxy(nave.x,nave.y);
       
printf(" (  (( * ))  ) ");
       
bExplosion=NO;
       
nave.x=2nave.y=4;
       
nave.direccion= DERECHA;

   }
}


void MoverNave()
{
   
if (nave.contador>200)
    {
     
nave.contador=0;
     
nave.x=nave.x + nave.direccion;
     
if( nave.x>(ANCHO_PANTALLA- nave.ancho))
      {
    
gotoxy(nave.x,nave.y);
    
printf("       ");
    
nave.direccion=IZQUIERDA;
    
nave.y+=3;
      }
      
if(nave.x < 2)
      {
    
gotoxy(nave.x,nave.y);
    
printf("       ");
    
nave.direccion=DERECHA;
    
nave.y+=3;
      }
     
if( nave.y>ALTO_PANTALLA - jugador.alto)
   
bPerdedor=SI;
   }
  
nave.contador++;
}

void MoverJugador()
{
 
if(jugador.contador>100)
  {
 
jugador.contador=0;
 
jugador.x+=jugador.direccion;
 
if(jugador.x>ANCHO_PANTALLA-jugador.ancho)
      {
     
gotoxy(jugador.x,jugador.y);
     
printf ("       ");
     
jugador.x =2;
      }
 
if(jugador.x <2)
      {
     
gotoxy(jugador.x,jugador.y);
     
printf ("         ");
     
jugador.x= ANCHO_PANTALLA- jugador.ancho;
      }
  }
 
jugador.contador++;
}


void MoverMisil()
{
 
misil.contador++;
 
if (misil.contador>30)
  {
  
misil.contador=0;
  
gotoxy(misil.x,misil.y);
  
printf(" ");
   (
misil.y)--;
  }
 
if ((misil.x> nave.x && misil.x <nave.x+nave.ancho) && misil.y==nave.y )
 {
  
puntaje+=VALOR_NAVE;
  
bExplosion=SI;
  
nave.direccion=QUIETO;
  }
 }

void MostrarPuntaje()
{
 
gotoxy(1,1);
 
printf("PUNTAJE : %d",puntaje);
 
if (puntaje>1000)
  {
   
bGanador=SI;
   
bSalida=SI;
  }
}
void Terminar()
{
 
if(bGanador==SI)
 {
  
gotoxy(8,4);
  
printf("REALMENTE ERES UN GENIO, GANASTE");
  
bSalida=SI;
 }
 
if(bPerdedor==SI)
  {
    
gotoxy(4,4);
    
printf( "SIGUE PARTICIPANDO LA PROXIMA LO HARAS MEJOR");
    
bSalida=SI;
   }
}