xxxxxxxxxx
//Classes and Objects
//Object Oriented Programming
//Object: way of organizing:
//Variables -> Properties
//Functions -> Methods
//Classes
//Blueprint for creating multiple objects
var bubbles_list = [];
var burstCount = 0;
var timer;
function setup() {
createCanvas(400, 400);
for(var i = 0; i<100; i++){
bubbles_list.push(new Bubbles());
}
print(bubbles_list);
timer = setTimeout(check_if_w_or_l, 15000);//calls function after three seconds
}
function draw() {
background(220);
for(var i = 0; i < 100; i++){
bubbles_list[i].display();
//bubbles_list[i].move();
bubbles_list[i].burst();
}
}
function check_if_w_or_l(){
if(burstCount == 5){
alert("you win");
}else{
alert("you lose")
}
}
class Bubbles{ //capital letters only come into play with classes, classes do not need parentheses
//variables
//size
//position
//color
constructor(){
this.x = random(400);
this.y = random(400);
this.s = random(50, 100); //size
this.c = color(random(200), random(200), random(200)); //chose 200 instead of 255 so it doesn't clash with the white background
this.speed = random(-1, 1);
}
//functions
//display
display(){
fill(this.c);
circle(this.x, this.y, this.s);
}
//burst
burst(){
//mouseX and mouseY
//dist()
if(dist(mouseX,mouseY,this.x, this.y
)< this.s/2){//uses distance formula to see how far the mouse is from the circles, if the distacnce is less than the radius (diameter/2), then the bubble will burst
if(mouseIsPressed){
this.x = -1000000;
burstCount++;
print(burstCount);
}
}
}
//move(){
// this.x += this.speed;
// if(this.x > 400){
// }
//}
}