[Java Game]Pong Game implemented in Java 05-18-2013, 01:08 PM
#1
Hello [username],
I have made a simple pong game and I hope you like it. I remember the day when I made this game in C and it was tough as I had to write a lot more.
Screenshot
I have made a simple pong game and I hope you like it. I remember the day when I made this game in C and it was tough as I had to write a lot more.
Code:
package pong;
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;
}
}
}Screenshot
Spoiler:
Wait wait!!! Not so fast. You have just seen the codes so you might want to try it. What if I tell you that yes you can try it online. Yes Dear [username] :angel:, you can see it live.
Spoiler:
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)
, I didn't wanted to miss the opportunity to apply java and make good use of these applets. So I made it like that. I will buy a domain soon for my Java articles that I will write, my applet games and other swing GUI's that are very soon to come. Seeing something live is better than reading the code I guess.