// // // Pong - Java Pong Game // // Copyright (c) 2004-2007 by Knute Johnson. All rights reserved. // // Version 1.4 - 15 Apr 2007 - bug fix contributed by Phillip, ball could get // stuck on top edge, remove some of the null // spot at center of paddle to improve // playability and housekeeping // 1.3 - 09 Feb 2004 - add speed increase and improve playability // 1.2 - 09 Feb 2004 - change code to make it 1.1 compatible // 1.1 - 09 Feb 2004 - change static Colors to pre 1.4 // 1.0 - 08 Feb 2004 - incept // // package com.knutejohnson.games.pong; import java.applet.*; import java.awt.*; import java.awt.event.*; public class Pong extends Applet implements Runnable { static final String info = "Pong 1.4 - Copyright \u00a9 2004-2007 " + "Knute Johnson. All rights reserverd."; Thread thread; Image osi; // offscreen image for double buffering Graphics osg; // graphics of offscreen image int width,height; // dimensions of applet int ballX,ballY; // position of ball int veloX,veloY; // speed of ball int paddleX,paddleY; // location of paddle center AudioClip bink,bonk,buhh; // sound effects public void init() { width = getSize().width; height = getSize().height; ballX = width / 2; ballY = 1; paddleX = width / 2; paddleY = height - 20; osi = createImage(width,height); // create offscreen image osg = osi.getGraphics(); // get its graphics bink = getAudioClip(getClass().getResource("sounds/bink.wav")); bonk = getAudioClip(getClass().getResource("sounds/bonk.wav")); buhh = getAudioClip(getClass().getResource("sounds/buhh.wav")); enableEvents(MouseEvent.MOUSE_MOVED|MouseEvent.MOUSE_PRESSED); } public void start() { thread = new Thread(this); thread.start(); } public void stop() { thread.interrupt(); } public void processMouseMotionEvent(MouseEvent me) { // paddle follows mouse movement paddleX = me.getX(); if (paddleX < 20) paddleX = 20; else if (paddleX > width - 20) paddleX = width - 20; } public void processMouseEvent(MouseEvent me) { // release ball on mouse click if (me.getID() == MouseEvent.MOUSE_PRESSED) { ballX = width / 2; ballY = 1; veloX = 0; veloY = 6; } } public void run() { try { while (true) { // determine new ball position ballX += veloX; ballY += veloY; // if ball is hitting left or right edge, bounce off if (ballX <= 5 || ballX >= width - 5) { veloX = -veloX; bink.play(); // if ball is hitting top bounce off } if (ballY <= 0) { veloY = -veloY; bink.play(); // if ball has reached the bottom } if (ballY > height - 5) { ballX = width / 2; ballY = 1; veloX = veloY = 0; buhh.play(); // if ball is at paddle height } if (ballY > height - 30) { // if ball is within paddle width if (ballX >= paddleX - 3 && ballX <= paddleX + 3) { veloY = -veloY; // 3 pixel wide null if (veloX == 0 && Math.abs(ballX - paddleX) > 1) { if (ballX < paddleX) --veloX; else if (ballX > paddleX) ++veloX; } bonk.play(); } else if (ballX >= paddleX - 10 && ballX <= paddleX + 10) { if (ballX < paddleX) --veloX; else if (ballX > paddleX) ++veloX; veloY = -veloY; bonk.play(); } else if (ballX >= paddleX - 17 && ballX <= paddleX + 17) { if (ballX < paddleX) veloX -= 2; else if (ballX > paddleX) veloX += 2; veloY = -veloY; bonk.play(); } else if (ballX >= paddleX - 25 && ballX <= paddleX + 25) { if (ballX < paddleX) veloX -= 4; else if (ballX > paddleX) veloX += 4; veloY = -veloY; // increase y velocity if hit on edge of paddle if (veloY > 0) ++veloY; else --veloY; bonk.play(); } } // draw background osg.setColor(Color.gray); osg.fillRect(0,0,width,height); osg.setColor(Color.white); // draw ball osg.fillRect(ballX-5,ballY,10,10); // draw paddle osg.fillRect(paddleX-20,paddleY,40,10); repaint(); Thread.sleep(30); // 30 frames per second } } catch (InterruptedException ie) { } } public void paint(Graphics g) { g.drawImage(osi,0,0,width,height,this); } public void update(Graphics g) { paint(g); } public String getAppletInfo() { return info; } }