Guest User

Untitled

a guest
Feb 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. //button to star movement of orange circle
  2. boolean button = false;
  3. //variable ellipse values
  4. int circleX = 0;
  5. int circleY = 100;
  6.  
  7. //color variables
  8. float c1 = 0;
  9. float c2 = 255;
  10. //varibles too incrementally change the color of the window
  11. float c1Change = 1;
  12. float c2Change = - 1;
  13.  
  14. float x = 100; //x location of square
  15. float y = 0; //y location of square
  16.  
  17. float speed = 0; // speed of square
  18. float gravity = 0.1; // the acceleration of the ball based on its location
  19.  
  20. void setup() {
  21. size(200,200);
  22.  
  23. }
  24.  
  25. void draw() {
  26. //draw rectangle on left
  27. fill(c2, 0, c1);
  28. rect(0, 0, 100, 200);
  29.  
  30. //draw rectangle on right
  31. fill(c2, 0, c1);
  32. rect(100, 0, 100, 200);
  33.  
  34. //adjust color values
  35. c1 = c1 + c1Change;//c1 is changing at an increment of 1
  36. c2 = c2 + c2Change;// is changeing by a negetive amount of 1
  37. // so that once the color gets to blue in reverse back to red and then recycles
  38. if (c1 < 0 || c1 > 255) {
  39. c1Change *= -1;
  40. }
  41.  
  42. if (c2 < 0 || c2 > 255) {
  43. c2Change *= -1;
  44. }
  45. fill(255, 150, 0);
  46. ellipse(circleX, circleY, 50, 50);
  47.  
  48. // draw the ball
  49. fill(255,150,0);
  50. noStroke();
  51. ellipse(x, y, 10, 10);
  52.  
  53. y = y + speed;
  54. speed = speed + gravity; // add speed to location add gravity to speed, becuase then the speed wont get to fast and is relative to location
  55.  
  56. //bounce back up!
  57. if (y > height) {
  58. speed = speed * -0.95; // -0.95 becuase it doesnt reverse the speed it "dampens" it or slows it down incrementally
  59.  
  60. y = height; // this is neceressary so that when draw loops this is at the end making sure the ball comes back down to "the ground" instead of leaving the window.
  61. }
  62. if (button) {
  63. circleX++;
  64. }
  65. if (circleX > width) {
  66. circleX = 0;
  67. }
  68.  
  69. }
  70. void mousePressed() {
  71. button = true;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. // some question: How to make a random X variable so that the ball bounces in all directions?
Add Comment
Please, Sign In to add comment