Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- W = 800;
- H = 600;
- fps = 60;
- newFrame = (new Date).getTime()
- let enemies = [];
- enemyLimit = 15;
- function setup() {
- frameRate(fps);
- createCanvas(W, H);
- windowResized();
- angleMode(DEGREES);
- startGame();
- }
- function draw() {
- strokeWeight(0);
- background(bg);
- lastFrame = newFrame
- newFrame = (new Date).getTime()
- if (alive) {
- translate(W / 2 - player.x, H / 2 - player.y);
- c = 0;
- for (e of enemies) {
- e.update();
- e.display();
- distance = (((e.x - player.x) ** 2) + ((e.y - player.y) ** 2)) ** 0.5
- if (distance >= (((W * 2) ** 2) + ((H * 2) ** 2)) ** 0.5) {
- enemies.splice(c, 1);
- enemies.push(new enemy);
- }
- c++;
- }
- player.trailCounter = 0;
- for (t of player.trail) {
- t.display();
- if (t.ttl <= 0) {
- player.trail.splice(this.trailCounter, 1);
- }
- player.trailCounter++;
- }
- for (b of player.bullets) {
- b.update();
- b.display();
- }
- translate(-(W / 2 - player.x), -(H / 2 - player.y))
- player.update();
- player.display();
- fill(255)
- fps = round(1000 / (newFrame - lastFrame))
- text(fps, 10, 20)
- text(((player.xSpeed ** 2) + (player.ySpeed ** 2)) ** 0.5, 10, 40)
- if (player.health <= 0) {
- alive = false;
- enemies = [];
- score = player.score;
- player = null;
- }
- } else {
- textSize(72);
- textAlign(CENTER);
- fill(200, 200, 0);
- text("Score: ".concat(score), W / 2, H / 4);
- fill(255)
- textSize(16)
- }
- }
- function startGame() {
- bg = [48, 48, 48];
- player = new character
- c = 0;
- while (c < enemyLimit) {
- enemies.push(new enemy);
- c++;
- }
- score = 0;
- alive = true;
- textSize(16)
- }
- function windowResized() {
- resizeCanvas(windowWidth, windowHeight);
- W = windowWidth, H = windowHeight;
- }
- class particle {
- constructor() {
- this.x = player.x + round(random(-10, 10)), this.y = player.y + round(random(-10, 10));
- this.radius = round(random(2, 20));
- this.Initialttl = fps;
- this.ttl = this.Initialttl;
- this.color = round(random(48, 200));
- this.xSpeed = -0.01 * player.xSpeed + round(random(-30, 30) / fps);
- this.ySpeed = -0.01 * player.ySpeed + round(random(-30, 30) / fps);
- }
- display() {
- this.x += this.xSpeed;
- this.y += this.ySpeed;
- fill(this.color, 10, 10);
- ellipse(this.x, this.y, 10, 10)
- ellipse(this.x, this.y, this.radius * (this.tll / this.Initialttl), this.radius * (this.tll / this.Initialttl));
- fill(255, 255, 255);
- this.ttl -= 1;
- }
- }
- function mouseReleased() {
- if (alive) {
- player.shooting = true;
- }
- }
- function mousePressed() {
- if (alive) {
- player.shooting = false;
- }
- }
- class character {
- constructor() {
- this.x = 0;
- this.y = 0;
- this.health = 100;
- this.score = 0;
- this.xSpeed = 0;
- this.ySpeed = 0;
- this.speed = 500;
- this.drag = 0.9;
- this.orientation = 45;
- this.width = 40, this.height = 40;
- this.trail = [];
- this.bullets = [];
- this.bulletSpeed = 2000;
- this.fireRate = 30;
- this.damage = 20;
- this.shotCounter = 0;
- }
- update() {
- this.shotCounter += 1;
- if(this.shotCounter >= (fps/this.fireRate) && this.shooting){
- this.shoot();
- }
- if (keyIsDown(87)) {
- this.xSpeed += sin(this.orientation) * this.speed / fps
- this.ySpeed += sin(this.orientation - 90) * this.speed / fps
- this.trail.push(new particle)
- }
- if (keyIsDown(83)) {
- this.xSpeed -= sin(this.orientation) * this.speed / fps
- this.ySpeed -= sin(this.orientation - 90) * this.speed / fps
- this.trail.push(new particle)
- }
- this.x += this.xSpeed / fps;
- this.y += this.ySpeed / fps;
- this.xSpeed = this.xSpeed * (1 - (this.drag / fps));
- this.ySpeed = this.ySpeed * (1 - (this.drag / fps));
- this.orientation = aimToMouse(mouseX, mouseY, W / 2, H / 2);
- }
- shoot() {
- this.bullets.push(new bullet(this.x, this.y, (sin(this.orientation) * this.bulletSpeed) + this.xSpeed, (sin(this.orientation - 90) * this.bulletSpeed) + this.ySpeed), this.damage)
- }
- display() {
- fill(48, 255, 48);
- translate(W / 2, H / 2)
- rotate(this.orientation)
- triangle(-this.width / 2, this.height / 2, 0, -this.height / 2, this.width / 2, this.height / 2);
- rotate(-this.orientation)
- translate(-W / 2, -H / 2)
- }
- }
- class bullet {
- constructor(x, y, xSpeed, ySpeed, damage) {
- this.x = x;
- this.y = y;
- this.xSpeed = xSpeed;
- this.ySpeed = ySpeed;
- this.damage = damage;
- this.radius = 8;
- }
- update() {
- this.x += this.xSpeed / fps;
- this.y += this.ySpeed / fps;
- this.c = 0
- for (this.e of enemies) {
- if (((((this.x - this.e.x) ** 2) + ((this.y - this.e.y) ** 2)) ** 0.5) < (this.radius + this.e.radius) * 0.75) {
- this.e.health -= this.damage;
- player.bullets.splice(player.bullets.indexOf(this), 1);
- }
- this.c++;
- }
- }
- display() {
- ellipse(this.x, this.y, this.radius, this.radius);
- }
- }
- class enemy {
- constructor() {
- this.x = round(random(-W * 2, -W)) * ((round(random(0, 1)) * 2) - 1) + player.x
- this.y = round(random(-H * 2, -H)) * ((round(random(0, 1)) * 2) - 1) + player.y
- this.radius = round(random(50, 250));
- this.xSpeed = 0;
- this.ySpeed = 0;
- this.health = 100;
- this.speed = round(random(400, 500));
- this.drag = 0.9;
- this.playerDist = W;
- }
- update() {
- if(this.health <= 0){
- enemies.splice(enemies.indexOf(this), 1);
- }
- this.playerDist = ((this.x - player.x) ** 2 + (this.y - player.y) ** 2) ** 0.5
- if (this.radius >= this.playerDist) {
- bg = [128, 48, 48]
- player.health = 0;
- }
- if (this.x < player.x) {
- this.xSpeed += this.speed / fps;
- } else {
- this.xSpeed -= this.speed / fps;
- }
- if (this.y < player.y) {
- this.ySpeed += this.speed / fps;
- } else {
- this.ySpeed -= this.speed / fps;
- }
- this.x += this.xSpeed / fps;
- this.y += this.ySpeed / fps;
- this.xSpeed = this.xSpeed * (1 - (this.drag / fps));
- this.ySpeed = this.ySpeed * (1 - (this.drag / fps));
- }
- display() {
- ellipse(this.x, this.y, this.radius, this.radius)
- }
- }
- function aimToMouse(x1, y1, x2, y2) {
- xDif = x1 - x2
- yDif = y1 - y2
- angle = 0
- if (xDif > 0 && yDif < 0) {
- angle = atan(xDif / -yDif)
- }
- if (xDif >= 0 && yDif >= 0) {
- angle = atan(yDif / xDif) + 90
- }
- if (xDif <= 0 && yDif >= 0) {
- angle = atan(-xDif / yDif) + 180
- }
- if (xDif <= 0 && yDif <= 0) {
- angle = atan(yDif / xDif) + 270
- }
- if (xDif == 0 && yDif <= 0) {
- angle = 0
- }
- return angle;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement