Sinisterly
[Java Game]Pong Game implemented in Java - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: [Java Game]Pong Game implemented in Java (/Thread-Java-Game-Pong-Game-implemented-in-Java)

Pages: 1 2


[Java Game]Pong Game implemented in Java - Psycho_Coder - 05-18-2013

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.

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:




[Java Game]Pong Game implemented in Java - Psycho_Coder - 05-18-2013

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.

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:




RE: [Java Game]Pong Game implemented in Java - TheDeceptionist - 05-18-2013

Thats really cool.
#InspiriationToLearnJava


RE: [Java Game]Pong Game implemented in Java - TheDeceptionist - 05-18-2013

Thats really cool.
#InspiriationToLearnJava


RE: [Java Game]Pong Game implemented in Java - ArkPhaze - 05-20-2013

Not bad, lots of hardcoded values though which makes code harder to change around. I'm not a Java programmer, but couldn't you do something like this?

Code:
if (lbdown && lbpy + 30 < BOTTOM - 4) lbpy += 4; }

Instead of this:
Code:
if (lbdown) { if (lbpy + 30 < BOTTOM - 4) { lbpy += 4; } }



RE: [Java Game]Pong Game implemented in Java - Psycho_Coder - 05-21-2013

(05-20-2013, 10:37 PM)ArkPhaze Wrote: Not bad, lots of hardcoded values though which makes code harder to change around. I'm not a Java programmer, but couldn't you do something like this?

Code:
if (lbdown && lbpy + 30 < BOTTOM - 4) lbpy += 4; }

Instead of this:
Code:
if (lbdown) { if (lbpy + 30 < BOTTOM - 4) { lbpy += 4; } }

Thank you for pointing out I will edit them later.


RE: [Java Game]Pong Game implemented in Java - ArkPhaze - 05-21-2013

If I am correct, just like C# && should short-circuit that condition and if the first returns false then it won't evaluate the second.


RE: [Java Game]Pong Game implemented in Java - Deque - 05-21-2013

I agree with ArkPhaze. Give those values a meaning by creating constants for them (as Java has no constants make static final fields or locally final variables).
Example:

Code:
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; } }

Now tell me in how many places you have to change the code if you want to change the (i suppose) paddle speed from 4 to something else?
Too many places. This is prone to bugs, because you might forget a place. So keep it simple and well readable: Create a constant for the paddlespeed.

Same is true for every other number. Every number you use, should be given a meaning somewhere.

Code:
public static final int frameWidth = 500; public static final int frameHeight = 350;

Constants (or the equivalent of them) have to be written in capitals, words are separated by _.
Example:

FRAME_WIDTH
FRAME_HEIGHT

Please correct this.

Add access modifiers to your methods and fields. They are missing in most places, which makes them accessable for other classes in the same package. Keep care for correct encapsulation.

Add @Override annotations for the methods you override.

Code:
if ((Math.abs(rbpx - ballX) < 14) && (ballY > rbpy - 10) && (ballY < rbpy + 40)) { if ((Math.abs(lbpx - ballX) < 14) && (ballY > lbpy - 10) && (ballY < lbpy + 40)) {

Give these long conditional statements a meaning by extracting them into a well named method that returns a boolean. In this case you can have one method for both that takes either lbpx or rbpx.
Variable names like lbpx and rbpx are not really good for readability. Other people reading the code will most likely not understand what they are for.

Cool Screenshot Tongue
It is good to see you do some Java.



Quote:If I am correct, just like C# && should short-circuit that condition and if the first returns false then it won't evaluate the second.

@ArkPhaze: That is correct.


RE: [Java Game]Pong Game implemented in Java - Psycho_Coder - 05-21-2013

(05-21-2013, 12:21 PM)Deque Wrote: I agree with ArkPhaze. Give those values a meaning by creating constants for them (as Java has no constants make static final fields or locally final variables).
Example:

Code:
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; } }

Now tell me in how many places you have to change the code if you want to change the (i suppose) paddle speed from 4 to something else?
Too many places. This is prone to bugs, because you might forget a place. So keep it simple and well readable: Create a constant for the paddlespeed.

Same is true for every other number. Every number you use, should be given a meaning somewhere.

Code:
public static final int frameWidth = 500; public static final int frameHeight = 350;

Constants (or the equivalent of them) have to be written in capitals, words are separated by _.
Example:

FRAME_WIDTH
FRAME_HEIGHT

Please correct this.

Add access modifiers to your methods and fields. They are missing in most places, which makes them accessable for other classes in the same package. Keep care for correct encapsulation.

Add @Override annotations for the methods you override.

Code:
if ((Math.abs(rbpx - ballX) < 14) && (ballY > rbpy - 10) && (ballY < rbpy + 40)) { if ((Math.abs(lbpx - ballX) < 14) && (ballY > lbpy - 10) && (ballY < lbpy + 40)) {

Give these long conditional statements a meaning by extracting them into a well named method that returns a boolean. In this case you can have one method for both that takes either lbpx or rbpx.
Variable names like lbpx and rbpx are not really good for readability. Other people reading the code will most likely not understand what they are for.

Cool Screenshot Tongue
It is good to see you do some Java.

Ok Mam, I will do all the editing stuff soon. I made this game in a hurry and the reason for such hurry I don't remember, I knew that I had to make the constants names, but I though that I will later update these. But I will take concern not to apply shortcut techniques (I thought many users will understand what the code does).

As always you are very much correct, I still need to get better a looooot better.

About the Screenshot Biggrin , 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.

But I don't like this game now as its very hard to use keyboards for playing.

My version v2 includes :-
1. Computer AI
2. Play with Mouse or Keys.
3. Make it faster.
4. Good background for the applet.

Thank you,
Sincerely,
Psycho_Coder,


RE: [Java Game]Pong Game implemented in Java - ArkPhaze - 05-21-2013

Looking forward to seeing v2 Psycho_Coder. Smile