xxxxxxxxxx
// object-oriented programming (OOP)
// is programming using OBJECTS
// objects are pieces of computer code
// that contain both functions *and* data
//
// objects in javascript are accessed through
// things called "classes"
// classes have methods which are functions
// classes have properties which are variables
// classes also have a constructor function which runs
// when you make one. this is called instantiation.
var bricks = []; // array to hold all the bricks
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
for(let i = 0;i<200;i++)
{
bricks[i] = new Brick(random(width), random(height), 80, 20);
}
}
function draw() {
for(let i = 0;i<bricks.length;i++)
{
bricks[i].draw();
}
}
// this is going to be a class definition:
class Brick {
// constructor: runs when you instantiate the class ("new" on a object)
constructor(x=width/2,y=height/2,w=10,h=10)
{
// set some properties:
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = random(128,255);
this.g = random(128,255);
this.b = random(128,255);
}
// add some methods:
draw()
{
fill(this.r,this.g,this.b);
rect(this.x,this.y,this.w,this.h);
}
}