Code:
/*
* Coded from scratch by LightzOut
* Date started: 9/15/10
* Date finished: 9/15/10
* Total Time: 32 minutes
* Name: Pong
*/
import java.awt.event.*;
import java.awt.*;
import java.aw*****lor;
import java.applet.*;
import java.awt.Image;
public class Pong extends Applet implements KeyListener, MouseListener, MouseMotionListener
{
Image backbuffer;
Image blackBG;
private Graphics backg;
private int xBall;
private int yBall;
private int xPad;
private int yPad;
private int xPos;
private int yPos;
private int clickX;
private int clickY;
private int xOffset;
private int yOffset;
private int xAIPad;
private int yAIPad;
private int hitCount;
private int score_AI;
private int score_USER;
private int buttonTextX;
private int buttonTextY;
private boolean moveUp;
private boolean moveDown;
private boolean padMoveDir;
private boolean padMoveDir1;
private boolean startGame;
private boolean gameOver;
private String buttonText;
public void init()
{
xBall = 225;
yBall = 350;
xPad = 1000;
yPad = 350;
xAIPad = 200;
yAIPad = 350;
xOffset = 8;
yOffset = 8;
hitCount = 0;
score_AI = 0;
score_USER = 0;
buttonTextY = 405;
startGame = false;
gameOver = false;
moveUp = false;
moveDown = false;
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
backbuffer = createImage(getSize().width, getSize().height);
backg = backbuffer.getGraphics();
blackBG = getImage(getCodeBase(), "blackBG.png");
System.out.println(getCodeBase());
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
backg.setColor(Color.WHITE);
map();
ball();
paddle();
AIpaddle();
status();
xhair();
g.drawImage(backbuffer, 0,0, this);
try {
Thread.sleep(10);
} catch(InterruptedException e) {}
repaint();
}
public void map()
{
backg.drawImage(blackBG, 0, 0, 1200, 800, this);
backg.drawRect(1, 1, 1198, 798);
if(!startGame && !gameOver)
{
if(score_USER > 0 || score_AI > 0)
{
buttonText = "Continue";
buttonTextX = 577;
}
else
{
buttonText = "Start";
buttonTextX = 588;
}
backg.drawRect(550, 375, 100, 50);
backg.drawString(buttonText, buttonTextX, buttonTextY);
}
if(gameOver)
{
if(score_USER > score_AI)
backg.drawString("YOU WON! GOOD JOB!", 580, 380);
else
backg.drawString("YOU LOST! FAILURE!", 580, 380);
}
backg.drawString("Hit Count: " + hitCount, 575, 750);
}
public void ball()
{
if(startGame && !gameOver)
{
if(xBall < 195)
{
score_USER++;
if(score_USER == 21)
gameOver = true;
xBall = xPad - 25;
yBall = yPad + 50;
xOffset = -8;
startGame = false;
}
if(xBall > 1005)
{
score_AI++;
if(score_AI == 21)
gameOver = true;
xBall = xAIPad + 25;
yBall = yAIPad + 50;
xOffset = 8;
startGame = false;
}
xBall += xOffset;
yBall += yOffset;
if(yBall >= yPad - 75 && yBall <= yPad + 50 && xBall >= xPad - 25)
{
hitCount++;
xOffset = -xOffset;
if(padMoveDir == false)
yOffset = 8;
if(padMoveDir == true)
yOffset = -8;
}
if(yBall >= yAIPad - 75 && yBall <= yAIPad + 50 && xBall <= xAIPad + 25)
{
hitCount++;
xOffset = -xOffset;
if(padMoveDir1 == false)
yOffset = 8;
if(padMoveDir1 == true)
yOffset = -8;
}
if(yBall < 0 || yBall > 760)
yOffset = -yOffset;
if(xBall < 0 || xBall > 1175)
xOffset = -xOffset;
backg.fillArc(xBall, yBall, 25, 25, 0, 360);
}
}
public void paddle()
{
if(startGame && !gameOver)
{
if(yPad < yPos)
{
padMoveDir = false;
yPad += 7;
}
if(yPad > yPos)
{
padMoveDir = true;
yPad -= 7;
}
/* if(moveUp == true)
yPad -= 5;
if(moveDown == true)
yPad += 5;*/
backg.fillRect(xPad, yPad - 50, 25, 100);
}
}
public void AIpaddle()
{
if(startGame && !gameOver)
{
if(yAIPad < yBall)
{
padMoveDir1 = false;
yAIPad += 7;
}
if(yAIPad > yBall)
{
padMoveDir1 = true;
yAIPad -= 7;
}
backg.fillRect(xAIPad, yAIPad - 50, 25, 100);
}
}
public void status()
{
backg.drawString("AI: " + score_AI, 380, 50);
backg.drawString("USER: " + score_USER, 820, 50);
}
public void xhair()
{
Frame browserFrame;
Component parentComponent;
parentComponent = getParent();
while ( parentComponent != null &&
!(parentComponent instanceof Frame)) {
parentComponent = parentComponent.getParent();
}
browserFrame = (Frame) parentComponent;
browserFrame.setCursor(Frame.CROSSHAIR_CURSOR);
/*
-:CURSOR OPTIONS:-
CROSSHAIR_CURSOR, DEFAULT_CURSOR,
E_RESIZE_CURSOR, HAND_CURSOR
MOVE_CURSOR, N_RESIZE_CURSOR,
NE_RESIZE_CURSOR, NW_RESIZE_CURSOR;
S_RESIZE_CURSOR,SE_RESIZE_CURSOR,
SW_RESIZE_CURSOR, TEXT_CURSOR,
W_RESIZE_CURSOR, WAIT_CURSOR
*/
}
public void mouseClicked(MouseEvent e)
{
if(!startGame)
{
clickX = e.getX();
clickY = e.getY();
if(clickX >= 550 && clickX <= 650 && clickY >= 375 && clickY <= 415)
startGame = true;
repaint();
e.consume();
}
}
public void keyPressed(KeyEvent e)
{
if(startGame && !gameOver)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
moveUp = true;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
moveDown = true;
repaint();
e.consume();
}
}
public void keyReleased(KeyEvent e)
{
moveUp = false;
moveDown = false;
repaint();
}
public void mouseMoved(MouseEvent e)
{
xPos = e.getX();
yPos = e.getY();
repaint();
e.consume();
}
public void mouseDragged(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void keyTyped(KeyEvent e){}
}