Results 1 to 5 of 5
Thread: The bug in the Pong game
- 07-14-2009, 03:44 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
The bug in the Pong game
Hello everyone,
I am new to this forum. I hope, i will contribute more as time passes.
I attached the program below. Everything seems fine but the program doesn't start working.
Any kind of contribution would be appreciated.
Thanks in advance.
Note: (From administration) The original author of the attached code requested an indication of his credit. The original code is said to be here. Please note that we did not check the code but found this request reasonable! This note is added to respect intellectual rights and to inform users of the forum.Last edited by Java Tip; 11-19-2009 at 10:59 PM.
- 07-14-2009, 07:27 PM #2
Hi Phoeenix, welcome to Java Forums!
I moved the code in the main() method to the frame's constructor. It works now. ;) Also, when extending components, be sure to call the super() constructor.
Very cool game, by the way. :D Glad to have you on the forum!Java Code:import java.io.*; import java.awt.*; import javax.swing.*; public class Pong extends JFrame implements Runnable { //Variables Thread pongThread; //Will be used to turn this runnable object into a working thread PongArena arena = new PongArena(400, 600); //Make a pong arena //Constructors public Pong() { super(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tell it to close the program when the frame closes //Setup the layout and how things will be displayed... Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(arena, BorderLayout.CENTER); this.setTitle("Pong - By: Chris Canik"); this.setResizable(false); //This tells the JFrame that it should resize according //to the component's (PongArena) prefered size this.pack(); //The boundaries must be created here because getX and getY have no //accurate values when the arena's constructor is originally called arena.side1 = new PongArenaBoundary((int)(arena.getX() - 500), (int)(arena.getY() + arena.topPaddle.getHeight() + 30), (int)(500), (int)(arena.getBounds().getHeight() - arena.topPaddle.getHeight() - arena.bottomPaddle.getHeight() - 60)); arena.side2 = new PongArenaBoundary((int)(arena.getX() + arena.getBounds().getWidth()), (int)(arena.getY() + arena.topPaddle.getHeight() + 30), (int)(arena.getX() + arena.getBounds().getWidth() + 500), (int)(arena.getBounds().getHeight() - arena.topPaddle.getHeight() - arena.bottomPaddle.getHeight() - 60)); //Give the arena focus within the JPanel arena.requestFocus(); setVisible(true); //Show the frame to the player //Turn this Runnable JPanel into an actual thread pongThread = new Thread(this); pongThread.start(); } //Methods public void run() { boolean keepGoing = true; //Tracks whether there has not been an error while (keepGoing) { try { //Find out what the ball needs to bounce off of and do it arena.ball.bounce(arena.ball.needToBounce(arena)); } catch (BallOffCourtException ex) //Point has been scored { //Make a new ball for a new round, and register it to //the two AI's so they don't try to follow the old one arena.topAI.registerBall(arena.newBall((arena.getWidth() / 2) - 10, (arena.getHeight() / 2) - 10, 20, 20)); arena.bottomAI.registerBall(arena.ball); } //Display FPS in JPanel title this.setTitle("Pong (FPS: " + arena.fps.getFrames() + ") - By: Chris Canik"); try { //Redraw everything and wait a bit as math is handled repaint(); Thread.sleep(10); //(1000/10) = 100FPS } catch (InterruptedException ex) { //Error... Time to stop running! System.out.println("Interrupted!"); keepGoing = false; } } } //Main Method public static void main(String[] args) { Pong pongGame = new Pong(); //Lets make a game frame } }Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-14-2009, 07:33 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Thank you ! Thank you ! Thank you!
You can't imagine how pleasent I am.
- 07-14-2009, 07:46 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
hmm... tim, isn't the super-constructor called implicitly? You only need to use super if you're calling a super-constructor with arguments.
e.g
You don't ALWAYS need to call the super-constructor explicitly.Java Code:public class Sub extends Super{ public Sub(){ } } //the above is equivalent to this: public class Sub extends Super{ public Sub(){ super(); } } //but this is different, and requires arguments to satisfy the super-constructor public class Sub extends Super{ public Sub(Object arg){ super(arg); } }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-21-2009, 07:13 PM #5
Similar Threads
-
Ping pong game
By adam405 in forum New To JavaReplies: 8Last Post: 10-20-2010, 03:52 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Pong Paddle Not Stopping Ball At Certain Speed
By JDCAce in forum Java AppletsReplies: 3Last Post: 04-01-2009, 11:07 PM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Help with pong game
By Eric in forum New To JavaReplies: 2Last Post: 07-03-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks