xxxxxxxxxx
52
let tx, ty, bullets;
function setup() {
createCanvas(400, 400);
bullets = [];
}
function draw() {
background(220);
for (let i = 0; i < bullets.length; i++) {
bullets[i].move();
bullets[i].display();
}
cleanMess();
}
function cleanMess() {
for (let i = bullets.length - 1; i >= 0; i--) {
if (dist(bullets[i].x, bullets[i].y, bullets[i].tx, bullets[i].ty)<5) bullets.splice(i,1);
}
}
function mouseClicked() {
bullets[bullets.length] = new Bullet(mouseX, mouseY);
}
class Bullet {
constructor(tx, ty) {
this.x = 10;
this.y = height - 10;
this.tx = tx;
this.ty = ty;
this.speed = 0.1;
}
move() {
this.x += this.speed * (this.tx - this.x);
this.y += this.speed * (this.ty - this.y);
}
display() {
push();
translate(this.x, this.y);
noStroke();
fill(0);
ellipse(0, 0, 10,10);
fill(220);
ellipse(-1, -1, 7,7);
fill(255);
ellipse(-2, -2, 3,3);
pop();
}
}