// // Pong game // // written by mike slattery - jan 2003 // // Simple game demonstrating use of KeyListener import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.*; public class Pong extends Applet implements Runnable { public static final int FrameWidth = 500; public static final int FrameHeight = 350; Thread anim; // paddle positions (right and left) int rpx, rpy; int lpx, lpy; // ball position and velocity int bx, by; int bdx, bdy; // score (and font to display score) int rpts = 0, lpts = 0; Font myFont; Image buffer; Graphics bufgr; // Keep state of input keys boolean rup = false; boolean rdown = false; boolean lup = false; boolean ldown = false; public void init() { bx = 70; by = 70; bdx = 7; bdy = 3; rpx = 450; rpy = 50; lpx = 50; lpy = 50; myFont = new Font("Courier", Font.BOLD, 24); addKeyListener (new keyBd()); addMouseListener (new mseL()); } public void start() { anim = new Thread(this); anim.start(); } public void stop() { anim = null; } public void update (Graphics g) { if (buffer == null) { buffer = createImage (FrameWidth, FrameHeight); bufgr = buffer.getGraphics(); } paint(bufgr); g.drawImage (buffer, 0, 0, null); } public void paint (Graphics g) { g.clearRect(0, 0, FrameWidth, FrameHeight); // Draw ball g.setColor(Color.red); g.fillOval(bx-10, by-10, 20, 20); // Draw paddles g.setColor(Color.blue); g.fillRect(rpx-4, rpy, 8, 30); g.fillRect(lpx-4, lpy, 8, 30); // Write score g.setColor(Color.black); g.setFont(myFont); g.drawString(""+lpts,80,310); g.drawString(""+rpts,340,310); } // thread starting point public void run () { while(anim != null) { // Move ball & paddles here and check for // collision of ball with walls & paddles // bx += bdx; by += bdy; // Check if the ball is past a paddle // and so one side scores if (bx < 20) { rpts++; bx = 430; by = (int)(70+20*Math.random()); bdx = -7; bdy = 4; } if (bx > Pong.FrameWidth-20) { lpts++; bx = 70; by = (int)(70+20*Math.random()); bdx = 7; bdy = 4; } // Check for bounce off top or bottom if (by < 30) bdy = -bdy; if (by > 270) bdy = -bdy; // Update paddles // Check each of the possible inputs if (rup) { if (rpy > 24) rpy -= 4; } if (rdown) { if (rpy < 296) rpy += 4; } if (lup) { if (lpy > 24) lpy -= 4; } if (ldown) { if (lpy < 296) lpy += 4; } /* * Check for paddles hitting ball */ if ((Math.abs(rpx-bx) < 14) && (by > rpy-10) && (by < rpy +40)) bdx = -bdx; if ((Math.abs(lpx-bx) < 14) && (by > lpy-10) && (by < lpy+40)) bdx = -bdx; repaint(); try { Thread.sleep(30); } catch (InterruptedException e) {System.err.println("Sleep catch:"+e);} } } private class keyBd extends KeyAdapter { public void keyPressed (KeyEvent e) { int code = e.getKeyCode(); switch (code) { case KeyEvent.VK_SEMICOLON: rup = true; break; case KeyEvent.VK_PERIOD: rdown = true; break; case KeyEvent.VK_A: lup = true; break; case KeyEvent.VK_Z: ldown = true; break; } } public void keyReleased (KeyEvent e) { int code = e.getKeyCode(); switch (code) { case KeyEvent.VK_SEMICOLON: rup = false; break; case KeyEvent.VK_PERIOD: rdown = false; break; case KeyEvent.VK_A: lup = false; break; case KeyEvent.VK_Z: ldown = false; break; } } } // // This is just a minimal mouse handler to allow us // to get keyboard focus (needed to receive keyboard // events). // private class mseL extends MouseAdapter { public void mouseClicked(MouseEvent e) { requestFocus(); } } }