Guest User

Untitled

a guest
Jan 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /* ***************************
  2. Challenge 3: Motion
  3. Designed By: Andy Grove
  4. Creates a ball that starts in the middle of the canvas and
  5. bounces off the edges of the canvas
  6.  
  7. What needs to be fixed to get this to work?
  8. What changes could be made to make this code more professional?
  9. ****************************** */
  10. int backgroundColor =255;
  11. float BALLX=width/2;
  12. float BALLY=height/2;
  13. float ballXVelocity = random(1,10);
  14. float ballYVelocity = random(1,10);
  15. final int ball_color = 200;
  16. float ball_size=80.0;
  17.  
  18. void setup() {
  19. size(1280, 800);
  20. }
  21.  
  22. void draw() {
  23. background(backgroundColor);
  24. fill(ball_color);
  25. ellipse( BALLX, BALLY, ball_size, ball_size);
  26. //use velocity to simulate motion
  27. BALLX = BALLX + ballXVelocity;
  28. BALLY = BALLY+ ballYVelocity;
  29. //reverse direction when the ball hits the left/right edges of the canvas
  30. if (BALLX >= width - 40 || BALLX <= 0 + 40){
  31. ballXVelocity = ballXVelocity * (1);
  32. ballYVelocity = ballYVelocity * (-1);
  33. }
  34. //reverse direction when the ball hits the top/bottom edges of the canvas
  35. if (BALLY >= height - 40 || BALLY <= 0 + 40){
  36. ballXVelocity = ballXVelocity * (-1);
  37. ballYVelocity = ballYVelocity * (1);
  38. }
  39. }
Add Comment
Please, Sign In to add comment