import java.awt.*;
import java.applet.*;

public class Catch_the_Enemy extends Applet implements Runnable {
  Thread kick;
  Font normalF, goF;
  String enemyStr = "o";
  String allyStr = "xx";
  Fall_String enemy[] = new Fall_String[10];   //enemy$B%*%V%8%'%/%H(B
  int enemyFlg[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  int width = 0, height = 0;   //$B2hLL$N2#=D(B
  int mouseX = 0;   //$B%^%&%9%]%$%s%?$N(BX$B:BI8(B
  int get = 0, lost = 0;
  Game_Over go;   //$B%2!<%`%*!<%P!<=hM}%*%V%8%'%/%H(B
  Button start_button;   //$B%\%?%s%*%V%8%'%/%H(B
  int startFlg = 0;   //$B%\%?%s2!2<$K$h$k%9%?!<%HH=Dj%U%i%0(B

  public void init() {
    /* $BGX7J?'$N@_Dj(B */
    this.setBackground(Color.black);
    /* $B2hLL$N2#=D$N@_Dj(B */
    width = size().width;
    height = size().height;
    /* $B%U%)%s%H$N@_Dj(B */
    normalF = new Font("TimesRoman", Font.PLAIN, 20);
    goF = new Font("TimesRoman", Font.ITALIC, 30);
    /* $B%\%?%s$N@_Dj(B */
    start_button = new Button("START");
    start_button.setForeground(Color.black);
    start_button.setBackground(Color.white);
    this.add(start_button);
  }

  public void start() {
    if (kick == null) {
      kick = new Thread(this);
      kick.start();
      for (int i = 0; i < (int)(1 + get / 10); i++) {   //$B3MF@%]%$%s%H$K0MB8$7$F=P$FMh$k(Benemy$B?t$r@)8f(B
	if (enemy[i] == null) {
	  enemy[i] = new Fall_String(width, height);
	  enemy[i].start();
	}
      }
    }
  }

  public void run() {
    while (true) {   //$B%9%l%C%I$@$+$iL58B%k!<%W(B
      if(startFlg == 1) {   //$B%9%?!<%H%\%?%s2!$5$l$?$i%9%?!<%H(B
	for (int i = 0; i < (int)(1 + get / 10); i++) {   //$B3MF@%]%$%s%H$K0MB8$7$F=P$FMh$k(Benemy$B?t$r@)8f(B
	  if (enemy[i] == null) {
	    enemy[i] = new Fall_String(width, height);
	    enemy[i].start();
	  }
	}
	if (lost == 5) {   //$B%2!<%`%*!<%P!<=hM}(B
	  for (int i = 0; i < (int)(1 + get / 10); i++) {
	    if (enemy[i] != null)
	      enemy[i].stop();
	  }
	  if (go == null) {
	    go = new Game_Over(width, height);
	    go.start();
	  }
	}
	repaint();
	deflag();   //enemy$B%*%V%8%'%/%H$,0lHV2<$^$G$$$C$?$iN)$F$k%U%i%0$r2<$2$k(B
	try {
	  Thread.sleep(100);
	} catch (InterruptedException e) {
	}
      }
    }
  }

  /* $B%^%&%9$NF0:n$K9g$o$;$F(Bally$B%*%V%8%'%/%H$rF0$+$9(B */
  public boolean mouseMove (Event e, int x, int y) {
    if (x < 5) {
      mouseX = 5;
    } else if (x > width - 5) {
      mouseX = width - 5;
    } else {
      mouseX = x;
    }
    return true;
  }

  /* enemy$B%*%V%8%'%/%H$,0lHV2<$^$G$$$C$?$iN)$F$k%U%i%0$r2<$2$k(B */
  public void deflag() {
    for (int i = 0; i < (int)(1 + get / 10); i++) {
      if (enemy[i] != null) {
	if (enemy[i].getY() < height - 100) {
	  enemyFlg[i] = 0;
	}
      }
    }
  }

  /* start$B%\%?%s$,2!$5$l$?$H$-$K8F$P$l$k(B */
  public boolean action (Event event, Object arg) {
    if (event.target == start_button) {
      startFlg = 1;
      return true;
    } else return super.action(event, arg);
  }

  public void paint(Graphics g) {
    g.setFont(normalF);
    g.setColor(Color.white);   //$B0J2<(Benemy$B=hM}(B
    for (int i = 0; i < (int)(1 + get / 10); i++) {   //$BA4(Benemy$B$KBP$7$F=hM}$r9T$&(B
      if (enemy[i] != null) {   //null$B%]%$%s%?%(%i!<2sHr$N$?$a(B
	g.drawString(enemyStr, enemy[i].getX(), enemy[i].getY());
	if (enemy[i].getY() > height - 20) {   //enemy$B$,0lHV2<$^$GCe$$$?$H$-$N=hM}(B
	  if (enemyFlg[i] == 0) {
	    if (enemy[i].getX() > mouseX - 20 &&
		enemy[i].getX() < mouseX + 20) {   //$B%-%c%C%A$G$-$?$H$-$N=hM}(B
	      g.setColor(Color.green);
	      g.drawString("GOOD!!", enemy[i].getX(), enemy[i].getY());
	      get++;
	    } else {   //$B%-%c%C%A$G$-$J$+$C$?$H$-$N=hM}(B
	      g.setColor(Color.red);
	      g.drawString("BAD!!", enemy[i].getX(), enemy[i].getY());
	      lost++;
	    }
	    enemyFlg[i]++;
	  }
	}
      }
    }
    g.setColor(Color.yellow);   //$B0J2<(Bally$B=hM}(B
    g.drawString(allyStr, mouseX, height - 10);
    g.setColor(Color.green);   //$B0J2<%9%3%"=hM}(B
    g.drawString("Get : " + Integer.toString(get), 10, 20);
    g.setColor(Color.red);
    g.drawString("Lost : " + Integer.toString(lost), 150, 20);
    if (lost == 5) {   //$B%2!<%`%*!<%P!<=hM}(B
      g.setFont(goF);
      g.setColor(Color.cyan);
      g.drawString("Game Over!!", go.getX(), go.getY());
    }
  }

  public void stop() {
    if (kick != null) {
      kick.stop();
      kick = null;
    }
  }
}

