Guest User

Untitled

a guest
Jan 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. /* ***************************
  2. Challenge 3: Motion
  3. Designed By:
  4. Creates a ball that bounces off the edges of the canvas
  5. ****************************** */
  6. final int BACKGROUND_COLOR =255;
  7.  
  8. float ballX;
  9. float ballY;
  10. float ballXVelocity = random(1,10);
  11. float ballYVelocity = random(1,10);
  12. final int BALL_COLOR = 200;
  13. final float BALL_SIZE=80.0;
  14.  
  15. void setup() {
  16. size(1280, 800);
  17. ballX = width/2;
  18. ballY=height/2;
  19. }
  20.  
  21. void draw() {
  22. background(BACKGROUND_COLOR);
  23. fill(BALL_COLOR);
  24. ellipse( ballX, ballY, BALL_SIZE, BALL_SIZE);
  25. //use velocity to simulate motion
  26. ballX+=ballXVelocity;
  27. ballY+=ballYVelocity;
  28. //reverse direction when the ball hits the left/right edges of the canvas
  29. if (ballX >= width || ballX <= 0){
  30. ballXVelocity *= -1;
  31. }
  32. //reverse direction when the ball hits the top/bottom edges of the canvas
  33. if (ballY >= height || ballY <= 0){
  34. ballYVelocity *= -1;
  35. }
  36. }
Add Comment
Please, Sign In to add comment