Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. PVector location;
  2. PVector velocity;
  3. float a = 0;
  4. float b = 0;
  5. float c = 0;
  6. int size = 16;
  7. boolean down = true;
  8. boolean gravity = true;
  9. boolean symmetrical = false;
  10.  
  11. void setup() {
  12. size(600,600);
  13. smooth();
  14. background(255);
  15. location = new PVector(350,300);
  16. velocity = new PVector(2.5,0);
  17. }
  18.  
  19. void draw() {
  20. noStroke();
  21. fill(255,10);
  22. rect(0,0,width,height);
  23.  
  24. location.add(velocity);//moves the ball
  25. if(gravity)//if gravity is true or "on"...
  26. {
  27. if(down == true)//...and down is true...
  28. {
  29. velocity.add(0,.2);//...gravity pulls ball down
  30. }
  31. if(down == false)//...and down is false
  32. {
  33. velocity.add(0,-.2);//...gravity pulls ball up
  34. }
  35. }
  36. if (((location.y > height) || (location.y < 0)) && (down == true)) {
  37. velocity.y = (velocity.y - .4) * -1;//makes ball lose some momentum when it bounces off of the floor or ceiling
  38. a = random(0,255);//sets a b and c variables to random floats between 0 and 255
  39. b = random(0,255);
  40. c = random(0,255);
  41. }
  42. if(((location.y > height) || (location.y < 0)) && (down == false))
  43. {
  44. velocity.y = (velocity.y + .4) * -1;//makes ball lose some momentum when it bounces off of the floor or ceiling
  45. a = random(0,255);//sets a b and c variables to random floats between 0 and 255
  46. b = random(0,255);
  47. c = random(0,255);
  48. }
  49.  
  50. if(symmetrical == false)//if symmetrical is false...
  51. {
  52. if ((location.x > width) || (location.x < 0)) {//...then the ball can move across the entire width of the rectangle
  53. velocity.x = velocity.x * -1;//makes ball bounce off of walls
  54. a = random(0,255);
  55. b = random(0,255);
  56. c = random(0,255);
  57. }
  58. stroke(0);
  59. fill(a,b,c);
  60. ellipse(location.x,location.y,size,size);//shows the path of the object
  61. }
  62.  
  63. if(symmetrical == true)//if symmetrical is true...
  64. {
  65. if ((location.x < (width/2 + size/2)) || (location.x > width)) {//...then makes ball bounce off of the middle line
  66. velocity.x = velocity.x * -1;
  67. a = random(0,255);
  68. b = random(0,255);
  69. c = random(0,255);
  70. }
  71. stroke(0);
  72. fill(a,b,c); // makes ball the color of random floats
  73. ellipse(location.x,location.y,size,size);//shows the path of the object
  74. scale(-1,1);
  75. ellipse(location.x-600,location.y,size,size);//shows a second object (that isn't there) on the other side of the line of symmetry, because i wanted to do it with only one PVector
  76. }
  77. }
  78.  
  79. void keyPressed()
  80. {
  81. if(key == 's' || key == 'S')//s for size
  82. {
  83. size = (int)random(2,30);//randomly changes the size of the ball
  84. }
  85. if(key == 'd' || key == 'D')//d for down
  86. {
  87. down = !down;//reverses gravity(goes up or down)
  88. }
  89. if(key == 'a' || key == 'A')//a for acceleration (sometimes, the ball goes out of bounds)
  90. {
  91. velocity.y = 0;//stops the ball from falling momentarily
  92. }
  93. if(key == 'g'|| key == 'G')//g for gravity
  94. {
  95. gravity = !gravity;//turns off gravity
  96. }
  97. }
  98. void mouseClicked()
  99. {
  100. setup();
  101. symmetrical = !symmetrical;//makes the symmetrical version run
  102. }
Add Comment
Please, Sign In to add comment