PONG BABY :includes super advanced A.I.
Author |
Message |
ify
|
Posted: Sun Jun 13, 2004 5:17 pm Post subject: PONG BABY :includes super advanced A.I. |
|
|
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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Paul
|
Posted: Sun Jun 13, 2004 7:42 pm Post subject: (No subject) |
|
|
Uh... can't run it, there's like 50 errors when I try to compile... am I doing something wrong? |
|
|
|
|
|
ify
|
Posted: Mon Jun 14, 2004 1:21 pm Post subject: . |
|
|
i used "ready to program" to make it u probably have a different java |
|
|
|
|
|
Jekate
|
Posted: Sat Oct 16, 2004 1:14 pm Post subject: (No subject) |
|
|
i used ready to program as well and there are still many errors. |
|
|
|
|
|
alikhan
|
Posted: Sun May 15, 2005 7:05 pm Post subject: (No subject) |
|
|
works for me though ure AI is kinda working weird. is it meant to dance about like that? |
|
|
|
|
|
wtd
|
Posted: Sun May 15, 2005 11:14 pm Post subject: (No subject) |
|
|
A tip: use standard Java naming conventions. In Java all class names start with a capital letter. Also, use meaningful class names.
Another tip: You have lots of (x, y) coordinates. Create a class for this.
Java: | class Point
{
private int x, y;
public Point(int x, int y )
{
this. x = x;
this. y = y;
}
public int getX () { return x; }
public int getY () { return y; }
public void setX (int x ) { this. x = x; }
public void setY (int y ) { this. y = y; }
} |
|
|
|
|
|
|
Andy
|
Posted: Mon May 16, 2005 6:27 pm Post subject: (No subject) |
|
|
Point2D! my favorite object haha... |
|
|
|
|
|
diablos8704
|
Posted: Fri Nov 24, 2006 2:24 pm Post subject: it's good but... |
|
|
the a.i. was dancing around so much i rarley had to move becuase the ball would come up and score on the a.i. so it's a good start but it needs to be fixed |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Fri Nov 24, 2006 2:34 pm Post subject: (No subject) |
|
|
Holy necro post Batman! |
|
|
|
|
|
|
|