import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import java.lang.*;

public class Spaceship4 extends Applet implements Runnable
{
 Random rand = new Random();
 int speed = 2;
 int x = 40;
 int y = 200;
 int a = 200;
 int b = 600 + 800;
 int score = 0;
 int highscore = 0;

 Thread main = new Thread(this);
 Button up = new Button("     ^     ");
 Button down = new Button("     v     ");
 Label points = new Label("Score: " + score);
 Label highpoints = new Label("High Score: " + highscore);
 Button restart = new Button("Restart");

 public void init()
 {
  add(up);
  add(down);
  add(points);
  add(highpoints);
  add(restart);
  main.start();
 }

 public void run()
 {
  while (main != null)
  {
   try
   {
    main.sleep(speed);
   }
   catch (Exception e){}
    {b--;}
   repaint();
   }
  }


public boolean action (Event ev, Object args)
{
 if (ev.target == up)
  {
   y -= 10;
   repaint();
  }

 if (ev.target == down)
  {
   y += 10;
   repaint();
  }

 if (ev.target == restart)
  {
   speed = 2;
   x = 40;
   y = 200;
   a = 200;
   b = 600 + 800;
   score = 0;

  points.setText("Score: " + score);

   repaint();
  }
 return true;
}

public void paint (Graphics g)
{

 setBackground (Color.black);

 g.setColor (Color.green);
 if (y < 400 - 40)
 	{g.fillOval(x, y, 80, 40);}
 else
 {
 	y--;
 }
 g.setColor (Color.green);
 if (y > 0)
 	{g.fillOval(x, y, 80, 40);}
 else
 {
 	y++;
 }

// Collision Statement

 if (
     // Quadrant I
     b <= x + 80 && a <= y + 40 && b >= x && a >= y ||
     // Quadrant II
     b >= x - 80 && a >= y - 40 && b <= x && a <= y ||
     // Quadrant III
     b >= x - 80 && a <= y + 40 && b <= x && a >= y ||
     // Quadrant IV
     b <= x + 80 && a >= y - 80 && b >= x && a <= y
    )
 {
  setBackground (Color.red);
  score = 0;

  points.setText("Score: " + score);

  speed = 2;
  x = 40;
  y = 200;
  a = 200;
  b = 600 + 800;
 }

 g.setColor (Color.blue);
 if (b > 0 - 80)
 	{g.fillOval(b, a, 80, 80);}
 else
 {      score++;

	points.setText("Score: " + score);

        b = 600 + 80;
	a = rand.nextInt(400) - 40;
	speed = rand.nextInt(3);

	if (speed == 0)
		{speed = 2;}

	if (score > highscore)
	{
	 highscore = score;
	 highpoints.setText("High Score: " + score);
	}



	repaint();
 }

}
}
