import java.applet.*; import java.awt.*; /* Java Pong by C. Neustaedter August 16, 2001 the following keys control the game: a - move bottom paddle left d - move bottom paddle right left arrow - move top paddle left right arrow - move top paddle right 1 - toggle top paddle to computer controlled 2 - toggle bottom paddle to computer controlled + speed up ball - slow down ball */ public class PongGame extends Applet implements Runnable { Thread t; private Image bufferImage; private Graphics buffer; private int width, height; // fields for both paddles private int p1x, p1y, p2x, p2y, pwidth, pheight, pspeed, p1score, p2score; private Color p1color, p2color; private int p1move, p2move; // fields for the ball private int ballx, bally, ballspeedx, ballspeedy, ballsize; private Color ballcolor; private String message; private boolean pause; // true if the game is paused, false if the game is not paused private boolean ai1; // if player 1 is controlled by the computer private boolean ai2; // if player 2 is controlled by the computer private Font myFont; public void init() { myFont = new Font("Helvetica", Font.BOLD, 24); setFont(myFont); pause =true; ai1 = false; // player 1 not controlled by computer ai2 = false; // player 2 not controlled by computer t=new Thread(this); t.start(); width=size().width; height=size().height; bufferImage = createImage(width, height); buffer=bufferImage.getGraphics(); p1x=p2x=width/2; p1y=30; p2y=height-30; pwidth=75; pheight=10; pspeed=4; p1move = 0; p2move = 0; message = "Click to Start"; p1score=0; p2score=0; p1color=Color.yellow; p2color=Color.red; ballx=width/2; bally = height/2; ballspeedx=ballspeedy=2; ballsize=10; ballcolor=Color.blue; } public void run() { while(true) { repaint(); try{ t.sleep(5); }catch(InterruptedException e) { ; } if (!pause) { // --------PADDLE MOVEMENT if (!ai1) // if not computer controlled p1x += p1move; else // computer controlled p1x = ballx-30; if (!ai2) // if not computer controlled p2x += p2move; else // computer controlled p2x = ballx-30; // --------KEEP PADDLE ON THE SCREEN if (p1x < 0) // gone off the left side p1x = 0; if (p1x+pwidth > width) // gone off the right side p1x = width - pwidth; if (p2x < 0) // gone off the left side p2x = 0; if (p2x+pwidth > width) // gone off the right side p2x = width - pwidth; // -------BALL MOVEMENT ---------------- ballx += ballspeedx; bally += ballspeedy; if (ballx+ballsize > width) // ball hits the right wall ballspeedx *= -1; // reverse the x direction if (ballx < 0) // ball hits the left wall ballspeedx *= -1; if (bally < 0) // ball hits top wall, goal for p2 { // move ball back to the middle ballx=width/2; bally=height/2; ballspeedy *=-1; // reverse direction p2score++; // p2 gets a point } if (bally > height) // ball hits bottom wall, goal for p1 { // move ball back to the middle ballx=width/2; bally=height/2; p1score+=1; ballspeedy *=-1; // reverse direction } if ((ballx > p2x) && (ballx < p2x + pwidth) && (bally+ballsize >p2y) && (bally < p2y +pheight)) // ball hits bottom paddle, confusing ballspeedy *= -1; if ((ballx > p1x) && (ballx < p1x + pwidth) && (bally < p1y + pheight) && (bally > p1y)) // ball hits top paddle, still confusing ballspeedy *= -1; } } } public void paint (Graphics g) { buffer.setColor(Color.black); buffer.fillRect(0,0,width,height); buffer.setColor(p1color); buffer.fillRect(p1x, p1y, pwidth, pheight); //draw paddle1 (p1) buffer.setColor(p2color); buffer.fillRect(p2x, p2y, pwidth, pheight); //draw paddle2 (p2) buffer.setColor(ballcolor); buffer.fillOval(ballx, bally, ballsize, ballsize); // draw ball buffer.setColor(Color.red); buffer.drawString(Integer.toString(p2score), width-30, height/2); // draw p2 score buffer.setColor(Color.yellow); buffer.drawString( Integer.toString(p1score), 20, height/2); // draw p1 score buffer.setColor(Color.green); buffer.drawString(message, 130, height /2); g.drawImage(bufferImage, 0, 0, this); // copy buffer to applet } public void update(Graphics g) { paint(g); } public boolean keyDown(Event e, int key) { if (key == 1006) // left arrow key p1move = pspeed * -1; if (key == 1007) // right arrow key p1move = pspeed; if (key == 97) // a key p2move=pspeed * -1; if (key == 100) p2move= pspeed; // d key if (key == 112) // p key if (pause == true) pause = false; else pause = true; if (key == 49) // 1 key, computer control if (ai1 == true) ai1 = false; else ai1 = true; if (key == 50) // 2 key, computer control if (ai2 == true) ai2 = false; else ai2 = true; if (key == 61) // +/= key { ballspeedx*=2; ballspeedy*=2; } if (key == 45) // - key { // reset the speed if (ballspeedx / 2 >= 2) { ballspeedx/=2; ballspeedy/=2; } } return true; } public boolean keyUp(Event e, int key) { if (key == 1006) // left arrow key p1move = 0; if (key == 1007) // right arrow key p1move = 0; if (key == 97) // a key p2move=0; if (key == 100) p2move= 0; // d key return true; } public boolean lostFocus(Event e, Object obj) { message = "Click to Start"; pause = true; return super.lostFocus(e, obj); } public boolean gotFocus(Event e, Object obj) { message = ""; pause = false; return super.gotFocus(e, obj); } }