Advertisement
Guest User

Untitled

a guest
Oct 10th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public class PongAnimatedBall {
  2. public static void main(String[] args) {
  3. int WINNER = -1; // number of wins
  4. int UPPER_PDL_xLIM = 16; // the boundary the paddle is limited to at the right of the canvas
  5. int UPPER_PDL_yLIM = 9; // the boundary the paddle is limited to at the top
  6. int LOWER_PDL_xLIM = 0; // the boundary the paddle is limited to at the left
  7. int LOWER_PDL_yLIM = 0; // the boundary the paddle is limited to at the bottom
  8. double PDL_VEL = 0.5; // how fast the paddle will move
  9. double ball_xvel = 0.0032 * (Math.random() < 0.5 ? -1 : 1); //how fast the ball will move between left and right
  10. double ball_yvel = 0.0018 * (Math.random() < 0.5 ? -1 : 1); //how fast the ball will move between top and bottom
  11. StdDraw.enableDoubleBuffering();
  12.  
  13. StdDraw.setXscale(-1.0, 1.0); //canvas set up
  14. StdDraw.setYscale(-1.0, 1.0);
  15. StdDraw.setCanvasSize(600, 600);
  16.  
  17. double bpos_x = 300; // ball position
  18. double bpos_y = 300;
  19. double radius = .05;
  20. StdDraw.setPenRadius(radius);
  21.  
  22.  
  23. double pdl_w = 10.0; //paddles
  24. double pdl_h = 50.0;
  25. double pdl1_x = 75.0;
  26. double pdl2_x = 525.0;
  27. double pdl1_y = 300.0;
  28. double pdl2_y = 300.0;
  29.  
  30. while(WINNER == -1){
  31.  
  32. if (Math.abs(bpos_x + ball_xvel) > 1.0 - radius){ ball_xvel = -ball_xvel; }
  33. if (Math.abs(bpos_y + ball_yvel) > 1.0 - radius){ ball_yvel = -ball_yvel; }
  34.  
  35. bpos_x = bpos_x + ball_xvel;
  36. bpos_y = bpos_y + ball_yvel;
  37.  
  38. StdDraw.clear();
  39.  
  40. StdDraw.filledCircle(bpos_x, bpos_y, radius);
  41. StdDraw.filledRectangle(pdl1_x, pdl1_y, pdl_w, pdl_h);
  42. StdDraw.filledRectangle(pdl2_x, pdl2_y, pdl_w, pdl_h);
  43.  
  44. StdDraw.show();
  45. StdDraw.pause(20);
  46.  
  47. }
  48.  
  49. }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement