Advertisement
Guest User

Untitled

a guest
Feb 12th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. final int DIAM = 200;
  2. final int BALL_DIAMETER = 15;
  3.  
  4. int centerX, centerY;
  5.  
  6.  
  7. int ballX, ballY;
  8. int ballSpeedX = 3, ballSpeedY = 2;
  9.  
  10. void setup() {
  11. size( 500, 500 );
  12. ballX = 20;
  13. ballY = 30;
  14.  
  15. centerX = width/2;
  16. centerY = height/2;
  17.  
  18. }
  19.  
  20. void draw() {
  21. background( 128 );
  22. fill(#ff5954);
  23. ellipse(centerX,centerY,DIAM,DIAM);
  24.  
  25. fill(255);
  26.  
  27. ellipse( ballX, ballY, BALL_DIAMETER, BALL_DIAMETER );
  28. moveBall();
  29. }
  30.  
  31.  
  32.  
  33. void moveBall() {
  34. changeSpeed();
  35. ballX = ballX + ballSpeedX;
  36. ballY = ballY + ballSpeedY;
  37. }
  38.  
  39.  
  40. void changeSpeed() {
  41. if ( ballX < BALL_DIAMETER/2 || ballX > width-BALL_DIAMETER/2 ) {
  42.  
  43. ballSpeedX = -ballSpeedX;
  44. }
  45. if ( ballY < BALL_DIAMETER/2 || ballY > height-BALL_DIAMETER/2 ) {
  46.  
  47. ballSpeedY = -ballSpeedY;
  48. }
  49.  
  50. if(dist(ballX,ballY,centerX,centerY) < (DIAM/2) + (BALL_DIAMETER/2)){
  51.  
  52. ballSpeedX = -ballSpeedX;
  53. ballSpeedY = -ballSpeedY;
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement