xxxxxxxxxx
290
const FRICT = 0.85;
const WIDTH = 800;
const HEIGHT = 600;
let bullets = [];
let enemy;
let score;
class Bullet {
constructor(x, y, sdx, sdy, tx, ty) {
this.x = x;
this.y = y;
this.dx = cos(atan2(ty - this.y, tx - this.x)) * 100 + sdx;
this.dy = sin(atan2(ty - this.y, tx - this.x)) * 100 + sdy;
this.life = 3;
this.deathSpeed = 5;
}
update() {
this.dx *= FRICT;
this.dy *= FRICT;
this.x += this.dx;
this.y += this.dy;
//wall collision
if (this.x >= WIDTH) {
this.dx = -this.dx;
this.x = WIDTH - 1;
}
else if (this.x < 0) {
this.dx = -this.dx;
this.x = 1;
}
if (this.y >= HEIGHT) {
this.dy = -this.dy;
this.y = HEIGHT - 1;
}
else if (this.y < 0) {
this.dy = -this.dy;
this.y = 1;
}
if (abs(this.dx) < this.deathSpeed && abs(this.dy) < this.deathSpeed) {
this.life--;
}
else {
if (this.life < 3) {
this.life++;
}
}
}
draw() {
strokeWeight(this.life);
stroke("white");
point(this.x, this.y);
}
}
class Enemy {
constructor(x, y, hp) {
this.x = x;
this.y = y;
this.r = 30;
this.hp = hp;
}
update(p, bullets) {
this.x += cos(atan2(p.y - this.y, p.x - this.x)) * (score + 1);
this.y += sin(atan2(p.y - this.y, p.x - this.x)) * (score + 1);
//bullet collision
for (let i = 0; i < bullets.length; i++) {
if (dist(this.x, this.y, bullets[i].x, bullets[i].y) < this.r) {
bullets.splice(i, 1);
i--;
this.hp--;
if (i < 0) {
break;
}
}
}
//wall collision
if (this.x + this.r >= WIDTH) {
this.x = WIDTH - this.r - 1;
}
else if (this.x - this.r < 0) {
this.x = this.r + 1;
}
if (this.y + this.r >= HEIGHT) {
this.y = HEIGHT - this.r - 1;
}
else if (this.y - this.r < 0) {
this.y = this.r + 1;
}
}
draw() {
strokeWeight(3);
stroke("green");
circle(this.x, this.y, this.r * 2);
}
}
let p = {
x: 64,
y: 64,
dx: 0,
dy: 0,
acc: 1.3,
r: 20,
shootCoolDown: 0,
dashCoolDown: 0,
update: function() {
this.x += this.dx;
this.y += this.dy;
this.dx *= FRICT;
this.dy *= FRICT;
//movement
if (keyIsDown(65)) {
this.dx -= this.acc;
}
else if (keyIsDown(68)) {
this.dx += this.acc;
}
if (keyIsDown(87)) {
this.dy -= this.acc;
}
else if (keyIsDown(83)) {
this.dy += this.acc;
}
//wall collision
if (this.x + this.r >= WIDTH) {
this.dx = -this.dx / 1.5;
this.x = WIDTH - this.r - 1;
}
else if (this.x - this.r < 0) {
this.dx = -this.dx / 1.5;
this.x = this.r + 1;
}
if (this.y + this.r >= HEIGHT) {
this.dy = -this.dy / 1.5;
this.y = HEIGHT - this.r - 1;
}
else if (this.y - this.r < 0) {
this.dy = -this.dy / 1.5;
this.y = this.r + 1;
}
//shooting
if (mouseIsPressed) {
if (mouseButton == LEFT) {
if (this.shootCoolDown <= 0) {
this.shootCoolDown = 20;
bullets.push(new Bullet(this.x, this.y, this.dx, this.dy, mouseX, mouseY));
}
this.shootCoolDown--;
if (this.shootCoolDown < 0) {
this.shootCoolDown = 0;
}
}
if (mouseButton == RIGHT && this.dashCoolDown <= 0) {
this.dashCoolDown = 100;
this.dx = cos(atan2(mouseY - this.y, mouseX - this.x)) * 40;
this.dy = sin(atan2(mouseY - this.y, mouseX - this.x)) * 40;
}
}
else {
this.shootCoolDown = 0;
}
this.dashCoolDown--;
if (this.dashCoolDown < 0) {
this.dashCoolDown = 0;
}
},
draw: function() {
strokeWeight(3);
stroke("red");
circle(this.x, this.y, this.r * 2);
},
reset: function() {
this.x = 64;
this.y = 64;
this.dx = 0;
this.dy = 0;
this.dashCoolDown = 0;
}
}; //p is short for player
function setup() {
createCanvas(800, 600);
for (let element of document.getElementsByClassName("p5Canvas")) {
element.addEventListener("contextmenu", (e) => e.preventDefault());
}
frameRate(1000);
noFill();
enemy = new Enemy(random(0, WIDTH), random(0, HEIGHT), 3);
score = 0;
p.reset();
}
function draw() {
background("black");
for (let i = 0; i < bullets.length; i++) {
if (bullets[i].life == 0) {
bullets.splice(i, 1);
i--;
if (i < 0) {
break;
}
}
bullets[i].update();
bullets[i].draw();
}
fill("white");
noStroke();
text("enemy hp: " + enemy.hp, 15, 15);
text("score: " + score, 15, 30);
fill("blue");
rect(14, 100, 100 - p.dashCoolDown, 15);
noFill();
if (enemy.hp <= 0) {
enemy = new Enemy(random(0, WIDTH), random(0, HEIGHT), score + 3);
score++;
}
if (dist(p.x, p.y, enemy.x, enemy.y) < p.r + enemy.r) {
setup();
}
p.update();
p.draw();
enemy.update(p, bullets);
enemy.draw();
}