import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class HardestGame extends Applet
implements KeyListener, MouseListener
{
final int WIDTH = 400;
final int HEIGHT = 123;
int myX = 400;
int myY = 400;
int PlayerX = 40; // These are beginning coordinates for the
int PlayerY = 80; // Player's circle
int squareLeft = 250;
int SQUARE_SIZE = 250;
int squareTop = 250;
Button startGame = new Button ("Start Game");
Canvas display = new Canvas ();
public void init ()
{
super.init ();
setLayout (new BorderLayout ());
int windowWidth = getSize ().width;
int windowHeight = getSize ().height;
display.resize (windowWidth, windowHeight);
setBackground (Color.white);
addKeyListener (this);
addMouseListener (this);
add ("North", startGame);
add ("Center", display);
}
public boolean action (Event e, Object o)
{
final int RADIUS = 7;
final int RADIUS2 = 7;
final int RADIUS3 = 7;
final int RADIUS4 = 7;
Graphics displayG;
int x1, y1, dx, dy, x2, y2, x3, y3, x4, y4, dx2, dy2, dx3, dy3, dx4, dy4;
displayG = display.getGraphics ();
x1 = 195;
y1 = 67;
x2 = 195;
y2 = 107;
x3 = 195;
y3 = 127;
x4 = 195;
y4 = 167;
drawBall (displayG, x1, y1, RADIUS, Color.blue);
drawBall2 (displayG, x2, y2, RADIUS2, Color.blue);
drawBall3 (displayG, x3, y3, RADIUS3, Color.blue);
drawBall4 (displayG, x4, y4, RADIUS4, Color.blue);
dx = 1;
dy = 0;
dx2 = -1;
dy2 = 0;
dx3 = 1;
dy3 = 0;
dx4 = -1;
dy4 = 0;
for (int i = 0 ; i < 1000000 ; i++) //loops for how long it will move
{
drawBall (displayG, x1, y1, RADIUS, getBackground ());
x1 += dx; //movement of the ball
y1 += dy; //movement of the ball
if (x1 <= RADIUS || x1 >= WIDTH - RADIUS)
dx = -dx; //determine which direction
if (y1 <= RADIUS || y1 >= HEIGHT - RADIUS)
dy = -dy; //determine which direction
drawBall (displayG, x1, y1, RADIUS, Color.blue); //draw the ball
drawBall2 (displayG, x2, y2, RADIUS2, getBackground ());
x2 += dx2; //movement of the ball
y2 += dy2; //movement of the ball
if (x2 <= RADIUS2 || x2 >= WIDTH - RADIUS2)
dx2 = -dx2; //determine which direction
if (y2 <= RADIUS2 || y2 >= HEIGHT - RADIUS2)
dy2 = -dy2; //determine which direction
drawBall2 (displayG, x2, y2, RADIUS2, Color.blue); //draw the ball
drawBall3 (displayG, x3, y3, RADIUS3, getBackground ());
x3 += dx3; //movement of the ball
y3 += dy3; //movement of the ball
if (x3 <= RADIUS2 || x3 >= WIDTH - RADIUS3)
dx3 = -dx3; //determine which direction
if (y3 <= RADIUS2 || y3 >= HEIGHT - RADIUS3)
dy3 = -dy3; //determine which direction
drawBall3 (displayG, x3, y3, RADIUS3, Color.blue); //draw the ball
drawBall4 (displayG, x4, y4, RADIUS4, getBackground ());
x4 += dx4; //movement of the ball
y4 += dy4; //movement of the ball
if (x4 <= RADIUS || x4 >= WIDTH - RADIUS4)
dx4 = -dx4; //determine which direction
if (y4 <= RADIUS2 || y4 >= HEIGHT - RADIUS3)
dy4 = -dy4; //determine which direction
drawBall4 (displayG, x4, y4, RADIUS4, Color.blue); //draw the ball
ballDelay (3000000); // Wastes time to produce delay.
remove (startGame);
}
return false;
} // action method
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_LEFT)
{
squareLeft -= 5;
repaint ();
PlayerX -= 5;
}
else if (key == KeyEvent.VK_RIGHT)
{
PlayerX += 5;
repaint ();
squareLeft += 5;
repaint ();
}
else if (key == KeyEvent.VK_UP)
{
PlayerY -= 5;
squareTop -= 5;
repaint ();
}
else if (key == KeyEvent.VK_DOWN)
{
PlayerY += 5;
squareTop += 5;
repaint ();
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited (MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
}
public void mouseReleased (MouseEvent e)
{
}
public void mouseClicked (MouseEvent e)
{
}
public void paint (Graphics g)
{
if (PlayerX < 90 && PlayerX >= 8 && PlayerY > 50 && PlayerY < 220)
{
if (PlayerY < 60)
PlayerY = 60;
if (PlayerX < 14)
PlayerX = 14;
if (PlayerX > 70)
PlayerX = 70;
}
if (PlayerX > 10 && PlayerX < 170 && PlayerY > 200 && PlayerY < 260)
{
if (PlayerY > 240)
PlayerY = 240;
if (PlayerX < 16)
PlayerX = 16;
if (PlayerX > 150)
PlayerX = 150;
}
if (PlayerX > 89 && PlayerX < 130 && PlayerY > 210)
{
if (PlayerY < 220)
PlayerY = 220;
}
if (PlayerX > 122 && PlayerX < 170 && PlayerY < 220)
{
if (PlayerX < 130)
PlayerX = 130;
if (PlayerY < 100)
PlayerY = 100;
}
if (PlayerX >= 170 && PlayerX < 490 && PlayerY > 92 && PlayerY < 210)
{
if (PlayerY > 200)
PlayerY = 200;
if (PlayerY < 100)
PlayerY = 100;
}
if (PlayerX > 460 && PlayerX < 518 && PlayerY > 80)
{
if (PlayerX > 510)
PlayerX = 510;
if (PlayerY > 200)
PlayerY = 200;
}
if (PlayerX > 480 && PlayerX < 650 && PlayerY > 40 && PlayerY < 98)
{
if (PlayerX < 490)
PlayerX = 490;
if (PlayerY < 60)
PlayerY = 60;
}
if (PlayerX > 510 && PlayerX < 570 && PlayerY > 60 && PlayerY < 110)
{
if (PlayerY > 80)
PlayerY = 80;
}
}
public void drawBall (Graphics g, int x, int y, int radius, Color clr)
{
g.setColor (clr); //clears where the ball has moved
g.fillOval (x - radius + 130, y - radius + 50, 2 * radius, 2 * radius); //determines the boundaries of the ball
g.setColor (Color.black);
g.fillRect (10, 60, 2, 200);
g.fillRect (10, 60, 80, 2);
g.fillRect (90, 60, 2, 160);
g.fillRect (10, 260, 160, 2);
g.fillRect (90, 220, 40, 2);
g.fillRect (130, 100, 2, 122);
g.fillRect (130, 100, 360, 2);
g.fillRect (490, 60, 2, 42);
g.fillRect (170, 220, 2, 42);
g.fillRect (170, 220, 360, 2);
g.fillRect (530, 100, 2, 122);
g.fillRect (530, 100, 40, 2);
g.fillRect (570, 100, 2, 160);
g.fillRect (570, 260, 80, 2);
g.fillRect (650, 60, 2, 202);
g.fillRect (490, 60, 160, 2);
g.setColor (new Color (190, 255, 190));
g.fillRect (12, 62, 78, 198);
g.fillRect (572, 62, 78, 198);
g.setColor (Color.RED);
g.fillRect (PlayerX, PlayerY, 20, 20);
}
public void drawBall2 (Graphics g, int x2, int y2, int radius2, Color clr)
{
g.setColor (clr); //clears where the ball has moved
g.fillOval (x2 - radius2 + 130, y2 - radius2 + 40, 2 * radius2, 2 * radius2); //determines the boundaries of the ball
} // drawBall method
public void drawBall3 (Graphics g, int x3, int y3, int radius3, Color clr)
{
g.setColor (clr); //clears where the ball has moved
g.fillOval (x3 - radius3 + 130, y3 - radius3 + 50, 2 * radius3, 2 * radius3); //determines the boundaries of the ball
} // drawBall method
public void drawBall4 (Graphics g, int x4, int y4, int radius4, Color clr)
{
g.setColor (clr); //clears where the ball has moved
g.fillOval (x4 - radius4 + 130, y4 - radius4 + 40, 2 * radius4, 2 * radius4); //determines the boundaries of the ball
} // drawBall method
public void ballDelay (int howLong)
{
// This wastes time.
for (int i = 1 ; i <= howLong ; i++)
{
double garbage = Math.PI * Math.PI;
}
} // delay method
}