import java.applet.*; import java.awt.*; import java.awt.event.*; public class BallApp extends Applet implements KeyListener,Runnable, ActionListener { Graphics bufferGraphics; Image offscreen; Dimension dim; int rad = 10; int bwidth = 20; int ovspeed = 3; int ohspeed = 3; int pwidth = 20; int pheight = 60; int posx = 320; int posy = 240; int rbound = (640 - bwidth - rad); int lbound = (bwidth + rad); int ubound = (bwidth + rad); int bbound = (480 - bwidth - rad); int lpposx = (bwidth + (3*rad)); int lpposy = 200; int lpuedge = (lpposy - rad); int lpredge = (lpposx + rad + pwidth); int lpbedge = (lpposy + rad + pheight); int lpledge = (lpposx - rad); int rpposx = (640 - bwidth - pwidth - 3*rad); int rpposy = 200; int rpuedge = (rpposy - rad); int rpbedge = (rpposy + rad + pheight); int rpledge = (rpposx - rad); int rpredge = (rpposx + rad + pwidth); int p1score = 0; int p2score = 0; Button startButton, pauseButton, resetButton; boolean readygo = false; boolean startover = false; boolean lpmoveup = false; boolean lpmovedown = false; boolean rpmoveup = false; boolean rpmovedown = false; public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if (key == KeyEvent.VK_W){ lpmoveup = true;} else if (key == KeyEvent.VK_S){ lpmovedown = true;} if (key == KeyEvent.VK_UP){ rpmoveup = true;} else if (key == KeyEvent.VK_DOWN){ rpmovedown = true;} } public void lpMove(){ if (lpuedge <= ubound && lpmoveup){ return;} if (lpbedge >= bbound && lpmovedown){ return;} if (lpmoveup) { lpposy = lpposy - 4;} if (lpmovedown){ lpposy = lpposy + 4;} this.repaint(); } public void rpMove() { if (rpuedge <= ubound && rpmoveup){ return;} if (rpbedge >= bbound && rpmovedown){ return;} if (rpmoveup){ rpposy = rpposy - 4;} if (rpmovedown){ rpposy = rpposy + 4;} this.repaint(); } public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); if (key == KeyEvent.VK_W){ lpmoveup = false;} else if (key == KeyEvent.VK_S){ lpmovedown = false;} else if (key == KeyEvent.VK_UP){ rpmoveup = false;} else if (key == KeyEvent.VK_DOWN){ rpmovedown = false;} } public void keyTyped(KeyEvent e){} public void init(){ addKeyListener(this); dim = getSize(); offscreen = createImage(dim.width,dim.height); bufferGraphics = offscreen.getGraphics(); startButton = new Button ("Start"); pauseButton = new Button ("Pause"); resetButton = new Button ("Reset"); add(startButton); add(pauseButton); add(resetButton); startButton.addActionListener(this); pauseButton.addActionListener(this); resetButton.addActionListener(this); startButton.setFocusable(false); pauseButton.setFocusable(false); resetButton.setFocusable(false); pauseButton.setEnabled(false); resetButton.setEnabled(false); } public void actionPerformed(ActionEvent evt){ if (evt.getSource() == startButton){ startover = false; readygo = true; startButton.setEnabled(false); pauseButton.setEnabled(true); resetButton.setEnabled(true);} if (evt.getSource() == pauseButton){ readygo = false; startButton.setEnabled(true); pauseButton.setEnabled(false);} if (evt.getSource() == resetButton){ startover = true; startButton.setEnabled(true); pauseButton.setEnabled(false);} } public void start(){ Thread th = new Thread(this); th.start();} public void stop(){} public void destroy(){} public void run (){ while (true){ do{ try{ Thread.sleep (10);} catch (InterruptedException ex){} } while (readygo == false); Thread.currentThread().setPriority(Thread.MIN_PRIORITY); posx = posx + ohspeed; posy = posy + ovspeed; lpuedge = (lpposy - rad); lpredge = (lpposx + rad + pwidth); lpbedge = (lpposy + rad + pheight); lpledge = (lpposx - rad); rpuedge = (rpposy - rad); rpbedge = (rpposy + rad + pheight); rpledge = (rpposx - rad); rpredge = (rpposx + rad + pwidth); AudioClip bonk = getAudioClip(getDocumentBase(), "bonk.wav"); AudioClip bunk = getAudioClip(getDocumentBase(), "bunk.wav"); AudioClip bink = getAudioClip(getDocumentBase(), "bink.wav"); AudioClip score = getAudioClip(getDocumentBase(), "score.wav"); repaint(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); if (posx == lpledge && posy >= lpuedge && posy <= lpbedge){ bonk.play(); ohspeed = -Math.abs(ohspeed);} if (posx == lpredge && posy >= lpuedge && posy <= lpbedge){ bonk.play(); ohspeed = Math.abs(ohspeed);} if (posy == lpuedge && posx >= lpledge && posx <= lpredge){ bonk.play(); ovspeed = -Math.abs(ovspeed);} if (posy == lpbedge && posx <= lpredge && posx >= lpledge){ bonk.play(); ovspeed = Math.abs(ovspeed);} if (posx == rpledge && posy >= rpuedge && posy <= rpbedge){ bunk.play(); ohspeed = -Math.abs(ohspeed);} if (posx == rpredge && posy >= rpuedge && posy <= rpbedge){ bunk.play(); ohspeed = Math.abs(ohspeed);} if (posy == rpuedge && posx >= rpledge && posx <= rpredge){ bunk.play(); ovspeed = -Math.abs(ovspeed);} if (posy == rpbedge && posx >= rpledge && posx <= rpredge){ bunk.play(); ovspeed = Math.abs(ovspeed);} if (posx >= rbound){ bink.play(); ohspeed = -Math.abs(ohspeed);} if (posx <= lbound){ bink.play(); ohspeed = Math.abs(ohspeed);} if (posy >= bbound){ bink.play(); ovspeed = -Math.abs(ovspeed);} if (posy <= ubound){ bink.play(); ovspeed = Math.abs(ovspeed);} if (lpmovedown == true || lpmoveup == true){ lpMove();} if (rpmovedown == true || rpmoveup == true){ rpMove();} if (posx <= lbound && posy >= (dim.height/2) - pheight && posy <= (dim.height/2) + pheight){ p2score++; score.play(); posx = 320; posy = 240; lpposy = 200; rpposy = 200; repaint(); try{ Thread.sleep (5000);} catch (InterruptedException ex){} } if (posx >= rbound && posy >= (dim.height/2) - pheight && posy <= (dim.height/2) + pheight){ p1score++; score.play(); posx = 320; posy = 240; lpposy = 200; rpposy = 200; repaint(); try{ Thread.sleep (5000);} catch (InterruptedException ex){} } if (startover == true){ readygo = false; resetButton.setEnabled(false); p1score = 0; p2score = 0; posx = 320; posy = 240; lpposy = 200; rpposy = 200; repaint(); } } } public void paint(Graphics g){ bufferGraphics.setColor (Color.blue); bufferGraphics.fillRect (0,0,dim.width,dim.height); bufferGraphics.setColor (Color.white); bufferGraphics.fillRect (((dim.width/2) - 5),0,10,dim.height); bufferGraphics.fillOval (((dim.width/2) - (3*rad)),((dim.height/2) - (3*rad)),6*rad,6*rad); bufferGraphics.setColor (Color.orange); bufferGraphics.fillOval (posx - rad,posy - rad,2*rad,2*rad); bufferGraphics.setColor (Color.gray); bufferGraphics.fillRect (0,0,bwidth,dim.height); bufferGraphics.fillRect (bwidth,0,dim.width,bwidth); bufferGraphics.fillRect ((dim.width - bwidth),0,bwidth,dim.height); bufferGraphics.fillRect (bwidth,(dim.height - bwidth),dim.width,bwidth); bufferGraphics.setColor (Color.black); bufferGraphics.fillRect (0,(dim.height/2) - pheight,bwidth,2*pheight); bufferGraphics.fillRect ((dim.width - bwidth),(dim.height/2) - pheight,bwidth,2*pheight); bufferGraphics.drawString ("SCORE:",bwidth,dim.height - (bwidth/2)); bufferGraphics.drawString ("P1: " + p1score,(dim.width/3),dim.height - (bwidth/2)); bufferGraphics.drawString ("P2: " + p2score,((2*dim.width)/3),dim.height - (bwidth/2)); bufferGraphics.setColor (Color.white); bufferGraphics.fillRect (lpposx,lpposy,pwidth,pheight); bufferGraphics.fillRect (rpposx,rpposy,pwidth,pheight); g.drawImage(offscreen,0,0,this); } public void update(Graphics g){ paint(g);} }