//John Marquiss
//Due Wednesday, April 29, 1998
//Play my game, bet ya can't beat me 'mono e mono'...
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SPBattle extends Applet implements KeyListener{

   private Image CartmanFront, CartmanBack, CartmanLeft, CartmanRight,
                 CartmanMad, CartmanDead, CartmanPainLeft, CartmanPainRight,
                 CartmanPainUp, CartmanPainDown;
   private AudioClip CartmanFartSound, CartmanHurtSound, CartmanDeadSound,
                     CartmanPoofSound;
   private int CartmanXPos = 177;
   private int CartmanYPos = 177;
   private int CartmanPicture = 2;
   private int CartmanLastPicture = 0;
   private float CartmanHealth = 100;
   private int CartmanMove = 3;
   private final int CartmanNumber = 1;
   private boolean IsCartmanAlive = true;

   private Image KennyFront, KennyBack, KennyLeft, KennyRight, KennyMad, KennyDead;
   private AudioClip KennyFartSound, KennyHurtSound, KennyDeadSound,
                     KennyPoofSound;
   private int KennyXPos = 81;
   private int KennyYPos = 83;
   private int KennyPicture = 2;
   private float KennyHealth = 100;
   private int KennyMove = 5;
   private final int KennyNumber = 2;
   private boolean IsKennyAlive = true;

   private Image Bullet1, Bullet2;
   private int BulletXPos = 0;
   private int BulletYPos = 0;
   private int BulletDirection = 0;
   private int BulletMove = 5;
   private int BulletDamage = 20;

   private Image FireFartLeft, FireFartRight, FireFartUp, FireFartDown;
   private int FireFartXPos = 0;
   private int FireFartYPos = 0;
   private int FireFartDirection = 0;
   private int FireFartDamage = 4;
   private int FireFartIncrement = 0;

   private Image Poof;
   private AudioClip PoofSound;
   private int PoofXPos = (int) ( Math.random() * 250 );
   private int PoofYPos = (int) ( Math.random() * 250 );
   private int PoofHealth = 25;

   private String S1 = "";
   private int sleepTime = 50;
   private boolean flag = true;
   private boolean flag2 = false;
   private boolean flag3 = true;
   private int Increment = 0;

   public void init()
   {
      CartmanFront = getImage( getDocumentBase(), "CFront.gif" );
      CartmanBack = getImage( getDocumentBase(), "CBack.gif" );
      CartmanLeft = getImage( getDocumentBase(), "CLeft.gif" );
      CartmanRight = getImage( getDocumentBase(), "CRight.gif" );
      CartmanPainLeft = getImage( getDocumentBase(), "CFLeft.gif" );
      CartmanPainRight = getImage( getDocumentBase(), "CFRight.gif" );
      CartmanPainDown = getImage( getDocumentBase(), "CBack.gif" );
      CartmanPainUp = getImage( getDocumentBase(), "CFLeft.gif" );
      CartmanMad = getImage( getDocumentBase(), "CMad.gif" );
      CartmanDead = getImage( getDocumentBase(), "CDead.gif" );
      CartmanFartSound = getAudioClip( getDocumentBase(), "CFire.au" );
      CartmanHurtSound = getAudioClip( getDocumentBase(), "CHurt.au" );
      CartmanPoofSound = getAudioClip( getDocumentBase(), "CPoof.au" );
      CartmanDeadSound = getAudioClip( getDocumentBase(), "CDead.au" );

      KennyFront = getImage( getDocumentBase(), "KFront.gif" );
      KennyBack = getImage( getDocumentBase(), "KBack.gif" );
      KennyLeft = getImage( getDocumentBase(), "KLeft.gif" );
      KennyRight = getImage( getDocumentBase(), "KRight.gif" );
      KennyMad = getImage( getDocumentBase(), "KMad.gif" );
      KennyDead = getImage( getDocumentBase(), "KDead.gif" );
      KennyFartSound = getAudioClip( getDocumentBase(), "KFire.au" );
      KennyHurtSound = getAudioClip( getDocumentBase(), "KHurt.au" );
      KennyPoofSound = getAudioClip( getDocumentBase(), "CDead.au" );
      KennyDeadSound = getAudioClip( getDocumentBase(), "KDead.au" );

      Bullet1 = getImage( getDocumentBase(), "Bullet1.gif" );
      Bullet2 = getImage( getDocumentBase(), "Bullet2.gif" );

      FireFartLeft = getImage( getDocumentBase(), "CFart1.gif" );
      FireFartRight = getImage( getDocumentBase(), "CFart2.gif" );
      FireFartUp = getImage( getDocumentBase(), "CFart3.gif" );
      FireFartDown = getImage( getDocumentBase(), "CFart4.gif" );

      Poof = getImage( getDocumentBase(), "ChPoof.gif" );
      PoofSound = getAudioClip( getDocumentBase(), "Poof.au" );

      addKeyListener( this );
      requestFocus();

   }

   public void paint( Graphics g )
   {
//Load all of the images into cache...
      if ( flag )
      {
         g.drawImage( Bullet1, 150, 150, this);
         g.drawImage( Bullet2, 150, 150, this);
         g.drawImage( FireFartLeft, 150, 150, this);
         g.drawImage( FireFartRight, 150, 150, this);
         g.drawImage( FireFartDown, 150, 150, this);
         g.drawImage( FireFartUp, 150, 150, this);
         g.drawImage( KennyBack, 150, 150, this);
         g.drawImage( KennyFront, 150, 150, this);
         g.drawImage( KennyLeft, 150, 150, this);
         g.drawImage( KennyRight, 150, 150, this);
         g.drawImage( KennyMad, 150, 150, this);
         g.drawImage( KennyDead, 150, 150, this);
         g.drawImage( Poof, 150, 150, this);
         g.drawImage( CartmanFront, 150, 150, this);
         g.drawImage( CartmanLeft, 150, 150, this);
         g.drawImage( CartmanRight, 150, 150, this);
         g.drawImage( CartmanPainLeft, 150, 150, this);
         g.drawImage( CartmanPainRight, 150, 150, this);
         g.drawImage( CartmanMad, 150, 150, this);
         g.drawImage( CartmanDead, 150, 150, this);
         g.drawImage( CartmanBack, 150, 150, this);
         g.setColor( Color.white );
         g.fillRect( 150, 150, 50, 50);
         flag = false;
      }

      flag2 = false;
      Increment++;
      FireFartIncrement--;
      if (FireFartIncrement == 0)
         FireFartDirection = 0;

// The Bullet Hits Cartman...
      if ( ( ( ( BulletDirection == 3 ) || ( BulletDirection == 4 ) ) &&
      ( ( BulletYPos > CartmanYPos - 7 ) && ( BulletYPos < CartmanYPos + 32 ) )
      && ( ( BulletXPos > CartmanXPos - 13 ) && ( BulletXPos < CartmanXPos + 31 ) ) ) 
      || (   ( ( BulletDirection == 1 ) || ( BulletDirection == 2 ) ) &&
      ( ( BulletYPos > CartmanYPos - 13 ) && ( BulletYPos < CartmanYPos + 32 ) )
      && ( ( BulletXPos > CartmanXPos - 7 ) && ( BulletXPos < CartmanXPos + 31 ) ) ) )
      {
         BulletYPos = 0;
         BulletXPos = 0;
         BulletDirection = 0;
         CartmanHealth = CartmanHealth - BulletDamage;
         if ( CartmanHealth <=0 )
            IsCartmanAlive = false;
         else {
            CartmanHurtSound.play();
            CartmanPicture = 7; }
      }

// The Fire Fart Hits Kenny...
      if ( ( ( ( FireFartDirection == 1 ) || ( FireFartDirection == 2 ) ) &&
      ( ( FireFartYPos > KennyYPos - 16 ) && ( FireFartYPos < KennyYPos + 32 ) )
      && ( ( FireFartXPos > KennyXPos - 25 ) && ( FireFartXPos < KennyXPos + 25 ) ) ) 
      || ( ( ( FireFartDirection == 3 ) || ( FireFartDirection == 4 ) ) &&
      ( ( FireFartYPos > KennyYPos - 26 ) && ( FireFartYPos < KennyYPos + 32 ) ) &&
      ( ( FireFartXPos > KennyXPos - 17 ) && ( FireFartXPos < KennyXPos + 25 ) ) ) )
      {
         KennyHealth = KennyHealth - FireFartDamage;
         if ( KennyHealth <=0 )
            IsKennyAlive = false;
         else {
            KennyHurtSound.play();
            KennyPicture = 5;
            }
      }

// Cartman gets the Cheesy Poof...
      if ( ( CartmanHealth < 100 ) &&
      ( ( ( PoofYPos > CartmanYPos - 32 ) && ( PoofYPos < CartmanYPos + 32 ) )
      && ( ( PoofXPos > CartmanXPos - 25 ) && ( PoofXPos < CartmanXPos + 31 ) ) ) )
      {
         Increment = 0;
         PoofXPos = (int) ( Math.random() * 250 );
         PoofYPos = (int) ( Math.random() * 250 );
         CartmanHealth = CartmanHealth + PoofHealth;
         if ( CartmanHealth > 100.0 )
            CartmanHealth = 100;
         CartmanPoofSound.play();
      }

// Kenny gets the Cheesy Poof...
      if ( ( KennyHealth < 100 ) &&
      ( ( ( PoofYPos > KennyYPos - 32 ) && ( PoofYPos < KennyYPos + 32 ) )
      && ( ( PoofXPos > KennyXPos - 25 ) && ( PoofXPos < KennyXPos + 25 ) ) ) )
      {
         Increment = 0;
         PoofXPos = (int) ( Math.random() * 250 );
         PoofYPos = (int) ( Math.random() * 250 );
         KennyHealth = KennyHealth + PoofHealth;
         if ( KennyHealth > 100.0 ) 
            KennyHealth = 100;
         KennyPoofSound.play();
      }

// Cartman Dies...
      if ( ( !IsCartmanAlive ) && ( flag3 ) )
      {
         CartmanPicture = 8;
         showStatus( "Kenny Wins!!!" );
         CartmanDeadSound.play();
         flag3 = false;
      }

// Kenny Dies...
      if ( ( !IsKennyAlive ) && ( flag3 ) )
      {
         KennyPicture = 6;
         showStatus( "Cartman Wins!!!" );
         KennyDeadSound.play();
         flag3 = false;
      }

// The Bullet Goes Off the Screen...
      if ( BulletXPos < 0 || BulletXPos > 280 || BulletYPos < 0 || BulletYPos > 280 )
      {
         BulletXPos = 0;
         BulletYPos = 0;
         BulletDirection = 0;
      }

//Draw the CheesyPoof
      if (Increment > 150 ) {
         g.drawImage( Poof, PoofXPos, PoofYPos, this); 
         PoofSound.play(); }

//Draw Cartman...
      if (CartmanPicture == 1)
         g.drawImage( CartmanBack, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 2)
         g.drawImage( CartmanFront, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 3)
         g.drawImage( CartmanLeft, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 4)
         g.drawImage( CartmanRight, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 5)
         g.drawImage( CartmanPainLeft, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 6)
         g.drawImage( CartmanPainRight, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 7)
         g.drawImage( CartmanMad, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 8)
         g.drawImage( CartmanDead, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 9)
         g.drawImage( CartmanPainUp, CartmanXPos, CartmanYPos, this);
      else if (CartmanPicture == 10)
         g.drawImage( CartmanPainDown, CartmanXPos, CartmanYPos, this);


//Draw Kenny...
      if (KennyPicture == 1)
         g.drawImage( KennyBack, KennyXPos, KennyYPos, this);
      else if (KennyPicture == 2)
         g.drawImage( KennyFront, KennyXPos, KennyYPos, this);
      else if (KennyPicture == 3)
         g.drawImage( KennyLeft, KennyXPos, KennyYPos, this);
      else if (KennyPicture == 4)
         g.drawImage( KennyRight, KennyXPos, KennyYPos, this);
      else if (KennyPicture == 5)
         g.drawImage( KennyMad, KennyXPos, KennyYPos, this);
      else if (KennyPicture == 6)
         g.drawImage( KennyDead, KennyXPos, KennyYPos, this);

//Draw Cartman's "weapon"...
      if (FireFartDirection == 1)
         {
            g.drawImage( FireFartDown, FireFartXPos, FireFartYPos, this);
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
            CartmanPicture = 9;
         }
      else if (FireFartDirection == 2)
         {
            g.drawImage( FireFartUp, FireFartXPos, FireFartYPos, this);
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
            CartmanPicture = 10;
         }
      else if (FireFartDirection == 3)
         {
            g.drawImage( FireFartLeft, FireFartXPos, FireFartYPos, this);
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
            CartmanPicture = 5;
         }
      else if (FireFartDirection == 4)
         {
            g.drawImage( FireFartRight, FireFartXPos, FireFartYPos, this);
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
            CartmanPicture = 6;
         }

//Draw Kenny's 'weapon'...
      if (BulletDirection == 1)
         {
            g.drawImage( Bullet2, BulletXPos, BulletYPos, this);
            BulletYPos = BulletYPos - BulletMove;
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
         }
      else if (BulletDirection == 2)
         {
            g.drawImage( Bullet2, BulletXPos, BulletYPos, this);
            BulletYPos = BulletYPos + BulletMove;
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
         }
      else if (BulletDirection == 3)
         {
            g.drawImage( Bullet1, BulletXPos, BulletYPos, this);
            BulletXPos = BulletXPos - BulletMove;
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
         }
      else if (BulletDirection == 4)
         {
            g.drawImage( Bullet1, BulletXPos, BulletYPos, this);
            BulletXPos = BulletXPos + BulletMove;
            
            try { Thread.sleep( sleepTime ); }
            catch ( InterruptedException e ) 
            { showStatus( e.toString() ); }

            flag2 = true;
         }

//Draw Cartman's Health Bar
      int p = ( int ) ( ( CartmanHealth * 24.0 ) / 100.0 );
      g.setColor( Color.black );
      g.drawRect( CartmanXPos + 3, CartmanYPos - 12, 24, 8 );
      g.setColor( Color.blue );
      g.fillRect( CartmanXPos + 3, CartmanYPos - 12, p, 8 );

//Draw Kenny's Health Bar
      p = (int) ( ( KennyHealth * 24.0 ) / 100.0 );
      g.setColor( Color.black );
      g.drawRect( KennyXPos, KennyYPos - 12, 24, 8 );
      g.setColor( Color.red );
      g.fillRect( KennyXPos, KennyYPos - 12, p, 8 );

      if ( flag2 ) repaint();
   }

   public void keyPressed( KeyEvent e )
   {
      S1 = e.getKeyText( e.getKeyCode() );

      if ( ( S1.equals( "Up" ) ) && ( CartmanYPos >= 5 ) && ( IsCartmanAlive ) )
         {
            CartmanPicture = 1;
            FireFartDirection = 0;
            if ( IsObjectAbove( CartmanNumber ) )
               CartmanYPos -= CartmanMove;
         }
      else if ( ( S1.equals( "Down" ) ) && ( CartmanYPos <= 240 ) && ( IsCartmanAlive ) )
         {
            CartmanPicture = 2;
            FireFartDirection = 0;
            if ( IsObjectBelow( CartmanNumber ) )
               CartmanYPos += CartmanMove;
         }
      else if ( ( S1.equals( "Left" ) ) && ( CartmanXPos >= 5 ) && ( IsCartmanAlive ) )
         {
            CartmanPicture = 3;
            FireFartDirection = 0;
            if ( IsObjectLeft( CartmanNumber ) )
               CartmanXPos -= CartmanMove;
         }
      else if ( ( S1.equals( "Right" ) ) && ( CartmanXPos <= 240 ) && ( IsCartmanAlive ) )
         {
            CartmanPicture = 4;
            FireFartDirection = 0;
            if ( IsObjectRight( CartmanNumber ) )
               CartmanXPos += CartmanMove;
         }
      else if ( S1.equals( "L" ) && ( IsCartmanAlive ) )
         {
            if (CartmanPicture == 1) {
               CartmanLastPicture = CartmanPicture;
               FireFartDirection = 1;
               FireFartXPos = CartmanXPos + 13;
               FireFartYPos = CartmanYPos - 12;
               FireFartIncrement = 27;
               CartmanFartSound.play();
               }
            else if (CartmanPicture == 2) {
               CartmanLastPicture = CartmanPicture;
               FireFartDirection = 2;
               FireFartXPos = CartmanXPos + 6;
               FireFartYPos = CartmanYPos + 26;
               FireFartIncrement = 27;
               CartmanFartSound.play();
               }
            else if (CartmanPicture == 3) {
               CartmanLastPicture = CartmanPicture;
               FireFartDirection = 3;
               FireFartXPos = CartmanXPos - 22;
               FireFartYPos = CartmanYPos + 26;
               FireFartIncrement = 27;
               CartmanFartSound.play();
               }
            else if (CartmanPicture == 4) {
               CartmanLastPicture = CartmanPicture;
               FireFartDirection = 4;
               FireFartXPos = CartmanXPos + 28;
               FireFartYPos = CartmanYPos + 26;
               FireFartIncrement = 27;
               CartmanFartSound.play();
               }
            else {
               FireFartDirection = CartmanLastPicture;
               FireFartIncrement = 27; 
               CartmanFartSound.play(); }
            }

      else if ( ( S1.equals( "W" ) ) && ( KennyYPos >= 5 ) && ( IsKennyAlive ) )
         {
            KennyPicture = 1;
            if ( IsObjectAbove( KennyNumber ) )
               KennyYPos -= KennyMove;
         }
      else if ( ( S1.equals( "S" ) ) && ( KennyYPos <= 240 ) && ( IsKennyAlive ) )
         {
            KennyPicture = 2;
            if ( IsObjectBelow( KennyNumber ) )
               KennyYPos += KennyMove;
         }
      else if ( ( S1.equals( "A" ) ) && ( KennyXPos >= 5 ) && ( IsKennyAlive ) )
         {
            KennyPicture = 3;
            if ( IsObjectLeft( KennyNumber ) )
               KennyXPos -= KennyMove;
         }
      else if ( ( S1.equals( "D" ) ) && ( KennyXPos <= 240 ) && ( IsKennyAlive ) )
         {
            KennyPicture = 4;
            if ( IsObjectRight( KennyNumber ) )
               KennyXPos += KennyMove;
            }
      else if ( S1.equals( "F" ) && ( IsKennyAlive ) && ( BulletDirection == 0 ) )
         {
            BulletDirection = KennyPicture;
            KennyFartSound.play();

            if( KennyPicture == 1 ) {
               BulletXPos = KennyXPos + 9;
               BulletYPos = KennyYPos - 13; 
               KennyPicture = 2; }
            else if( KennyPicture == 2 ) {
               BulletXPos = KennyXPos + 9;
               BulletYPos = KennyYPos + 32; 
               KennyPicture = 1; }
            else if( KennyPicture == 3 ) {
               BulletXPos = KennyXPos - 13;
               BulletYPos = KennyYPos + 22; 
               KennyPicture = 4; }
            else if( KennyPicture == 4 ) {
               BulletXPos = KennyXPos + 25;
               BulletYPos = KennyYPos + 22; 
               KennyPicture = 3; }
         }

      repaint();
   }

   public boolean IsObjectAbove( int player )
   {
      if ( ( player == 1 ) &&
      ( (CartmanXPos + 31 > KennyXPos) && (CartmanXPos < KennyXPos + 25) ) &&
      ( (CartmanYPos <= KennyYPos + 32 + 6 ) && (CartmanYPos >= KennyYPos + 32 ) ) )
           return false;

      else if ( ( player == 2 ) &&
      ( (CartmanXPos + 31 > KennyXPos) && (CartmanXPos < KennyXPos + 25) ) &&
      ( (KennyYPos <= CartmanYPos + 32 + 6 ) && (KennyYPos >= CartmanYPos + 32 ) ) )
           return false;
      else return true;
   }

   public boolean IsObjectBelow( int player )
   {
      if ( ( player == 1 ) &&
      ( (CartmanXPos + 31 > KennyXPos) && (CartmanXPos < KennyXPos + 25) ) &&
      ( (CartmanYPos + 32 >= KennyYPos - 6) && (CartmanYPos + 32 <= KennyYPos) ) )
           return false;

      else if ( ( player == 2 ) &&
      ( (CartmanXPos + 31 > KennyXPos) && (CartmanXPos < KennyXPos + 25) ) &&
      ( (KennyYPos + 32 >= CartmanYPos - 6) && (KennyYPos + 32 <= CartmanYPos) ) )
           return false;
      else return true;
   }

   public boolean IsObjectLeft( int player )
   {
      if ( ( player == 1 ) &&
      ( (KennyYPos + 32 >= CartmanYPos) && (KennyYPos <= CartmanYPos + 32) ) &&
      ( (KennyXPos + 25 >= CartmanXPos - 6) && (KennyXPos + 25 <= CartmanXPos) ) )
           return false;

      else if ( ( player == 2 ) &&
      ( (CartmanYPos + 32 >= KennyYPos) && (CartmanYPos <= KennyYPos + 32) ) &&
      ( (CartmanXPos + 32 >= KennyXPos - 6) && (CartmanXPos + 32 <= KennyXPos) ) )
           return false;
      else return true;
   }

   public boolean IsObjectRight( int player )
   {
      if ( ( player == 1 ) &&
      ( (CartmanYPos + 32 >= KennyYPos) && (CartmanYPos <= KennyYPos + 32) ) &&
      ( (CartmanXPos + 32 >= KennyXPos - 6) && (CartmanXPos + 32 <= KennyXPos) ) )
           return false;

      else if ( ( player == 2 ) &&
      ( (KennyYPos + 32 >= CartmanYPos) && (KennyYPos <= CartmanYPos + 32) ) &&
      ( (KennyXPos + 25 >= CartmanXPos - 6) && (KennyXPos + 25 <= CartmanXPos) ) )
           return false;
      else return true;
   }

   public void keyTyped( KeyEvent e ) { }
   public void keyReleased( KeyEvent e ) { }
}