xxxxxxxxxx
109
var bubbles=[];
var count=75
var score=0
var button
function setup() {
createCanvas(500, 450);
for (i = 0; i < count; i++) {
bubbles[i] = new Bubbles(random(0,width),random(0,height),random(-1,1),random(-1,1),random(20,40));
}
}
function draw() {
background(0)
for (i = 0; i < bubbles.length; i++) {
bubbles[i].fly()
bubbles[i].show()
bubbles[i].bounce()
bubbles[i].pop()
fill(255)
strokeWeight(0.5)
textSize(40)
text(score,200,50)
}
gameEnd()
}
class Bubbles { // class names shoud have capital letters
constructor(x,y,Mx,My,rad) {
this.rad = rad // all this is the data for my bubble
this.x = x
this.y = y
this.Mx=Mx
this.My=My
}
fly() {
this.x = this.x + this.Mx
this.y = this.y + this.My
}
show() {
noFill()
strokeWeight(2)
stroke(random(255),random(255),random(255))
circle(this.x, this.y, this.rad)
}
bounce() {
if (this.x>=width) {
this.Mx=random(-1,-1.5)
}
if(this.y>=height) {
this.My=random(-1,-1.5)
}
if (this.x<=0) {
this.Mx=random(1,1.5)
}
if (this.y<=0) {
this.My=random(1,1.5)
}
}
pop(){
if (mouseIsPressed) {
var d = dist(mouseX, mouseY, this.x,this.y)
if (d < this.rad/2) {
this.x=100000
this.y=100000
console.log('pop')
score=score+1
}
}
}
}
function gameEnd(){
if (score>=count) {
noStroke()
fill(0)
textSize(25)
fill(255)
text('GAME OVER',150,200)
button=createButton('PLAY AGAIN?')
button.position(175,300)
button.mousePressed(refresh)
button.size(100)
}
}
function refresh(){
location.reload()
}