Created
September 6, 2013 08:38
-
-
Save AnimeshShaw/6461133 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.applet.Applet; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class PongGame extends Applet implements Runnable { | |
Thread th; | |
boolean rbup = false; | |
boolean rbdown = false; | |
boolean lbup = false; | |
boolean lbdown = false; | |
final int TOP = 20; | |
final int BOTTOM = 280; | |
int rbpx, rbpy, lbpx, lbpy; | |
int ballX, ballY, balldx, balldy; | |
int rpoints, lpoints; | |
Image buf; | |
Graphics buffergr; | |
public static final int frameWidth = 500; | |
public static final int frameHeight = 350; | |
public void start() { | |
th = new Thread(this); | |
th.start(); | |
} | |
public void stop() { | |
th = null; | |
} | |
public void run() { | |
while (th != null) { | |
controlBars(); | |
ballUpdate(); | |
repaint(); | |
try { | |
Thread.sleep(30); | |
} catch (InterruptedException e) { | |
System.err.println("Sleep catch:" + e); | |
} | |
} | |
} | |
public void init() { | |
setBackground(new Color(26, 26, 26)); | |
setSize(500, 350); | |
ballX = 70; | |
ballY = 70; | |
balldx = 7; | |
balldy = 3; | |
rbpx = 450; | |
rbpy = 50; | |
lbpx = 50; | |
lbpy = 50; | |
addKeyListener(new keypadControl()); | |
addMouseListener(new mouseControl()); | |
} | |
public void paint(Graphics g) { | |
g.clearRect(0, 0, frameWidth, frameHeight); | |
//Draw Ball | |
g.fillOval(ballX - 10, ballY - 10, 20, 20); | |
g.setColor(new Color(30,164,16)); | |
//Draw Bars | |
g.fillRect(rbpx - 4, rbpy, 8, 30); | |
g.fillRect(lbpx - 4, lbpy, 8, 30); | |
g.setColor(new Color(255, 82, 82)); | |
g.drawString("" + lpoints, 80, 310); | |
g.drawString("" + rpoints, 340, 310); | |
} | |
public void update(Graphics g) { | |
if (buf == null) { | |
buf = createImage(frameWidth, frameHeight); | |
buffergr = buf.getGraphics(); | |
} | |
paint(buffergr); | |
g.drawImage(buf, 0, 0, null); | |
} | |
private class keypadControl extends KeyAdapter { | |
public void keyPressed(KeyEvent e) { | |
switch (e.getKeyCode()) { | |
case KeyEvent.VK_Q: | |
lbup = true; | |
break; | |
case KeyEvent.VK_A: | |
lbdown = true; | |
break; | |
case KeyEvent.VK_W: | |
rbup = true; | |
break; | |
case KeyEvent.VK_S: | |
rbdown = true; | |
break; | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
switch (e.getKeyCode()) { | |
case KeyEvent.VK_Q: | |
lbup = false; | |
break; | |
case KeyEvent.VK_A: | |
lbdown = false; | |
break; | |
case KeyEvent.VK_W: | |
rbup = false; | |
break; | |
case KeyEvent.VK_S: | |
rbdown = false; | |
break; | |
} | |
} | |
} | |
private class mouseControl extends MouseAdapter { | |
public void mouseClicked(MouseEvent e) { | |
requestFocus(); | |
} | |
} | |
void controlBars() { | |
if (rbup) { | |
if (rbpy > TOP + 4) { | |
rbpy -= 4; | |
} | |
} | |
if (rbdown) { | |
if (rbpy + 30 < BOTTOM - 4) { | |
rbpy += 4; | |
} | |
} | |
if (lbup) { | |
if (lbpy > TOP + 4) { | |
lbpy -= 4; | |
} | |
} | |
if (lbdown) { | |
if (lbpy + 30 < BOTTOM - 4) { | |
lbpy += 4; | |
} | |
} | |
} | |
void ballUpdate() { | |
ballX += balldx; | |
ballY += balldy; | |
if (ballX < 20) { | |
rpoints++; | |
ballX = 430; | |
ballY = (int) (70 + 20 * Math.random()); | |
balldx = -7; | |
balldy = 4; | |
} | |
if (ballX > PongGame.frameWidth - 20) { | |
lpoints++; | |
ballX = 70; | |
ballY = (int) (70 + 20 * Math.random()); | |
balldx = 7; | |
balldy = 4; | |
} | |
if (ballY - 10 < TOP) { | |
balldy = -balldy; | |
} | |
if (ballY + 10 > BOTTOM) { | |
balldy = -balldy; | |
} | |
if ((Math.abs(rbpx - ballX) < 14) && (ballY > rbpy - 10) && (ballY < rbpy + 40)) { | |
balldx = -balldx; | |
} | |
if ((Math.abs(lbpx - ballX) < 14) && (ballY > lbpy - 10) && (ballY < lbpy + 40)) { | |
balldx = -balldx; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment