MrThoe

FirstClass

Oct 2nd, 2020 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** For learning Classes and Arrays
  2.   * AP CS Principles  10/2/2020
  3.   * @Teacher:  Mr. Thoe
  4.   */
  5.  
  6. var b = [];
  7. //var numBalls;
  8.  
  9. function setup() {
  10.   createCanvas(400, 400);
  11.   //numBalls = 50;
  12.   for(var i = 0; i < 50; i++){
  13.     b[i] = new Ball();
  14.   }
  15. }
  16.  
  17. function draw() {
  18.   background(120);
  19.   for(var i=0; i < b.length; i++){
  20.     b[i].show();
  21.   }
  22. }
  23.  
  24. class Ball{
  25.   constructor(){
  26.     this.x = random(width);
  27.     this.y = random(height);
  28.     this.Vx = random(-5,5);
  29.     this.Vy = random(-5,5);
  30.     this.grey = random(255);
  31.   }
  32.  
  33.   show(){
  34.     this.move();
  35.     fill(this.grey);
  36.     circle(this.x, this.y, 20);
  37.   }
  38.  
  39.   move(){
  40.     this.x += this.Vx;
  41.     this.y += this.Vy;
  42.     if(this.x < 0 || this.x > width){
  43.       this.Vx *= -1;
  44.     }
  45.     if(this.y < 0 || this.y > height){
  46.       this.Vy *= -1;
  47.     }
  48.   }
  49. }
Add Comment
Please, Sign In to add comment