Pong Applet

Platform:  Java
Published  Apr 10, 2009
Updated  Apr 14, 2009
This is a Pong game applet that I made in Java. It's player vs. computer, and the computer never loses. You just move the mouse up and down to control your paddle. I'd like to figure out how to change the speeds of the ball's x and y based on where it hits on the paddle, so it's not "predicted" where the ball will go.

You can play it right here! (As long as you've installed the java plugin, obviously...)

Screen:
/**
* Pong Applet
* Coded by MountainDew
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pong extends Applet implements MouseMotionListener, KeyListener {

int my,bx,by,px,py,compx,compy,width,height,speedx,speedy,bwidth,bheight,pwidth,pheight,score;
boolean started;
private Timer timer1;

public void init() {
setSize(800,500);
width = getSize().width;
height = getSize().height;
setBackground(Color.black);
pheight = 120;
pwidth = 15;
bheight = 30;
bwidth = 30;
addKeyListener(this);
addMouseMotionListener(this);
px = 35;
compx = width - 35 - pwidth;
newgame();
timer1 = new Timer(10,new ActionListener() {
public void actionPerformed(ActionEvent e) {
height = getSize().height;
width = getSize().width;
bx += speedx;
by += speedy;
if (by <= 0 || by + bheight >= height) {
speedy = -speedy;
}
if (bx <= px + pwidth && by + bheight >= py && by <= py + pheight && bx > px) {
speedx = -speedx;
++score;
}
if (bx + bwidth >= compx && by + bheight >= compy && by <= compy + pheight && bx < compx + pwidth) {
speedx = -speedx;
}
if (speedx < 0) {
if (compy + pheight / 2 != height / 2) {
if (compy + pheight / 2 > height / 2) {
compy -= -speedx;
}
else {
compy += -speedx;
}
}
}
else {
if (by + bheight / 2 <= compy + pheight / 2) {
compy -= speedx;
}
else {
compy += speedx;
}
}
if (compy < 0) {
compy = 0;
}
if (compy + pheight > height) {
compy = height - pheight;
}
if (bx + bwidth < 0) {
py = height / 2 - pheight / 2;
timer1.stop();
started = false;
}
repaint();
}
});
}

public void mouseMoved(MouseEvent e) {
if (started) {
my = e.getY();
if (my + pheight / 2 > height) {
my = height - pheight / 2;
}
if (my < pheight / 2) {
my = pheight / 2;
}
py = my - pheight / 2;
repaint();
}
}

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,40);
g.setColor(Color.white);
g.drawRect(0,0,width - 1,height - 1);
g.fillRect(px,py,pwidth,pheight);
g.fillRect(compx,compy,pwidth,pheight);
g.setFont(font1);
g.drawString("Score: " + score,20,20);
if (started) {
g.fillArc(bx,by,bwidth,bheight,0,360);
}
else {
g.setFont(font2);
g.setColor(Color.green);
g.drawString("Pong",width / 2 - 46,height / 2 - 16);
g.setFont(font1);
g.drawString("Press 's' to start",width / 2 - 69,height / 2 + 30);
}
}

public void newgame() {
py = height / 2 - pheight / 2;
compy = py;
bx = width / 2 - bwidth / 2;
by = height / 2 - bheight / 2;
speedx = 10;
speedy = 10;
score = 0;
}

public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 's') {
started = true;
newgame();
timer1.start();
}
}

public void keyTyped(KeyEvent e) { }

public void keyReleased(KeyEvent e) { }

}

Comments

Sign in to comment.
Hawkee   -  Mar 15, 2011
Hey mountain, you can post these as apps now with the .jar file included in the zip to make them playable on the site.
 Respond  
Joshuaxiong1   -  Nov 16, 2009
awesome script moutaindew! You're the best!
 Respond  
Lindrian   -  Jun 16, 2009
How about some double buffering? If you need help doing it, I'd be glad to help. You know where to find me :)! (yes, it is very fast too hehe)

Also, I would add this.requestfocus(); to the init event.
 Respond  
mountaindew   -  Apr 14, 2009
There has to be some challenge to it :)
 Respond  
Hawkee   -  Apr 14, 2009
Wow, increased the speed a "little" eh? More like a lot! I like the improvements though, good job.
 Respond  
mountaindew   -  Apr 14, 2009
* Added a little title screen; will go to that when you lose
* Increased the speed a little
* Added a score based on how many returns you hit
 Respond  
Hawkee   -  Apr 11, 2009
Very cool, I just compiled and ran it on Eclipse and it worked perfectly. I just couldn't figure out how to restart the game after I lost. Would also be nice if it kept score and there was a way to actually trick the computer into missing.
 Respond  
Kirby   -  Apr 11, 2009
Good update on game reset.
 Respond  
Aucun50   -  Apr 11, 2009
Very nice your getting to good at java :O
 Respond  
mountaindew   -  Apr 10, 2009
Forgot about that, updated. Tomorrow I think I'm gonna try to figure out the varied speed and make the computer "vulnerable", so there can be a score and stuff.
 Respond  
Kirby   -  Apr 10, 2009
Cool md.
I played it but when I lost, the ball couldn't be reset unless I refreshed the page. Want to add a macro to restart the game? Would be nice.
Unless, that's also what you meant by

Quote

so it's not "predicted" where the ball will go.
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.