xxxxxxxxxx
216
// Global variables
let player;
let bullets = [];
let viruses = [];
let score = 0;
function setup() {
createCanvas(600, 400);
player = new Player(width / 2, height / 2);
// Spawn initial viruses
for (let i = 0; i < 5; i++) {
spawnVirus();
}
}
function draw() {
background(0);
// Update and display player
player.update();
player.display();
// Update and display bullets
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
bullets[i].display();
// Check bullet collision with viruses
for (let j = viruses.length - 1; j >= 0; j--) {
if (bullets[i].hits(viruses[j])) {
bullets[i].remove();
viruses[j].remove();
score++;
break;
}
}
}
// Remove bullets and viruses
bullets = bullets.filter(bullet => !bullet.isOffscreen());
viruses = viruses.filter(virus => !virus.isOffscreen());
// Spawn new viruses
if (random(1) < 0.01) {
spawnVirus();
}
// Display score
fill(255);
textSize(20);
text('Score: ' + score, 10, 30);
// Check player collision with viruses
for (let i = viruses.length - 1; i >= 0; i--) {
if (player.hits(viruses[i])) {
gameOver();
return;
}
}
}
function keyPressed() {
if (keyCode === UP_ARROW) {
player.move(0, -1);
} else if (keyCode === DOWN_ARROW) {
player.move(0, 1);
} else if (keyCode === LEFT_ARROW) {
player.move(-1, 0);
} else if (keyCode === RIGHT_ARROW) {
player.move(1, 0);
} else if (key === ' ') {
bullets.push(new Bullet(player.x, player.y));
}
}
function spawnVirus() {
let side = floor(random(4));
let x, y;
if (side === 0) {
x = random(width);
y = -20;
} else if (side === 1) {
x = width + 20;
y = random(height);
} else if (side === 2) {
x = random(width);
y = height + 20;
} else {
x = -20;
y = random(height);
}
viruses.push(new Virus(x, y));
}
function gameOver() {
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text('Game Over', width / 2, height / 2);
noLoop();
}
// Player class
class Player {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 3;
this.size = 20;
}
update() {
this.x = constrain(this.x, 0, width - this.size);
this.y = constrain(this.y, 0, height - this.size);
}
display() {
fill(0, 255, 0);
rect(this.x, this.y, this.size, this.size);
}
move(xDir, yDir) {
this.x += xDir * this.speed;
this.y += yDir * this.speed;
}
hits(virus) {
return (
this.x < virus.x + virus.size &&
this.x + this.size > virus.x &&
this.y < virus.y + virus.size &&
this.y + this.size > virus.y
);
}
}
// Bullet class
class Bullet {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 5;
this.size = 5;
}
update() {
this.y -= this.speed;
}
display() {
fill(255);
ellipse(this.x, this.y, this.size);
}
isOffscreen() {
return this.y < 0;
}
hits(virus) {
return (
this.x > virus.x &&
this.x < virus.x + virus.size &&
this.y > virus.y &&
this.y < virus.y + virus.size
);
}
remove() {
this.x = -100;
this.y = -100;
}
}
// Virus class
class Virus {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 2;
this.size = 30;
}
update() {
let dx = player.x - this.x;
let dy = player.y - this.y;
let angle = atan2(dy, dx);
this.x += cos(angle) * this.speed;
this.y += sin(angle) * this.speed;
}
display() {
fill(255, 0, 0);
rect(this.x, this.y, this.size, this.size);
}
isOffscreen() {
return (
this.x < -this.size ||
this.x > width ||
this.y < -this.size ||
this.y > height
);
}
remove() {
this.x = -100;
this.y = -100;
}
}