import java.lang.*; import java.awt.*; import java.applet.*; public class Pong extends Applet implements Runnable { boolean initializing = true; boolean done = false; boolean start = false; boolean hit = false; int easiness; int enemyscore,playerscore; Thread game; Point mouse; Point ball; double ball_dx, ball_dy; int ballRadius; Rectangle player, enemy, walls; . AudioClip wavPong; Image doublebuffer; Image bgImage; Font scoreFont; public void init(){ int x,y,w,h; mouse = new Point( 0, 0 ); doublebuffer = createImage( getWidth(), getHeight() ); scoreFont = new Font( "Impact", Font.PLAIN, (int)( getHeight()*0.15) ); x = (int)( 0.01 * getWidth() ); y = (int)( 0.01 * getHeight() ); walls = new Rectangle( x, y, getWidth() - x*2, getHeight() - y*2 ); w = (int)( 0.02 * getWidth() ); h = (int)( 0.35 * getHeight() ); x = (int)( walls.x + w ); player = new Rectangle( x, getHeight()/2, w, h ); x = (int)( ( walls.width + walls.x ) - w ); enemy = new Rectangle( x, getHeight()/2, w, h ); easiness = 20; ballRadius = 16; ball = new Point( getWidth() / 2, getHeight() / 2 ); enemyscore = 0; playerscore = 0; } public void start() { wavPong = getAudioClip( getCodeBase(), "pong.wav" ); bgImage = getImage( getCodeBase(), "kalle.Gif" ); initializing = false; game = new Thread( this ); game.start(); } public void stop() { done = true; } public boolean mouseDown( Event e, int x, int y ) { if ( start == false ) startGame(); return true; } public boolean mouseMove( Event e, int x, int y ) { mouse.x = x; mouse.y = y; return true; } public void run() { while( !done ) { testPlayer(); testEnemy(); testWalls(); updatePlayer(); updateEnemy(); updateBall(); playSounds(); repaint(); try { Thread.sleep( 1000/60 ); } catch( Exception e ){} } } void startGame() { double speedX = 0.006; double speedY = 0.004; start = true; ball.x = getWidth() / 2; ball.y = getHeight() / 2; boolean isEven; double speedScale; isEven = ( (int)( Math.random() * 10) % 2 == 0 ); ball_dx = speedX*getWidth() * ( isEven ? -1 : 1 ); isEven = ( (int)( Math.random() * 10) % 2 == 0 ); ball_dy = speedY*getHeight() * ( isEven ? -1 : 1 ); } void testWalls() { int left = walls.x; int top = walls.y; int right = walls.x + walls.width; int bottom = walls.y + walls.height; if ( ( ball.x + ball_dx - ballRadius ) <= left ) score(); if ( ( ball.x + ball_dx + ballRadius) >= right ) score(); if ( ( ball.y + ball_dy - ballRadius) <= top ) ball_dy *= -1; if ( ( ball.y + ball_dy + ballRadius) >= bottom ) ball_dy *= -1; } void testPlayer() { if ( ball_dx < 0 ) { int right = player.x + player.width / 2; int top = player.y - player.height / 2; int bottom = player.y + player.height / 2; if ( (ball.x - ballRadius) <= right ) { if ( ball.y + ballRadius > top && ball.y - ballRadius < bottom ) { ball_dx *= -1.05; ball_dy *= 1.05; hit = true; } } } } void testEnemy() { if ( ball_dx > 0 ) { int left = enemy.x - player.width / 2; int top = enemy.y - player.height / 2; int bottom = enemy.y + player.height / 2; if ( (ball.x + ballRadius) >= left ) { if ( ball.y + ballRadius > top && ball.y - ballRadius < bottom ) { ball_dx *= -1.05; ball_dy *= 1.05; hit = true; } } } } void playSounds() { if ( hit ) { wavPong.play(); hit = false; } } void score() { if ( ball_dx > 0 ) { playerscore++; easiness *= 0.80; } else { enemyscore++; } ball.x = getWidth() / 2; ball.y = getHeight() / 2; ball_dx = 0; ball_dy = 0; start = false; } void updatePlayer() { player.y = mouse.y; } void updateEnemy() { int yDistanceToBall; if (ball_dx > 0) { yDistanceToBall = Math.abs( ball.y - enemy.y ); if ( ball.y + ball_dy < enemy.y ) enemy.y += ( -1 * yDistanceToBall / easiness ); else if ( ball.y + ball_dy > enemy.y ) enemy.y += ( yDistanceToBall / easiness ); } else { if ( enemy.y < getHeight() / 2 - 1 ) enemy.y += 2; else if ( enemy.y > getHeight() / 2 + 1 ) enemy.y += -2; } } void updateBall() { ball.x += ball_dx; ball.y += ball_dy; } public void update( Graphics g ) { paint(g); } public void paint( Graphics g ) { int x,y; if ( initializing ) { g.drawString( "Initializing...", getWidth() - 15, getHeight() / 2 ); return; } Graphics dbG = doublebuffer.getGraphics(); dbG.setColor( Color.RED ); dbG.fillRect(0, 0, getWidth(), getHeight() ); // Draw background image, centered. /* dbG.drawImage( bgImage, getWidth() / 2 - bgImage.getWidth( this ) / 2, getHeight() / 2 - bgImage.getHeight( this ) / 2, this ); */ dbG.drawImage( bgImage, 0, 0, getWidth(), getHeight(), 0, 0, bgImage.getWidth(this), bgImage.getHeight(this), this ); // Draw Score board, center and above playfield. dbG.setColor( Color.YELLOW ); dbG.setFont( scoreFont ); int len; len = dbG.getFontMetrics().stringWidth( "00" ); y = (int)( 0.025*getWidth() + dbG.getFontMetrics().getHeight() ); x = (int)( getWidth() / 4 - len/2 ); dbG.drawString( formatScore( playerscore ), x, y ); x = (int)( getWidth() / 4*3 - len/2 ); dbG.drawString( formatScore( enemyscore ), x, y ); dbG.setColor( Color.BLACK ); dbG.drawRect( walls.x-1, walls.y-1, walls.width+2, walls.height+2 ); dbG.drawRect( walls.x+1, walls.y+1, walls.width-2, walls.height-2 ); dbG.setColor( Color.WHITE ); dbG.drawRect( walls.x, walls.y, walls.width, walls.height ); dbG.setColor( Color.BLACK ); dbG.fillOval( ball.x-ballRadius, ball.y-ballRadius, ballRadius*2, ballRadius*2 ); dbG.setColor( Color.WHITE ); dbG.fillOval( ball.x-ballRadius+2, ball.y-ballRadius+2, ballRadius*2 - 4, ballRadius*2 - 4 ); dbG.setColor( Color.BLACK ); dbG.fillRect( player.x - player.width/2, player.y - player.height/2, player.width, player.height ); dbG.setColor( Color.WHITE ); dbG.fillRect( player.x - player.width/2 + 2, player.y - player.height/2 + 2, player.width - 4 , player.height - 4 ); dbG.setColor( Color.BLACK ); dbG.fillRect( enemy.x - enemy.width/2, enemy.y - enemy.height/2, enemy.width, enemy.height ); dbG.setColor( Color.WHITE ); dbG.fillRect( enemy.x - enemy.width/2 + 2, enemy.y - enemy.height/2 + 2, enemy.width - 4, enemy.height - 4 ); g.drawImage(doublebuffer, 0, 0, this ); dbG.dispose(); } String formatScore( int score ) { String str; if (score >= 99 ) str = "99"; else if (score < 10) str = "0" + score; else str = "" + score; return str; } }