xxxxxxxxxx
let ball1, ball2, ball3, ball4, ball5;
let bounce = true;
let speedX = 5;
let speedY = 5;
let counter = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
ball1 = new Ball();
ball2 = new Ball();
ball3 = new Ball();
ball4 = new Ball();
ball5 = new Ball();
}
function draw() {
background(100);
//move();
//display();
ball1.move();
ball1.display();
ball1.bounce();
ball2.move();
ball2.display();
ball2.bounce();
ball3.move();
ball3.display();
ball3.bounce();
ball4.move();
ball4.display();
ball4.bounce();
ball5.move();
ball5.display();
ball5.bounce();
textSize(50);
fill(300);
if (counter >= 5) {
text("GAME OVER", 100, 100);
}
}
class Ball {
constructor() {
this.x = random(width);
this.y = random(height);
this.speedX = random(-10, 10);
this.speedY = random(-10, 10);
this.alive = true;
}
move() {
this.x = this.x + this.speedX;
this.y = this.y + this.speedY;
}
display() {
if (this.alive) {
ellipse(this.x, this.y, 40, 40);
}
}
bounce() {
if (this.x > width - 20 || this.x < 0 + 20) {
this.speedX = this.speedX * -1;
}
if (this.y > height - 20 || this.y < 0 + 20) {
this.speedY = this.speedY * -1;
}
}
pop() {
let d = dist(mouseX, mouseY, this.x, this.y);
if (d < 40) {
this.alive = false;
counter++;
}
}
}
function mousePressed() {
ball1.pop();
ball2.pop();
ball3.pop();
ball4.pop();
ball5.pop();
}