MrThoe

BallArrays

Sep 28th, 2020 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. /** Array of Balls
  2. /** Array of Balls
  3. * This will use arrays to handle
  4. * numberous balls
  5. */
  6.  
  7. /* GLOBAL VARIABLES HERE */
  8.  
  9. //POSITION VARIABLES
  10. var x = []; //[] means This is an array
  11. var y = [];
  12. //VELOCITY VARIABLES
  13. var Vx = [];
  14. var Vy = [];
  15. //Colors
  16. var r = [];
  17. var g = [];
  18. var b = [];
  19. //OTHER VARIABLES
  20. var rad; //radius
  21. var numBalls;
  22.  
  23. function setup() {
  24. createCanvas(400, 400);
  25. rad = 20;
  26. numBalls = 50;
  27. //Populate the array
  28. for(var i = 0; i < numBalls; i++){
  29. x[i] = random(width); //i = index
  30. y[i] = random(height);
  31. r[i] = random(255);
  32. g[i] = random(255);
  33. b[i] = random(255);
  34. Vx[i] = random(-5,5);
  35. Vy[i] = random(-5,5);
  36. }
  37. }
  38. function draw() {
  39. background(120);
  40. //Draw the array of balls
  41. for(var i = 0; i < numBalls; i++){
  42. x[i] += Vx[i];
  43. y[i] += Vy[i];
  44. fill(r[i], g[i], b[i], 200);
  45. ellipse(x[i], y[i], rad, rad);
  46. if(x[i] > width || x[i] < 0){
  47. Vx[i] *= -1; //REVERSE THE Vx
  48. }
  49. if(y[i] > height || y[i] < 0){
  50. Vy[i] *= -1;
  51. }
  52.  
  53.  
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment