what else can i say its freakin pong
Java: | // The "asdf" class.
import java.awt.*;
import javax.swing.*;
public class asdf extends Frame
{
// declaiations of variables
int ballX = 300;
int ballY = 200;
int xTwenty = 20;
int yFive = 7;
int playerY = 200;
int computerY = 200;
int playerScore = 0;
int computerScore = 0;
int twenty = 20;
Image offScreenImage;
Graphics offScreenBuffer;
MenuItem newOption, exitOption, helpOption, aboutOption;
public asdf ()
{
super ("asdf"); // 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 = 300;
ballY = 200;
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, "PONG its tennis but electronic", "Help window", JOptionPane. INFORMATION_MESSAGE);
}
else if (evt. target == aboutOption )
{
JOptionPane. showMessageDialog (null, "programed by: Steven Baldwin, Julian Change", "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 = computerY - 30;
else if (computerY <= ballY )
computerY = computerY + 30;
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 (75);
}
catch (Exception e )
{
}
//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)
{
xTwenty = -xTwenty;
}
else if (ballX <= 540 && ballX >= 520 && ballY >= computerY & ballY <= computerY + 100)
{
xTwenty = -xTwenty;
}
if (ballX <= 0)
{
ballX = 300;
ballY = 200;
computerScore++;
JOptionPane. showMessageDialog (null, playerScore + " / " + computerScore, "Score", JOptionPane. INFORMATION_MESSAGE);
}
else if (ballX >= 600)
{
ballX = 300;
ballY = 200;
playerScore++;
JOptionPane. showMessageDialog (null, playerScore + " / " + computerScore, "Score", JOptionPane. INFORMATION_MESSAGE);
}
if (playerScore == 10)
{
ballX = 300;
ballY = 200;
playerScore = 0;
computerScore = 0;
JOptionPane. showMessageDialog (null, "PLAYER WINS!!!!!!!!", "Game Over", JOptionPane. INFORMATION_MESSAGE);
}
else if (computerScore == 10)
{
ballX = 300;
ballY = 200;
computerScore = 0;
playerScore = 0;
JOptionPane. showMessageDialog (null, "computer wins due to super advanced AI", "Game Over", 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
offScreenBuffer. setColor (Color. LIGHT_GRAY);
offScreenBuffer. fillRect (50, playerY, 10, 100);
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 asdf (); // Create a asdf frame
} // main method
} // asdf class
|
enjoy |