xxxxxxxxxx
376
let scene = 0;
let player = {
x: 200,
y: 200,
r: 20,
angle: 0,
dead: false,
fillColor: "red",
show: function () {
noStroke();
push();
translate(this.x, this.y);
rotate(PI / 2);
rotate(this.angle);
fill(this.fillColor);
triangle(0, -this.r / 2, -this.r / 2, this.r / 2, this.r / 2, this.r / 2);
pop();
},
moveTo: function (x, y, speed) {
this.angle = atan2(y - this.y, x - this.x);
// move player
if (dist(this.x, this.y, x, y) > 5) {
this.x += speed * cos(this.angle);
this.y += speed * sin(this.angle);
}
},
checkHit: function () {
for (let obstacle of obstacles) {
if (
dist(this.x, this.y, obstacle.x, obstacle.y) <
obstacle.r + player.r / 2
) {
return true;
}
}
return false;
},
shoot: function () {
let bullet = {
x: this.x,
y: this.y,
speed: 4,
fillColor: color(255),
angle: this.angle,
show: function () {
stroke(this.fillColor);
strokeWeight(3);
line(
this.x,
this.y,
2 * this.speed * cos(this.angle) + this.x,
2 * this.speed * sin(this.angle) + this.y
);
strokeWeight(1);
stroke(0);
},
update: function () {
this.x += this.speed * cos(this.angle);
this.y += this.speed * sin(this.angle);
this.checkHit();
if (this.x < 0 || this.x > width || this.y < 0 || this.y > height) {
bullets.splice(bullet, 1);
}
},
checkHit: function () {
for (let i = obstacles.length - 1; i > -1; i--) {
if (
obstacles[i].pointInside(
2 * this.speed * cos(this.angle) + this.x,
2 * this.speed * sin(this.angle) + this.y
)
) {
makeParticles(15, this.x, this.y, 15, 30);
obstacles[i].health -= 20;
bullets.splice(this, 1);
}
}
},
};
bullets.push(bullet);
},
bomb: function () {
let bomb = {
x: this.x,
y: this.y,
r: 15,
lifespan: 30,
age: 0,
speed: 4,
fillColor: color(0),
angle: this.angle,
show: function () {
fill(this.fillColor);
circle(this.x, this.y, this.r);
strokeWeight(1);
stroke(0);
},
update: function () {
this.x += this.speed * cos(this.angle);
this.y += this.speed * sin(this.angle);
this.checkHit();
if (this.x < 0 || this.x > width || this.y < 0 || this.y > height) {
bombs.splice(bomb, 1);
}
this.age++;
},
checkHit: function () {
for (let i = obstacles.length - 1; i > -1; i--) {
if (
dist(this.x, this.y, obstacles[i].x, obstacles[i].y) <
obstacles[i].r + this.r
) {
this.explode();
obstacles[i].health -= 50;
}
}
if (this.age >= this.lifespan) {
this.explode();
}
},
explode: function () {
makeParticles(25, this.x, this.y, 30, 40);
for (let i = obstacles.length - 1; i > -1; i--) {
if (
dist(obstacles[i].x, obstacles[i].y, this.x, this.y) <
2 * (obstacles[i].r + this.r)
) {
obstacles[i].health -= 30;
}
}
bombs.splice(this, 1);
},
};
bombs.push(bomb);
},
checkEdges: function () {
if (this.x < this.r / 2) {
this.x = this.r / 2;
} else if (this.x > width - this.r / 2) {
this.x = width - this.r / 2;
}
if (this.y < this.r / 2) {
this.y = this.r / 2;
} else if (this.y > height - this.r / 2) {
this.y = height - this.r / 2;
}
},
};
let obstacles = [];
let bullets = [];
let bombs = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 20; i++) {
createObstacle(random(width), random(height / 6), random(10, 20));
}
}
function draw() {
background(51);
if (scene == 0) {
startScreen();
} else if (scene == 1) {
playGame();
} else if (scene == 2) {
loseScreen();
} else if (scene == 3) {
winScreen();
}
}
function startScreen() {
background(0);
textAlign(CENTER);
fill(255);
textSize(30);
text("ASTEROIDS", width / 2, height / 2);
if (
mouseX > width / 4 &&
mouseX < (3 * width) / 4 &&
mouseY > (3 * height) / 5 &&
mouseY < (4 * height) / 5
) {
fill(60, 215, 60);
} else {
fill(100, 255, 100);
}
rect(width / 4, (3 * height) / 5, width / 2, height / 5, width / 30);
fill(0);
textAlign(CENTER, CENTER);
text("Play", width / 2, (3 * height) / 5 + height / 10);
if (
mouseX > width / 4 &&
mouseX < (3 * width) / 2 &&
mouseY > (3 * height) / 5 &&
mouseY < (4 * height) / 5 &&
mouseIsPressed
) {
scene = 1;
}
}
function playGame() {
if (player.dead) {
scene = 2;
}
for (let i = obstacles.length - 1; i > -1; i--) {
if (obstacles[i].dead) {
obstacles.splice(i, 1);
if (obstacles.length <= 0) {
scene = 3;
}
}
}
updateAndDrawParticles();
if (obstacles.length > 0) {
for (let i = obstacles.length - 1; i > -1; i--) {
obstacles[i].show();
obstacles[i].update();
if (obstacles[i]) {
obstacles[i].checkEdges();
}
}
}
if (bullets.length > 0) {
for (let i = bullets.length - 1; i > -1; i--) {
bullets[i].show();
bullets[i].update();
}
}
if (bombs.length > 0) {
for (let i = bombs.length - 1; i > -1; i--) {
bombs[i].show();
bombs[i].update();
}
}
player.checkEdges();
player.show();
player.moveTo(mouseX, mouseY, 2);
if (player.checkHit()) {
player.fillColor = color(255, 255, 100);
player.dead = true;
} else {
player.fillColor = color(255, 0, 0);
}
if (shooting) {
shotCounter++;
if (shotCounter % 10 == 0) {
player.shoot();
}
}
}
function loseScreen() {
fill(150, 0, 0);
textAlign(CENTER);
textSize(30);
background(255, 0, 0);
text("Game Over.", 200, 200);
}
function winScreen() {
fill(0, 150, 0);
textAlign(CENTER);
textSize(30);
background(0, 255, 0);
text("You Win!", 200, 200);
}
function keyPressed() {
if (key == " ") {
player.shoot();
shooting = true;
} else if (key == "w") {
player.bomb();
}
}
function keyReleased() {
if (key == " ") {
shooting = false;
}
}
let shooting = false;
let shotCounter = 0;
function mousePressed() {
player.shoot();
shooting = true;
}
function mouseReleased() {
shooting = false;
}
function createObstacle(x, y, r) {
let obstacle = {
x: x,
y: y,
r: r,
fillColor: similarColorTo(255, 219, 145, 20),
velX: random(-3, 3),
velY: random(-3, 3),
health: (r / 20) * 100,
dead: false,
show: function () {
noStroke();
fill(this.fillColor);
circle(this.x, this.y, this.r * 2);
},
update: function () {
// this.velX += random(-0.3, 0.3);
// this.velY += random(-0.3, 0.3);
this.velX = constrain(this.velX, -3, 3);
this.velY = constrain(this.velY, -3, 3);
this.x += this.velX;
this.y += this.velY;
if (this.health <= 0) {
this.dead = true;
}
},
checkEdges: function () {
if (this.x < this.r) {
this.x = this.r;
this.velX *= -1;
} else if (this.x > width - this.r) {
this.x = width - this.r;
this.velX *= -1;
}
if (this.y < this.r) {
this.y = this.r;
this.velY *= -1;
} else if (this.y > height - this.r) {
this.y = height - this.r;
this.velY *= -1;
}
},
pointInside(x, y) {
return dist(x, y, this.x, this.y) < this.r;
},
};
obstacles.push(obstacle);
}
function similarColorTo(r0, g0, b0, jitter) {
r = r0 + random(-jitter, jitter);
g = g0 + random(-jitter, jitter);
b = b0 + random(-jitter, jitter);
return color(r, g, b);
}