Octocamo
12-10-2006, 10:42 PM
I'm in Java programming for school. I'm going to take AP Java programming in January. It's an okay class. Easy at sometimes, hard at others. Our most recent project was to recreate the game "Pong".
This is how mine came out:
http://img169.imageshack.us/img169/2054/ponggo7.jpg
It's okay. I had to make the AI... "suck" because it was too good. If I can get it uploaded and playable, I will link it here.
Don't criticize my code for being poorly indented or whatever like my teacher does. I know it's not the best optimized, but hey... it works.
/**
* Pong applet - This Pong applet recreates the classic game of Pong in java.
* @author Kevin Lee
* @version 1.0 (December 2006)
*/
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.color.*;
import javax.swing.JOptionPane;
public class Pong extends JApplet implements Runnable, KeyListener
{
Ball Ball1;
Paddle Paddle1;
Paddle Paddle2;
double AI=0;
boolean ballOffscreen=false;
int reset=1;
int x=250;
int y=250;
// These are the integers used for collision detection
int ballX=250;
int ballY=250;
// Left Paddle's X
int Paddle1X=10;
//Left Paddle's Y
int Paddle1Y=250;
//Right Paddle's X
int Paddle2X=480;
//Right Paddle's Y
int Paddle2Y=250;
// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen;
int xChange = 1;
int yChange = 1;
public void init()
{
addKeyListener(this);
// This is the ball
Ball1 = new Ball (Color.white,ballX,ballY);
// This is the Paddle on the left
Paddle1 = new Paddle (Color.white,Paddle1X,Paddle1Y);
//This is the Paddle on the right
Paddle2 = new Paddle (Color.white,Paddle2X,Paddle2Y);
Dimension dim = getSize();
// We'll redraw the applet each time the mouse has moved.
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect larger
// size could slow it down unnecessary.
offscreen = createImage(dim.width,dim.height);
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
bufferGraphics = offscreen.getGraphics();
// This pauses the game until someone presses the spacebar
}
public void paint (Graphics g)
{
drawAnimation();
// draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
g.drawImage(offscreen,0,0,this);
}
public void drawAnimation()
{
//The following two lines resets the screen
bufferGraphics.setColor(Color.black);
bufferGraphics.fillRect(0,0,500,500);
//This draws the ball.
bufferGraphics.setColor(Color.white);
Ball1.drawBall(bufferGraphics);
//The left side paddle
Paddle1.drawPaddle(bufferGraphics);
//The right side paddle
Paddle2.drawPaddle(bufferGraphics);
//The middle line in the game
bufferGraphics.fillRect(250,0,10,500);
//The method used to check if the ball needs to bounce or not
bufferGraphics.drawString("Java Pong - Kevin Lee",375,495);
collisionDetection();
paddleAI();
}
public void collisionDetection()
{
//Left Paddles hits
//up
if (Ball1.x==20 && Ball1.y>Paddle1.y-20 && Ball1.y<Paddle1.y+10)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//right
if (Ball1.x==20 && Ball1.y>Paddle1.PaddleY+10 && Ball1.y<Paddle1.PaddleY+40)
{
Ball1.right=true;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==20 && Ball1.y<Paddle1.PaddleY+100)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}
//Right Paddle hits
//up
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY-20 && Ball1.y<Paddle2.PaddleY+10)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//left
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+10 && Ball1.y<Paddle2.PaddleY+40)
{
Ball1.right=false;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+40 && Ball1.y<Paddle2.PaddleY+100)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}
}
public void paddleAI()
{
AI=Math.random();
if (Paddle1.PaddleY>0)
{
if (Paddle1.PaddleY>Ball1.y)
{
if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY-2;
}
}
}
if (Paddle1.PaddleY<400)
{
if (Paddle1.PaddleY<Ball1.y)
{
if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY+2;
}
}
}
}
// This makes the paddles move according to the users presses
public void keyPressed(KeyEvent e)
{
//These variables get the key the user presses
char theChar=e.getKeyChar();
int theCode=e.getKeyCode();
// Right Paddle Move Up
if(theCode==KeyEvent.VK_UP)
{
Paddle2.moveUp();
repaint();
}
// Right Paddle Move Down
if(theCode==KeyEvent.VK_DOWN)
{
Paddle2.moveDown();
repaint();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
/************************************************** *******************************************/
/* BELOW IS FOR ANIMATION. THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY
*/
int frame;
int delay=4; // This is the time of the delay in milliseconds.
Thread animator;
public void update(Graphics g)
{
paint(g);
}
/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start()
{
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run()
{
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator)
{
// Display the next frame of animation.
repaint();
try
{
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
// Advance the frame
frame++;
}
}
/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop()
{
animator = null;
}
}
/**
* This is the class ball for the Pong applet
*
* @author Kevin Lee
* @date December 2006
*/
class Ball
{
int x=250;
int y=250;
int xChange = 1;
int yChange = 1;
int Player1Score=0;
int Player2Score=0;
//These booleans are what tells the ball which direction to move.
boolean up = true;
boolean right = true;
boolean start = true;
int xMovement=1;
int yMovement=1;
int ballX;
int ballY;
Color ballColor;
public void init()
{
}
public Ball(Color theColor,int x, int y)
{
ballColor = theColor;
ballX= x;
ballY= y;
}
public void resetBall(Graphics g)
{
g.setColor(Color.white);
g.fillOval(x,y,5,5);
}
public void bounceLeft()
{
ballX= ballX-1;
}
public void bounceRight()
{
ballX= ballX+1;
}
public void drawBall(Graphics g)
{
//This draws the ball.
g.setColor(Color.white);
g.fillOval(x,y,15,15);
g.drawString(""+Player1Score,375,15);
g.drawString(""+Player2Score,125,15);
//tells ball to start moving down when it is near the top
if (y>479)
{
up=false;
}
//tells ball to return to the middle once it hits the right wall,
//then gives player 2 a point
if (x>500)
{
x=250;
Player2Score++;
xMovement=1;
yMovement=1;
}
//tells ball to start moving up when it hits the bottom
if (y<1)
{
up=true;
}
//returns ball to middle, gives player 2 a point
if (x<-20)
{
x=250;
Player1Score++;
xMovement=1;
yMovement=1;
}
if (up)
{
y=y+yMovement;
}
else y=y-yMovement;
if (right)
{
x=x+xMovement;
}
else x=x-xMovement;
}
}
/**
* This is the class Paddle for the Java applet
*
* @author Kevin Lee
* @date 11-28-06
*/
class Paddle
{
int PaddleX;
int PaddleY;
Color PaddleColor;
int color = 0;
int upOrDown=0;
int x;
int y;
public void init()
{
}
public Paddle(Color theColor,int x, int y)
{
PaddleColor=theColor;
PaddleX=x;
PaddleY=y;
}
public void drawPaddle (Graphics g)
{
g.setColor(Color.white);
g.fillRect(PaddleX,PaddleY,10,100);
}
public void moveUp()
{
if(PaddleY>=10)
PaddleY= PaddleY-25;
}
public void moveDown()
{
if(PaddleY<400)
PaddleY= PaddleY+25;
}
}
I did have it split up into 3 classes (Pong, Paddle, and Ball), but to upload it for the teacher to grade, I had to put it all into one java file.
So far I've made a paint program, Pong, BMI calculator, and other various mini-applets.
Anyone else have to program in java?
This is how mine came out:
http://img169.imageshack.us/img169/2054/ponggo7.jpg
It's okay. I had to make the AI... "suck" because it was too good. If I can get it uploaded and playable, I will link it here.
Don't criticize my code for being poorly indented or whatever like my teacher does. I know it's not the best optimized, but hey... it works.
/**
* Pong applet - This Pong applet recreates the classic game of Pong in java.
* @author Kevin Lee
* @version 1.0 (December 2006)
*/
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.color.*;
import javax.swing.JOptionPane;
public class Pong extends JApplet implements Runnable, KeyListener
{
Ball Ball1;
Paddle Paddle1;
Paddle Paddle2;
double AI=0;
boolean ballOffscreen=false;
int reset=1;
int x=250;
int y=250;
// These are the integers used for collision detection
int ballX=250;
int ballY=250;
// Left Paddle's X
int Paddle1X=10;
//Left Paddle's Y
int Paddle1Y=250;
//Right Paddle's X
int Paddle2X=480;
//Right Paddle's Y
int Paddle2Y=250;
// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen;
int xChange = 1;
int yChange = 1;
public void init()
{
addKeyListener(this);
// This is the ball
Ball1 = new Ball (Color.white,ballX,ballY);
// This is the Paddle on the left
Paddle1 = new Paddle (Color.white,Paddle1X,Paddle1Y);
//This is the Paddle on the right
Paddle2 = new Paddle (Color.white,Paddle2X,Paddle2Y);
Dimension dim = getSize();
// We'll redraw the applet each time the mouse has moved.
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect larger
// size could slow it down unnecessary.
offscreen = createImage(dim.width,dim.height);
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
bufferGraphics = offscreen.getGraphics();
// This pauses the game until someone presses the spacebar
}
public void paint (Graphics g)
{
drawAnimation();
// draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
g.drawImage(offscreen,0,0,this);
}
public void drawAnimation()
{
//The following two lines resets the screen
bufferGraphics.setColor(Color.black);
bufferGraphics.fillRect(0,0,500,500);
//This draws the ball.
bufferGraphics.setColor(Color.white);
Ball1.drawBall(bufferGraphics);
//The left side paddle
Paddle1.drawPaddle(bufferGraphics);
//The right side paddle
Paddle2.drawPaddle(bufferGraphics);
//The middle line in the game
bufferGraphics.fillRect(250,0,10,500);
//The method used to check if the ball needs to bounce or not
bufferGraphics.drawString("Java Pong - Kevin Lee",375,495);
collisionDetection();
paddleAI();
}
public void collisionDetection()
{
//Left Paddles hits
//up
if (Ball1.x==20 && Ball1.y>Paddle1.y-20 && Ball1.y<Paddle1.y+10)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//right
if (Ball1.x==20 && Ball1.y>Paddle1.PaddleY+10 && Ball1.y<Paddle1.PaddleY+40)
{
Ball1.right=true;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==20 && Ball1.y<Paddle1.PaddleY+100)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}
//Right Paddle hits
//up
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY-20 && Ball1.y<Paddle2.PaddleY+10)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//left
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+10 && Ball1.y<Paddle2.PaddleY+40)
{
Ball1.right=false;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+40 && Ball1.y<Paddle2.PaddleY+100)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}
}
public void paddleAI()
{
AI=Math.random();
if (Paddle1.PaddleY>0)
{
if (Paddle1.PaddleY>Ball1.y)
{
if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY-2;
}
}
}
if (Paddle1.PaddleY<400)
{
if (Paddle1.PaddleY<Ball1.y)
{
if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY+2;
}
}
}
}
// This makes the paddles move according to the users presses
public void keyPressed(KeyEvent e)
{
//These variables get the key the user presses
char theChar=e.getKeyChar();
int theCode=e.getKeyCode();
// Right Paddle Move Up
if(theCode==KeyEvent.VK_UP)
{
Paddle2.moveUp();
repaint();
}
// Right Paddle Move Down
if(theCode==KeyEvent.VK_DOWN)
{
Paddle2.moveDown();
repaint();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
/************************************************** *******************************************/
/* BELOW IS FOR ANIMATION. THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY
*/
int frame;
int delay=4; // This is the time of the delay in milliseconds.
Thread animator;
public void update(Graphics g)
{
paint(g);
}
/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start()
{
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run()
{
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator)
{
// Display the next frame of animation.
repaint();
try
{
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
// Advance the frame
frame++;
}
}
/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop()
{
animator = null;
}
}
/**
* This is the class ball for the Pong applet
*
* @author Kevin Lee
* @date December 2006
*/
class Ball
{
int x=250;
int y=250;
int xChange = 1;
int yChange = 1;
int Player1Score=0;
int Player2Score=0;
//These booleans are what tells the ball which direction to move.
boolean up = true;
boolean right = true;
boolean start = true;
int xMovement=1;
int yMovement=1;
int ballX;
int ballY;
Color ballColor;
public void init()
{
}
public Ball(Color theColor,int x, int y)
{
ballColor = theColor;
ballX= x;
ballY= y;
}
public void resetBall(Graphics g)
{
g.setColor(Color.white);
g.fillOval(x,y,5,5);
}
public void bounceLeft()
{
ballX= ballX-1;
}
public void bounceRight()
{
ballX= ballX+1;
}
public void drawBall(Graphics g)
{
//This draws the ball.
g.setColor(Color.white);
g.fillOval(x,y,15,15);
g.drawString(""+Player1Score,375,15);
g.drawString(""+Player2Score,125,15);
//tells ball to start moving down when it is near the top
if (y>479)
{
up=false;
}
//tells ball to return to the middle once it hits the right wall,
//then gives player 2 a point
if (x>500)
{
x=250;
Player2Score++;
xMovement=1;
yMovement=1;
}
//tells ball to start moving up when it hits the bottom
if (y<1)
{
up=true;
}
//returns ball to middle, gives player 2 a point
if (x<-20)
{
x=250;
Player1Score++;
xMovement=1;
yMovement=1;
}
if (up)
{
y=y+yMovement;
}
else y=y-yMovement;
if (right)
{
x=x+xMovement;
}
else x=x-xMovement;
}
}
/**
* This is the class Paddle for the Java applet
*
* @author Kevin Lee
* @date 11-28-06
*/
class Paddle
{
int PaddleX;
int PaddleY;
Color PaddleColor;
int color = 0;
int upOrDown=0;
int x;
int y;
public void init()
{
}
public Paddle(Color theColor,int x, int y)
{
PaddleColor=theColor;
PaddleX=x;
PaddleY=y;
}
public void drawPaddle (Graphics g)
{
g.setColor(Color.white);
g.fillRect(PaddleX,PaddleY,10,100);
}
public void moveUp()
{
if(PaddleY>=10)
PaddleY= PaddleY-25;
}
public void moveDown()
{
if(PaddleY<400)
PaddleY= PaddleY+25;
}
}
I did have it split up into 3 classes (Pong, Paddle, and Ball), but to upload it for the teacher to grade, I had to put it all into one java file.
So far I've made a paint program, Pong, BMI calculator, and other various mini-applets.
Anyone else have to program in java?