Top

Pong Applet


Java Code
+ 2 likes
Please Register to submit score.
Bookmark and Share
Average Score  6.5 (of 2 scores)
Date Added  Apr 10, 2009
Last Updated  Apr 14, 2009
Tags  applet  ball  classic  game  java  paddle  ping  pong  site  swing 

Introduction

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:


Grab the Code

/**
 * 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

  (10)  RSS
Kirby
Comments: 475
 
Java Snippet:  Pong Applet
Posted on Apr 10, 2009 8:49 pm
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.
mountaindew
Comments: 1,826
 
Java Snippet:  Pong Applet
Posted on Apr 10, 2009 9:06 pm
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.
Aucun50
Comments: 548
 
Java Snippet:  Pong Applet
Posted on Apr 11, 2009 1:52 am
Very nice your getting to good at java :O
Kirby
Comments: 475
 
Java Snippet:  Pong Applet
Posted on Apr 11, 2009 4:13 pm
Good update on game reset.
Hawkee
Comments: 1,039
 
Java Snippet:  Pong Applet
Posted on Apr 11, 2009 11:47 pm
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.
mountaindew
Comments: 1,826
 
Java Snippet:  Pong Applet
Posted on Apr 14, 2009 12:25 pm
* 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
Hawkee
Comments: 1,039
 
Java Snippet:  Pong Applet
Posted on Apr 14, 2009 12:53 pm
Wow, increased the speed a "little" eh? More like a lot! I like the improvements though, good job.
mountaindew
Comments: 1,826
 
Java Snippet:  Pong Applet
Posted on Apr 14, 2009 2:30 pm
There has to be some challenge to it :)
Lindrian
Comments: 761
 
Java Snippet:  Pong Applet
Posted on Jun 16, 2009 4:26 am
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.
Joshuaxiong1
Comments: 127
 
Java Snippet:  Pong Applet
Posted on Nov 16, 2009 1:04 pm
awesome script moutaindew! You're the best!

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom