xxxxxxxxxx
53
let skull2;
let ballLst = [];
let colorLst;
function setup() {
createCanvas(400, 400);
//initialize all variables
skull2 = new Skull (60,60, 1);
colorLst = [random(255), random(255), random(255)];
for (let i = 0; i < 5; i++){
ballLst.push(new Ball(random(400), random(400), random(-3,3), random(-3,3)));
}
}
function draw() {
background(colorLst[0], colorLst[1], colorLst[2]);
//draw skull
skull2.show();
//if mouse is pressed, draw laser
if (mouseIsPressed){
skull2.laser(mouseX, mouseY);
}
//update skull
skull2.update();
for (let i = 0; i < ballLst.length; i++){
//update ball position
ballLst[i].move();
ballLst[i].bounce();
ballLst[i].show();
//if mouse if pressed on the ball position, delete the ball, add a new ball and chane the background color
if (mouseIsPressed && dist(mouseX, mouseY, ballLst[i].x, ballLst[i].y) < ballLst[i].radius){
console.log("boom");
//change background color
colorLst[0] = random(255);
colorLst[1] = random(255);
colorLst[2] = random(255);
//delete a ball
ballLst.splice(i, 1);
//add new ball
ballLst.push(new Ball(random(400), random(400), random(-3,3), random(-3,3)));
}
}
}