// Author = Alexander Stuy  10/5/97


//******************************************************************************
// App5_1.java: Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;



class Bullet
{
  int px, py;

  void draw(Graphics g)
  {
    g.setColor(Color.green);
    g.fillRect((px - 1), (py - 1), 4, 6);
  }

  int get_px()
  {
    return px;
  }

  int get_py()
  {
    return py;
  }

  void Start_Off()
  {
    px = 149;
    py = 212;
  }

  void Update_Pos(int fhx, int fhy)
  {
    if (py == 0)
    {
      Start_Off();
      return;
    }
    if ((px >= fhx) && (px <= (fhx + 20)) &&
        (py >= fhy) && (py <= (fhy + 30)))
    {
      Start_Off();
      return;
    }
    py -= 1;
  }

  
  void init()
  {
    Start_Off();
  }
}


class Floating_Head
{ 
  int px, py, x_Cond, y_Cond;
  int Ouch, Dead;

  Floating_Head(int startX, int startY)
  {
    px = startX;
    py = startY;
    x_Cond = 1;
    y_Cond = 1;
    Ouch = 0;
    Dead = 0;
  }
  
  int get_x()
  {
    return px;
  }

  int get_y()
  {
    return py;
  }

  void draw(Graphics g)
  {

    if (Dead > 0) 
    {  
      g.setColor(Color.red);
      g.drawRoundRect(px, py, 20, 30, 20, 20);
      g.setColor(Color.blue);
      g.drawLine((px + 3), (py + 5), (px + 9), (py + 11));
      g.drawLine((px + 3), (py + 11),(px + 9), (py + 5));
      g.drawLine((px + 11), (py + 5), (px + 17), (py + 11));
      g.drawLine((px + 11), (py + 11),(px + 17), (py + 5));
      g.setColor(Color.red);
      g.drawRoundRect((px + 8), (py + 13), 4, 6, 3, 3);
      g.fillRoundRect((px + 4), (py + 23), 12, 5, 3, 3);
      return;
    }

    if (Ouch > 0)
    {
      g.setColor(Color.red);
      g.drawRoundRect(px, py, 20, 30, 20, 20);
      g.setColor(Color.blue);
      g.fillRoundRect((px + 3), (py + 5), 6, 10, 3, 3);
      g.fillRoundRect((px + 11), (py + 5), 6, 10, 3, 3); 
      g.setColor(Color.red);
      g.drawRoundRect((px + 8), (py + 13), 4, 6, 3, 3);
      g.fillRoundRect((px + 4), (py + 23), 12, 5, 3, 3);
      return;  
    }
    
    g.setColor(Color.red);
    g.drawRoundRect(px, py, 20, 30, 20, 20);
    g.setColor(Color.blue);
    g.fillRoundRect((px + 3), (py + 8), 6, 4, 3, 3);
    g.fillRoundRect((px + 11), (py + 8), 6, 4, 3, 3);
    g.setColor(Color.red);
    g.drawRoundRect((px + 8), (py + 13), 4, 6, 3, 3);
    g.drawRoundRect((px + 4), (py + 23), 12, 1, 1, 1);
          
  }

  void Update_Pos(int MaxX, int MaxY, int bx, int by)
  {
    
    Ouch -= 1;
    Dead -= 1;
    if ((px > (MaxX - 22)) || (px < 3))
    {
      Ouch = 10;
      x_Cond = 1 - x_Cond;
    }
    if ((py > (MaxY - 32)) || (py < 3))
    {
      Ouch = 10; 
      y_Cond = 1 - y_Cond;
    } 
    if (x_Cond == 1)
      px += 1;
    else
      px -= 1;
    if (y_Cond == 1)
      py += 1;
    else
      py -= 1;
    
    if ((bx >= px) && (bx <= (px + 20)) &&
        (by >= py) && (by <= (py + 30)))
      Dead = 20;

  }


  void init()
  {
    x_Cond = 1;
    y_Cond = 1;
    Ouch = 0;
    Dead = 0;
  }

}


class Gun
{
  void draw(Graphics g)
  {
    g.setColor(Color.yellow); 
    g.fillRect(130, 220, 40, 15);
    g.drawRect(149, 212, 2, 8);
  }
}


//==============================================================================
// Main Class for applet App5_1
//
//==============================================================================
public class App5_1 extends Applet implements Runnable
{
  
  Thread m_Animation = null;
  public int MaxX, MaxY;
  public int nSleepValue = 10;
  
  Floating_Head m_FH = new Floating_Head(70, 40);
  Bullet m_Bullet = new Bullet();
  Gun m_Gun = new Gun();

  // App4_2 Class Constructor
  //--------------------------------------------------------------------------
  public App5_1()
  {
    // constructor code would go here
  }

        
  public String getAppletInfo()
  {
    return "Name: App5_1\r\n" +
    "Author: Alexander Stuy\r\n";
  }


  public void init()
  {
    resize(300, 250);
    MaxX = 300;
    MaxY = 200;   
  }


  public void destroy()
  {
    // applet cleanup code would go here
  }

        
  public void paint(Graphics g)
  {
    m_Gun.draw(g);
    m_FH.draw(g);
    m_Bullet.draw(g);
  }
       
  public void start()
  {
    if (m_Animation == null)
    {
      m_Animation = new Thread(this);
      m_Animation.start();
    }
    // additional applet start code would go here
  }
        
      
  public void stop()
  {
    if (m_Animation != null)
    {
      m_Animation.stop();
      m_Animation = null;
    } 
  }

  public void run()
  {
    while (true)
    {
      try
      {
        repaint();
        Thread.sleep(nSleepValue);
        m_FH.Update_Pos(MaxX, MaxY, m_Bullet.get_px(), m_Bullet.get_py());
        m_Bullet.Update_Pos(m_FH.get_x(), m_FH.get_y());
      }
      catch (InterruptedException e)
      {
        stop();
      }
    }
  }


}