Legebatterie

p5js--bounceBalls

Mar 26th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. var amount=10;
  2. var growfactor=1.01;
  3.  
  4. var person= [];
  5.  
  6. class Bubble{
  7.  
  8. constructor() {
  9. this.x=random(width);
  10. this.y=random(height);
  11. this.vx=random(-10,10);
  12. this.vy=random(-10,10);
  13. }
  14. move(){
  15. this.x=this.x+this.vx;
  16. this.y=this.y+this.vy;
  17. }
  18. checkbounce(){
  19. if(this.x>width || this.x<0){
  20. this.vx=this.vx*-1
  21. this.vx=this.vx*growfactor;
  22. }
  23. if(this.y>width || this.y<0){
  24. this.vy=this.vy*-1
  25. this.vy=this.vy*growfactor;
  26. }
  27. }
  28. show(){
  29. stroke(255)
  30. noFill()
  31. ellipse(this.x,this.y,10,10)
  32. }
  33. }
  34.  
  35. function setup() {
  36. createCanvas(400, 400);
  37. for(var i=0;i<amount;i++){
  38. person[i]= new Bubble();
  39. }
  40.  
  41. }
  42.  
  43. function draw() {
  44. background(0);
  45. for(var i=0;i<amount;i++){
  46. person[i].move();
  47. person[i].checkbounce();
  48. person[i].show();
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment