// The "EARTHQUAKEPONG" class.
import java.awt.*;
import javax.swing.*;
public class EARTHQUAKEPONG extends Frame
{
// declaiations of variables
int ballX = 200;
int ballY = 330;
int xTwenty = 20;
int yFive = 7;
int playerY = 200;
int computerY = 200;
int playerScore = 0;
int computerScore = 0;
int x2 = 0;
int y2 = 0;
Image offScreenImage;
Graphics offScreenBuffer;
MenuItem newOption, exitOption, helpOption, aboutOption;
public EARTHQUAKEPONG ()
{
super ("EARTHQUAKE PONG"); // Set the frame's name
setResizable (false);
setSize (600, 400); // Set the frame's size
setLocation (360, 25);
setBackground (Color.black);
newOption = new MenuItem ("New");
exitOption = new MenuItem ("Exit");
helpOption = new MenuItem ("Help");
aboutOption = new MenuItem ("Info");
Menu gameMenu = new Menu ("Game");
Menu gameMenuTwo = new Menu ("Help");
gameMenu.add (newOption);
gameMenu.addSeparator ();
gameMenu.add (exitOption);
gameMenuTwo.add (helpOption);
gameMenuTwo.addSeparator ();
gameMenuTwo.add (aboutOption);
// Set up the Game Menu
MenuBar mainMenu = new MenuBar ();
mainMenu.add (gameMenu);
mainMenu.add (gameMenuTwo);
// Set the menu bar for this frame to mainMenu
setMenuBar (mainMenu);
show (); // Show the frame
// Constructor
}
public void newGame ()
{
ballX = 200;
ballY = 330;
playerScore = 0;
computerScore = 0;
repaint ();
}
public boolean action (Event evt, Object arg)
{
if (evt.target == newOption)
{
newGame ();
}
else if (evt.target == exitOption)
{
hide ();
System.exit (0);
}
else if (evt.target == helpOption)
{
JOptionPane.showMessageDialog (null, "electronic tennis earthquake stylz to the max", "Help window", JOptionPane.INFORMATION_MESSAGE);
}
else if (evt.target == aboutOption)
{
JOptionPane.showMessageDialog (null, "programed by: IFY", "About", JOptionPane.INFORMATION_MESSAGE);
}
else
return false;
return true;
}
// handles imput from the mouse
public boolean mouseMove (Event event, int x, int y)
{
if (y >= 30 || y <= 350)
{
playerY = y;
repaint ();
return true;
}
else
return false;
}
// Handles the close button on the window
public boolean handleEvent (Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
{
hide ();
System.exit (0);
return true;
}
// If not handled, pass the event along
return super.handleEvent (evt);
}
//draws the images on the screen however were using a offscreen buffer so the images are drawn
//off screne and then brought on screen
public void paint (Graphics g)
{
if (computerY < ballY)
{
computerY += 20;
}
else
{
computerY -= 20;
}
if (offScreenBuffer == null)
{
offScreenImage = createImage (size ().width, size ().height);
offScreenBuffer = offScreenImage.getGraphics ();
}
offScreenBuffer.clearRect (0, 0, size ().width, size ().height);
//places a delay in the code so the ball doesnt move super fast
try
{
Thread.sleep (5);
}
catch (Exception e)
{
}
int pat = (int) (Math.random () * 3) + 1;
int com = (int) (Math.random () * 3) + 1;
//switches the dy to nevitive if the ball hits a wall so the ball will go the opposite way
if (ballY < 55 || ballY > 350)
{
yFive = -yFive;
}
//this is incase the ball hits a corner and doesnt get stuck in the wall or if the ball hits the pattle
else if (ballX <= 60 && ballX >= 50 && ballY >= playerY & ballY <= playerY + 100)
{
x2 = 0;
}
else if (ballX <= 540 && ballX >= 520 && ballY >= computerY & ballY <= computerY + 100)
{
x2 = 1;
}
if (ballY > 350)
{
y2 = 1;
}
else if (ballY < 55)
{
y2 = 0;
}
if (y2 == 0)
{
yFive = (int) (Math.random () * 10) + 1;
}
else if (y2 == 1)
{
yFive = (int) (Math.random () * -10) + 1;
}
if (x2 == 0)
{
xTwenty = (int) (Math.random () * 10) + 1;
}
else if (x2 == 1)
{
xTwenty = (int) (Math.random () * -10) + 1;
}
if (ballX <= 0)
{
ballX = 200;
ballY = 330;
computerScore++;
JOptionPane.showMessageDialog (null, playerScore + " / " + computerScore, "Score", JOptionPane.INFORMATION_MESSAGE);
}
else if (ballX >= 600)
{
ballX = 200;
ballY = 330;
playerScore++;
JOptionPane.showMessageDialog (null, playerScore + " / " + computerScore, "Score", JOptionPane.INFORMATION_MESSAGE);
}
// drawing the ball
ballX = ballX + xTwenty;
ballY = ballY + yFive;
offScreenBuffer.setColor (Color.white);
offScreenBuffer.fillRect (ballX, ballY, 10, 10);
//draws the pattle which is the player&and the computer pattle
if (pat == 1)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (53, playerY + 2, 10, 100);
}
else if (pat == 2)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (45, playerY + 4, 10, 100);
}
else if (pat == 3)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (50, playerY, 10, 100);
}
if (pat == 1)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (534, computerY - 5, 10, 100);
}
else if (pat == 2)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (527, computerY + 2, 10, 100);
}
else if (pat == 3)
{
offScreenBuffer.setColor (Color.LIGHT_GRAY);
offScreenBuffer.fillRect (530, computerY, 10, 100);
}
//repaints the screen so we dont have old images on the screen
repaint ();
// Transfer the offScreenBuffer to the screen
g.drawImage (offScreenImage, 0, 0, this);
// Place the drawing code here
} // paint method
public void update (Graphics g)
{
paint (g);
}
public void destroy ()
{
offScreenBuffer.dispose ();
}
public static void main (String[] args)
{
new EARTHQUAKEPONG (); // Create a EARTHQUAKEPONG frame
} // main method
} // EARTHQUAKEPONG class
|