Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** For learning Classes and Arrays
- * AP CS Principles 10/2/2020
- * @Teacher: Mr. Thoe
- */
- var b = [];
- //var numBalls;
- function setup() {
- createCanvas(400, 400);
- //numBalls = 50;
- for(var i = 0; i < 50; i++){
- b[i] = new Ball();
- }
- }
- function draw() {
- background(120);
- for(var i=0; i < b.length; i++){
- b[i].show();
- }
- }
- class Ball{
- constructor(){
- this.x = random(width);
- this.y = random(height);
- this.Vx = random(-5,5);
- this.Vy = random(-5,5);
- this.grey = random(255);
- }
- show(){
- this.move();
- fill(this.grey);
- circle(this.x, this.y, 20);
- }
- move(){
- this.x += this.Vx;
- this.y += this.Vy;
- if(this.x < 0 || this.x > width){
- this.Vx *= -1;
- }
- if(this.y < 0 || this.y > height){
- this.Vy *= -1;
- }
- }
- }
Add Comment
Please, Sign In to add comment