Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PongAnimatedBall {
- public static void main(String[] args) {
- int WINNER = -1; // number of wins
- int UPPER_PDL_xLIM = 16; // the boundary the paddle is limited to at the right of the canvas
- int UPPER_PDL_yLIM = 9; // the boundary the paddle is limited to at the top
- int LOWER_PDL_xLIM = 0; // the boundary the paddle is limited to at the left
- int LOWER_PDL_yLIM = 0; // the boundary the paddle is limited to at the bottom
- double PDL_VEL = 0.5; // how fast the paddle will move
- double ball_xvel = 0.0032 * (Math.random() < 0.5 ? -1 : 1); //how fast the ball will move between left and right
- double ball_yvel = 0.0018 * (Math.random() < 0.5 ? -1 : 1); //how fast the ball will move between top and bottom
- StdDraw.enableDoubleBuffering();
- StdDraw.setXscale(-1.0, 1.0); //canvas set up
- StdDraw.setYscale(-1.0, 1.0);
- StdDraw.setCanvasSize(600, 600);
- double bpos_x = 300; // ball position
- double bpos_y = 300;
- double radius = .05;
- StdDraw.setPenRadius(radius);
- double pdl_w = 10.0; //paddles
- double pdl_h = 50.0;
- double pdl1_x = 75.0;
- double pdl2_x = 525.0;
- double pdl1_y = 300.0;
- double pdl2_y = 300.0;
- while(WINNER == -1){
- if (Math.abs(bpos_x + ball_xvel) > 1.0 - radius){ ball_xvel = -ball_xvel; }
- if (Math.abs(bpos_y + ball_yvel) > 1.0 - radius){ ball_yvel = -ball_yvel; }
- bpos_x = bpos_x + ball_xvel;
- bpos_y = bpos_y + ball_yvel;
- StdDraw.clear();
- StdDraw.filledCircle(bpos_x, bpos_y, radius);
- StdDraw.filledRectangle(pdl1_x, pdl1_y, pdl_w, pdl_h);
- StdDraw.filledRectangle(pdl2_x, pdl2_y, pdl_w, pdl_h);
- StdDraw.show();
- StdDraw.pause(20);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement