/**
* Pong Applet
* Coded by Jordan
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pong extends Applet implements MouseMotionListener, KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int my,bx,by,px,py,xcomp,ycomp,width,height,xspeed,yspeed,ballwidth,ballheight,paddlewidth,paddleheight,score;
boolean started;
private Timer timer1;
public void init() {
setSize(1280,800);
width = getSize().width;
height = getSize().height;
setBackground(Color.white);
paddleheight = 120;
paddlewidth = 20;
ballheight = 30;
ballwidth = 30;
addKeyListener(this);
addMouseMotionListener(this);
px = 35;
xcomp = width - 35 - paddlewidth;
newgame();
timer1 = new Timer(10,new ActionListener() {
public void actionPerformed(ActionEvent e) {
height = getSize().height;
width = getSize().width;
bx += xspeed;
by += yspeed;
if (by <= 0 || by + ballheight >= height) {
yspeed = -yspeed;
}
if (bx <= px + paddlewidth && by + ballheight >= py && by <= py + paddleheight && bx > px) {
xspeed = -xspeed;
++score;
}
if (bx + ballwidth >= xcomp && by + ballheight >= ycomp && by <= ycomp + paddleheight && bx < xcomp + paddlewidth) {
xspeed = -xspeed;
}
if (xspeed < 0) {
if (ycomp + paddleheight / 2 != height / 2) {
if (ycomp + paddleheight / 2 > height / 2) {
ycomp -= -xspeed;
}
else {
ycomp += -xspeed;
}
}
}
else {
if (by + ballheight / 2 <= ycomp + paddleheight / 2) {
ycomp -= xspeed;
}
else {
ycomp += xspeed;
}
}
if (ycomp < 0) {
ycomp = 0;
}
if (ycomp + paddleheight > height) {
ycomp = height - paddleheight;
}
if (bx + ballwidth < 0) {
py = height / 2 - paddleheight / 2;
timer1.stop();
started = false;
}
repaint();
}
});
}
public void mouseMoved(MouseEvent e) {
if (started) {
my = e.getY();
if (my + paddleheight / 2 > height) {
my = height - paddleheight / 2;
}
if (my < paddleheight / 2) {
my = paddleheight / 2;
}
py = my - paddleheight / 2;
}
}
public void mouseDragged(MouseEvent e) { }
public void paint(Graphics g) {
Font font1 = new Font("Arial", Font.BOLD, 18);
Font font2 = new Font("Arial", Font.BOLD,70);
g.setColor(Color.black);
g.drawRect(0,0,width - 1,height - 1);
g.setColor(Color.gray);
g.fillRect(px,py,paddlewidth,paddleheight);
g.fillRect(xcomp,ycomp,paddlewidth,paddleheight);
g.setFont(font1);
g.setColor(Color.black);
g.drawString("Points: " + score,20,20);
if (started) {
g.fillArc(bx,by,ballwidth,ballheight,0,360);
}
else {
g.setFont(font2);
g.setColor(Color.blue);
g.drawString("Pong",width / 2 - 76,height / 2 - 16);
g.setFont(font1);
g.setColor(Color.cyan);
g.drawString("Use The Mouse To Control The Paddle",width / 2 - 145,height / 2 + 30);
g.drawString("Press 'Space' To Start",width / 2 - 85,height / 2 + 50);
}
}
public void newgame() {
py = height / 2 - paddleheight / 2;
ycomp = py;
bx = width / 2 - ballwidth / 2;
by = height / 2 - ballheight / 2;
xspeed = 14;
yspeed = 14;
score = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == ' ') {
started = true;
newgame();
timer1.start();
}
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}