943,654 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 817
  • Java RSS
Dec 13th, 2008
0

Problem with Pong

Expand Post »
Ok, i'm trying to make two player, no-network pong. i don't know what the problem is, there are some very strange things happening...

at the beginning of the program i try to set a y value to half the height, but it doesn't it stays at y=0
when the user tries to use the up (up arrow/w key) button it works like it should, but when the user presses down (down arrow/s key) the entire program freezes, but the close button still works (basically the animation freezes)

theres quite a bit of code, four classes:
Main.java
java Syntax (Toggle Plain Text)
  1. import java.awt.* ;
  2. import java.awt.event.* ;
  3. import java.awt.geom.* ;
  4. import javax.swing.* ;
  5. import javax.swing.event.* ;
  6. import java.util.concurrent.* ;
  7.  
  8. /**
  9.  * Write a description of class Main here.
  10.  * @author William Peckham
  11.  * @version 1.0A
  12.  */
  13. public class Main extends JFrame {
  14. PlayArea pa = new PlayArea ( ) ;
  15.  
  16. /**make a new Main*/
  17. public Main ( ) {
  18. this.setTitle ( "Pong Remake" ) ;
  19. this.add ( pa , BorderLayout.WEST ) ;
  20. this.pack ( ) ;
  21. this.setVisible ( true ) ;
  22. ScheduledThreadPoolExecutor executer = new ScheduledThreadPoolExecutor ( 3 ) ;
  23. executer.scheduleAtFixedRate ( new Thread ( ) {
  24. public void run ( ) {
  25. pa.run ( ) ;
  26. }
  27. } , 0L , 100L , TimeUnit.MILLISECONDS ) ;
  28. }
  29.  
  30. /**main*/
  31. public static void main ( String [ ] args ) {
  32. new Main ( ) ;
  33. }
  34. }
PlayArea.java
java Syntax (Toggle Plain Text)
  1. import java.awt.* ;
  2. import java.awt.event.* ;
  3. import java.awt.geom.* ;
  4. import javax.swing.* ;
  5. import javax.swing.event.* ;
  6.  
  7. /**
  8.  * This Class holds the code for the game.
  9.  * @author William Peckham
  10.  * @version 1.0A
  11.  */
  12. public class PlayArea extends JComponent {
  13. /**Variable for the Hieght of the Play Area*/
  14. public int height = 300 ;
  15. /**Variable for the Width of the Play Area*/
  16. public int width = 600 ;
  17. /**Because this is Pong we need a Ball*/
  18. public Ball ball ;
  19. /**This will be player one's paddle*/
  20. public Paddle player1 ;
  21. /**This will be player two's paddle*/
  22. public Paddle player2 ;
  23. /**This variable will hold player one's score*/
  24. public int score1 = 0 ;
  25. /**This variable will hold player two's score*/
  26. public int score2 = 0 ;
  27. /**This variable will keep track of whether the game is paused
  28.   * it's default is false, so the game will not be paused at start*/
  29. public boolean paused = false ;
  30. /**variable will keep track of player one's up ('w') key*/
  31. public volatile boolean wpressed = false ;
  32. /**variable will keep track of player one's down ('s') key*/
  33. public volatile boolean spressed = false ;
  34. /**variable will keep track of player two's up (up arrow) key*/
  35. public volatile boolean uppressed = false ;
  36. /**variable will keep track of player two's down (down arrow) key*/
  37. public volatile boolean downpressed = false ;
  38.  
  39. /**make a new PlayArea*/
  40. public PlayArea ( ) {
  41. System.out.println ( "djkl;" ) ;
  42. this.setSize ( width , height ) ;
  43. this.setPreferredSize ( new Dimension ( width , height ) ) ;
  44. this.addKeyListener ( new keylistener ( ) ) ;
  45. setFocusable ( true ) ;
  46. requestFocusInWindow ( ) ;
  47. ball = new Ball ( this ) ;
  48. player1 = new Paddle ( this , 0 ) ;
  49. player2 = new Paddle ( this , 0 ) ;
  50. player2.x = width - player2.width ;
  51. }
  52.  
  53. public void run ( ) {
  54. if ( ! paused ) {
  55. if ( wpressed ) {
  56. player1.step ( true , true ) ;
  57. } else if ( spressed ) {
  58. player1.step ( true , false ) ;
  59. } else {
  60. player1.step ( false , false ) ;
  61. }
  62. if ( uppressed ) {
  63. player2.step ( true , true ) ;
  64. } else if ( downpressed ) {
  65. player2.step ( true , false ) ;
  66. } else {
  67. player2.step ( false , false ) ;
  68. }
  69. ball.step ( ) ;
  70. if ( ! ball.checkAndCollideWithHorizWall ( ) ) {
  71. if ( ball.x > width / 2 ) {
  72. ball.checkAndCollideWithPaddle ( player2 ) ;
  73. } else {
  74. ball.checkAndCollideWithPaddle ( player1 ) ;
  75. }
  76. } else {
  77. ball.initialize ( ) ;
  78. }
  79. }
  80. repaint ( ) ;
  81. }
  82.  
  83. public void paint ( Graphics g ) {
  84. Graphics2D g2 = ( Graphics2D ) g ;
  85. g2.fill ( player1 ) ;
  86. g2.fill ( player2 ) ;
  87. g2.fill ( ball ) ;
  88. }
  89. class keylistener implements KeyListener {
  90. public void keyReleased ( KeyEvent e ) {
  91. if ( e.getKeyCode ( ) == KeyEvent.VK_W ) {
  92. PlayArea.this.wpressed = false ;
  93. } else if ( e.getKeyCode ( ) == KeyEvent.VK_S ) {
  94. PlayArea.this.spressed = false ;
  95. }
  96. if ( e.getKeyCode ( ) == KeyEvent.VK_UP ) {
  97. PlayArea.this.uppressed = false ;
  98. } else {
  99. if ( e.getKeyCode ( ) == KeyEvent.VK_DOWN ) {
  100. PlayArea.this.downpressed = false ;
  101. }
  102. }
  103. System.out.println ( e.getKeyText ( e.getKeyCode ( ) ) + " released" ) ;
  104. }
  105.  
  106. public void keyPressed ( KeyEvent e ) {
  107. System.out.println ( "dsssd" ) ;
  108. if ( e.getKeyCode ( ) == KeyEvent.VK_W ) {
  109. PlayArea.this.wpressed = true ;
  110. }
  111. if ( e.getKeyCode ( ) == KeyEvent.VK_S ) {
  112. PlayArea.this.spressed = true ;
  113. }
  114. if ( e.getKeyCode ( ) == KeyEvent.VK_UP ) {
  115. PlayArea.this.uppressed = true ;
  116. }
  117. if ( e.getKeyCode ( ) == KeyEvent.VK_DOWN ) {
  118. PlayArea.this.downpressed = true ;
  119. }
  120. System.out.println ( e.getKeyText ( e.getKeyCode ( ) ) + " pressed" ) ;
  121. }
  122.  
  123. public void keyTyped ( KeyEvent e ) {
  124. System.out.println ( "DDDD" ) ;
  125. }
  126. }
  127. }
Ball.java
java Syntax (Toggle Plain Text)
  1. import java.awt.* ;
  2. import java.awt.event.* ;
  3. import java.awt.geom.* ;
  4. import javax.swing.* ;
  5. import javax.swing.event.* ;
  6.  
  7. /**
  8.  * Write a description of class Ball here.
  9.  * @author (your name)
  10.  * @version (a version number or a date)
  11.  */
  12. public class Ball extends Ellipse2D.Double {
  13. /**X part of position*/
  14. public int x ;
  15. /**Y part of position*/
  16. public int y ;
  17. /**This Variabl is for the total speed*/
  18. public int speed ;
  19. /**X part of velocity*/
  20. public int xVel ;
  21. /**Y part of velocity*/
  22. public int yVel ;
  23. /**Diameter of the ball*/
  24. public int diameter = 10 ;
  25. /**This holds a copy of the PlayArea for wall comparisons*/
  26. private PlayArea parent ;
  27.  
  28. /**make a new Ball*/
  29. public Ball ( PlayArea parent1 ) {
  30. parent = parent1 ;
  31. initialize ( ) ;
  32. }
  33.  
  34. /**set initial values to relevant variables*/
  35. public void initialize ( ) {
  36. x = parent.width / 2 - diameter / 2 ;
  37. y = parent.height / 2 - diameter / 2 ;
  38. randomDirection ( ) ;
  39. }
  40.  
  41. /**Find a random direction that is not completely verticle*/
  42. public void randomDirection ( ) {
  43. double angle = Math.PI / 2 ;
  44. while ( angle == Math.PI / 2 || angle == 3 * Math.PI / 2 ) {
  45. angle = 2 * Math.PI * Math.random ( ) ;
  46. }
  47. speed = ( int ) Math.ceil ( 5 + 10 * Math.random ( ) ) ;
  48. xVel = ( int ) ( speed * Math.cos ( angle ) ) ;
  49. yVel = ( int ) ( speed * Math.sin ( angle ) ) ;
  50. }
  51.  
  52. /**Checks for collisions with the walls of the play area*/
  53. public void checkAndCollideWithVertWall ( ) {
  54. if ( y <= 0 || y + diameter >= parent.height ) {
  55. yVel = - yVel ;
  56. }
  57. }
  58.  
  59. /**Checks for collisions with the walls of the play area*/
  60. public boolean checkAndCollideWithHorizWall ( ) {
  61. if ( x <= 0 || x + diameter >= parent.width ) return true ;
  62. return false ;
  63. }
  64.  
  65. /**check for collision with a paddle*/
  66. public void checkAndCollideWithPaddle ( Paddle p ) {
  67. if ( p.intersects ( new Rectangle2D.Double ( x , y , diameter , diameter ) ) ) {
  68. xVel = - xVel ;
  69. }
  70. }
  71.  
  72. /**update the rectangle class so that we draw in the right place*/
  73. public void updateSuper ( ) {
  74. super.x = x ;
  75. super.y = y ;
  76. super.width = diameter ;
  77. super.height = diameter ;
  78. }
  79.  
  80. /**step the ball one frame*/
  81. public void step ( ) {
  82. checkAndCollideWithVertWall ( ) ;
  83. x += xVel ;
  84. y += yVel ;
  85. updateSuper ( ) ;
  86. }
  87. }
Paddle.java
java Syntax (Toggle Plain Text)
  1. import java.awt.* ;
  2. import java.awt.event.* ;
  3. import java.awt.geom.* ;
  4. import javax.swing.* ;
  5. import javax.swing.event.* ;
  6.  
  7. /**
  8.  * Write a description of class Paddle here.
  9.  * @author William Peckham
  10.  * @version 1.0A
  11.  */
  12. public class Paddle extends Rectangle2D.Double {
  13. /**This is the x position and shouldn't change*/
  14. public int x ;
  15. /**This is the y position which is changed by the user*/
  16. public int y ;
  17. /**This is the speed at which the paddle should move
  18.   * when the user presses a button.
  19.   */
  20. public int speed = 10 ;
  21. /**This is the width*/
  22. public int width = 10 ;
  23. /**This is the height*/
  24. public int height = 20 ;
  25. /**This is a variable to hold the play area for wall checks*/
  26. private PlayArea parent ;
  27.  
  28. /**make a new Paddle*/
  29. public Paddle ( PlayArea parent1 , int nX ) {
  30. parent = parent ;
  31. x = nX ;
  32. }
  33.  
  34. public void initialize ( ) {
  35. y = parent.height / 2 - height / 2 ;
  36. updateSuper ( ) ;
  37. }
  38.  
  39. public void updateSuper ( ) {
  40. super.x = x ;
  41. super.y = y ;
  42. super.width = width ;
  43. super.height = height ;
  44. }
  45.  
  46. public void step ( boolean any , boolean up ) {
  47. if ( any ) {
  48. System.out.println ( "move" ) ;
  49. if ( up ) {
  50. System.out.println ( "up processing" ) ;
  51. if ( y - speed >= 0 ) {
  52. System.out.println ( "enough room" ) ;
  53. y -= speed ;
  54. } else {
  55. System.out.println ( "not enough room" ) ;
  56. y = 0 ;
  57. }
  58. } else {
  59. System.out.println ( "down processing" ) ;
  60. if ( y + speed + height <= parent.height ) {
  61. System.out.println ( "enough room" ) ;
  62. y += speed ;
  63. } else {
  64. System.out.println ( "not enough room" ) ;
  65. y = parent.height - height ;
  66. }
  67. }
  68. }
  69. System.out.println ( "updating" ) ;
  70. updateSuper ( ) ;
  71. }
  72. }
this is actually supposed to accompany a final paper for my writing class, which i have to hand in on friday 12/19 so, it would be appreciated if i could get some help by then, but i still got a week, so no real rush...

thanks for the help
Similar Threads
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 14th, 2008
0

Re: Problem with Pong

Difficult to wade through this code, but general recommendations to start finding the problems:
- if you're subclassing things like Ellipse2D, don't spuriously have extra variables like 'x', 'y' that duplicate functionality of the underlying class -- you'll just tie yourself in knots keeping them up to date. When you need to set the position of the object, just set the position of the underlying class, full stop.
- I'd recommend that you don't use ScheduledThreadPoolExecutor; use the java.swingx.Timer class, which makes sure that the code is executed in the GUI thread;
- if your animation is stopping, that may be an exception occurring that isn't being logged out -- put a try/catch around your PlayArea's run() method (by the way, I'd recommend calling it something other than run() since it isn't actually a Runnable) and print out any exception.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 14th, 2008
0

Re: Problem with Pong

how would i go about using javax.swing.timer? i've never been told there was a problem with ScheduledThreadPoolExecutor nor have i had one is there something i should be aware of? What exceptions would be thrown that would stop the animation that wouldn't be logged?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 14th, 2008
1

Re: Problem with Pong

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
how would i go about using javax.swing.timer? i've never been told there was a problem with ScheduledThreadPoolExecutor nor have i had one is there something i should be aware of?
There's nothing wrong with it per se -- it's a very powerful component -- but it's not designed for what you're trying to do. It is designed for cases where you want to run code in some arbitrary thread from a pool of threads. What you want to do is execute code in the GUI thread, because you are updating GUI components.

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
What exceptions would be thrown that would stop the animation that wouldn't be logged?
Any RuntimeException -- NullPointerException, NumberFormatException, ArrayIndexOutOfBoundsException etc etc. Why not just add the logging and find out?

You seem to not believe me, but it's clearly stated in the documentation to ScheduledThreadPoolExecutor.scheduleAtFixedRate() that if an exception does occur, future executions will be cancelled, so I really would check that this isn't what's happening!!
Last edited by neilcoffey; Dec 14th, 2008 at 9:42 pm. Reason: typo
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 14th, 2008
0

Re: Problem with Pong

it's not that i don't believe you, i'm just a bit cynical... and i didn't have time to try it, so i figured i would ask. i figured that asking wouldn't hurt, sorry if i offended you in some way...

still doesn't explain why it won't originally place the Paddle in the correct location though any thoughts on that?

oh, by the way, about the variable names, i can change them, i just like to have a variable in the class i'm working with so i don't have to remember that i'm using the superclass. i find it helps when trying to do cirtain things, like use the center of a circle as where it gets positioned rather than the top left corner. is it possible that there are conflicts with the super's variables and the subclasses?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 15th, 2008
0

Re: Problem with Pong

Oh the positioning -- I think that's just because you're not actually calling the initialize() method, isn't it?
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 15th, 2008
0

Re: Problem with Pong

crap, i can't believe i didn't catch that one... thanks, i have a bit of time now, i'm going to try{}catch{} that now
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 15th, 2008
0

Re: Problem with Pong

wow... i figured it out when i actually called initialize it did log an exception, a NullPointerException, i realized that i was setting parent=parent rather than parent1... that fixed everything thank you for you help
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: convert pixel to rgb values
Next Thread in Java Forum Timeline: Urgent Binary Search Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC