import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class movementTest extends Applet implements KeyListener
{

 int win = 0;
 int goal = 10;
 int move = 10;

 int y = 200;
 int x = 50;
 int a = 700 - 60;
 int b = 200;
 boolean touch = false;

 int scorea = 0;
 int scoreb = 0;

 public void init()
 {
  addKeyListener(this);
 }

 public void keyPressed(KeyEvent e)
 {
 if (e.getKeyChar() == 'd')
 {
  x += move;
  touching();
  scoreaUP();
  repaint();
 }

 if (e.getKeyChar() == 'a')
 {
  x -= move;
  touching();
  scoreaUP();
  repaint();
 }

  if (e.getKeyChar() == 'w')
 {
  y -= move;
  touching();
  scoreaUP();
  repaint();
 }

 if (e.getKeyChar() == 's')
 {
  y += move;
  touching();
  scoreaUP();
  repaint();
 }

 if (e.getKeyCode() == KeyEvent.VK_LEFT)
 {
  a -= move;
  touching();
  scorebUP();
  repaint();
 }

 if (e.getKeyCode() == KeyEvent.VK_RIGHT)
 {
  a += move;
  touching();
  scorebUP();
  repaint();
 }

 if (e.getKeyCode() == KeyEvent.VK_UP)
 {
  b -= move;
  touching();
  scorebUP();
  repaint();
 }

 if (e.getKeyCode() == KeyEvent.VK_DOWN)
 {
  b += move;
  touching();
  scorebUP();
  repaint();
 }
 }

 public void scoreaUP()
 {
  if (touch == true)
  {
   scorea++;
   scoreCheck();
   repaint();
  }
 }

 public void scorebUP()
 {
  if (touch == true)
  {
   scoreb++;
   scoreCheck();
   repaint();
  }
 }


 public void scoreCheck()
 {
  if (scorea < 0)
  {
   scorea = 0;
   repaint();
  }

  if (scoreb < 0)
  {
   scoreb = 0;
   repaint();
  }

  if (scorea == goal)
  {
   reset();
   win = 1;
   repaint();
  }

  if (scoreb == goal)
  {
   reset();
   win = 2;
   repaint();
  }
 }

 public void touching()
 {
  if
  (
     // Quadrant I
     a <= x + 10 && b <= y + 10 && a >= x && b >= y ||
     // Quadrant II
     a >= x - 10 && b >= y - 10 && a <= x && b <= y ||
     // Quadrant III
     a >= x - 10 && b <= y + 10 && a <= x && b >= y ||
     // Quadrant IV
     a <= x + 10 && b >= y - 10 && a >= x && b <= y
   )


 {touch = true;
  reset();
  repaint();}

 else
 {touch = false;
  repaint();}

  if
  (
   x <= 0 ||
   y <= 0 ||
   y >= 400 - 10 ||
   x >= 700 - 10
  )

  {
   scorea--;
   scoreCheck();
   reset();
   repaint();
  }

  if
  (
   b <= 0 ||
   a <= 0 ||
   a >= 700 - 10 ||
   b >= 400 - 10
  )

  {
   scoreb--;
   scoreCheck();
   reset();
   repaint();
  }
 }

 public void reset()
 {
  y = 200;
  x = 50;
  a = 700 - 60;
  b = 200;
  repaint();
 }

 public void keyReleased(KeyEvent e) {}

 public void keyTyped(KeyEvent e)
 {}

 public void paint(Graphics g)
 {
  setBackground(Color.white);

  g.setColor(Color.red);
  g.fillRect(x, y, 10, 10);

  g.setColor(Color.black);
  g.fillRect(a, b, 10, 10);

  g.setColor(Color.red);
  g.drawString("Player 1: " + scorea, 40, 20);

  g.setColor(Color.black);
  g.drawString("Player 2: " + scoreb, 600, 20);

  if (touch == true)
  {
   g.drawString("Touch!", 100, 100);
  }

  if (win == 1)
  {
   g.setColor(Color.blue);
   g.drawString("Player 1 Wins!!", 330, 180);
  }

  if (win == 2)
  {
   g.setColor(Color.blue);
   g.drawString("Player 1 Wins!!", 330, 180);
  }

 }

}
