xxxxxxxxxx
74
class Ball {
constructor(x, y, speed, letter, col) {
this.x = x;
this.y = y;
this.letter = letter;
this.speed = speed;
this.alive = true;
this.color = col;
}
show() {
fill(this.color, 100, 100, 0.3);
stroke("cyan");
noStroke();
circle(this.x, this.y, 100);
fill("white");
textSize(30);
text(String.fromCharCode(this.letter), this.x, this.y);
}
move() {
this.y += this.speed;
if (this.y > height + 50) {
this.y = -random(50, height);
this.letter = Math.floor(random(65, 91));
this.alive = true;
}
}
}
let balls = [];
let count = 0;
function createBall() {
let ball = new Ball(
random(50, width - 50),
-random(50, height / 2),
random(1, 5),
Math.floor(random(65, 91)),
random(360)
);
balls.push(ball);
}
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
textAlign(CENTER, CENTER);
colorMode(HSB);
for (let i = 0; i < 10; i++) {
createBall();
}
}
function draw() {
background("black");
for (let ball of balls) {
if (ball.alive) {
if (keyIsDown(ball.letter)) {
count++;
ball.alive = false;
strokeWeight(5);
stroke("red");
line(ball.x, ball.y, width / 2, height - 50);
}
ball.show();
}
ball.move();
}
fill("red");
triangle(width / 2, height - 50, width / 2 - 20, height, width / 2 + 20, height);
text("Hits : " + count, width - 100, 50);
}