xxxxxxxxxx
36
//sepparate file for organization
// Define the class Ball. This is essentially a blueprint.
class Ball {
constructor(x, y, w) { //constructors are passed in when applying this class
this.x = x; // x location of ball
this.y = y; // y location of ball
this.w = w; // size of ball
this.speed = 0; // speed
this.fill = 0;
}
// automatically is a function as it is defined in a class
display() {
// Display the ellipse
noStroke();
ellipse(this.x, this.y, this.w, this.w);
}
update() {
// Add speed to location
this.y = this.y + this.speed;
// Add gravity to speed
this.speed = this.speed + gravity;
// If square reaches the bottom
// Reverse speed by multiplying by a negative #
if (this.y > height) {
this.speed = this.speed * -0.95;
}
}
}