Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>2D Shooter</title>
- </head>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <body>
- <canvas id="Canvy"></canvas>
- <script>
- var canvas = document.getElementById("Canvy");
- var b = canvas.getContext("2d");
- b.canvas.width = window.innerWidth;
- b.canvas.height = window.innerHeight;
- document.body.appendChild(canvas);
- var inform = false;
- var click = false;
- var offsetX;
- var offsetY;
- var scalewidth = window.innerWidth / 1920;
- var scaleheight = window.innerHeight / 969;
- var paused = false;
- var mouseDown = 0;
- var timeDilation = 1;
- var maxFreeze = 100;
- var freeze = 100;
- var freezeTime = false;
- var cantFreeze = false;
- let mouse = {
- x: canvas.width / 2,
- y: canvas.height / 2
- }
- var keyState = {};
- var keyTime = {};
- window.addEventListener('keydown',function(e){
- keyState[e.keyCode || e.which] = true;
- keyTime[e.keyCode || e.which] = new Date();
- },true);
- setInterval(function() {
- var now = new Date();
- for(key in keyTime) {
- if(now - keyTime[key] >= 0500 && document.hasFocus() == false) {
- // run the release code for this key
- keyState[key] = false;
- }
- }
- }, 1000);
- window.addEventListener('keyup',function(e){
- keyState[e.keyCode || e.which] = false;
- },true);
- window.addEventListener("mousemove", function(e){
- mouse.x = event.clientX;
- mouse.y = event.clientY;
- })
- document.body.onmousedown = function() {
- mouseDown = 1;
- }
- document.body.onmouseup = function() {
- mouseDown = 0;
- }
- window.addEventListener("keydown", function(e){
- if(e.keyCode == 67) {
- var newcode = prompt("Enter Code here");
- eval(newcode);
- }
- });
- addEventListener('resize', () => {
- canvas.width = innerWidth;
- canvas.height = innerHeight;
- init();
- })
- function bullet(Ix, Iy, width, height, color, speed, origin, power, dx, dy) {
- bullets.push(this);
- this.Ix = Ix;
- this.Iy = Iy;
- this.width = width;
- this.height = height;
- this.x = this.Ix;
- this.y = this.Iy;
- this.origin = origin;
- this.power = power;
- if(dx == null && dy == null) {
- if(this.origin == "player") {
- this.dx = (mouse.x - this.Ix - offsetX) / 100;
- this.dy = (mouse.y - this.Iy - offsetY) / 100;
- } else {
- //Determine which player is closest
- var dist = [];
- for(var i = 0; i < players.length; i++) {
- //Find distance for each and push it into array
- dist.push(Math.hypot(this.x + 0.5*this.width - players[i].x + 0.5*players[i].width, this.y + 0.5*this.height - players[i].y + 0.5*players[i].height))
- }
- //Find value of minimum distance
- Array.min = function(array) {
- return Math.min.apply(Math, array);
- }
- var mindist = Array.min(dist);
- //find Index value of minimum distance and use it to find that player
- var a = dist.indexOf(mindist);
- //console.log("Array is: " + dist);
- //console.log("Min Distance is " + mindist);
- //console.log("Index is " + a);
- this.dx = (players[a].x - this.Ix) / 100;
- this.dy = (players[a].y - this.Iy) / 100;
- }
- //this.dx = (mouse.x - 0.5*canvas.width) / 100;
- //this.dy = (mouse.y - 0.5*canvas.height) / 100;
- var length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
- this.dx /= length;
- this.dy /= length;
- this.dx = this.dx * speed;
- this.dy = this.dy * speed;
- } else {
- this.dx = dx;
- this.dy = dy;
- }
- this.draw = function() {
- if(this.x < canvas.width - offsetX && this.x > -this.width - offsetX && this.y < canvas.height - offsetY && this.y > -this.height - offsetY) {
- //this.x += this.dx;
- //this.y += this.dy;
- for(var i = 0; i < players.length; i++) {
- if(this.origin != "player" && players[i].x > this.x - players[i].width && players[i].x < this.x + this.width && players[i].y > this.y - players[i].height && players[i].y < this.y + this.height) {
- players[i].health -= this.power;
- if(players[i].health <= 0) {
- bullets = [];
- players[i].x = 800;
- players[i].y = 400;
- players[i].health = players[i].healthI;
- }
- var item = this;
- bullets = bullets.filter(bullets => bullets !== item);
- }
- }
- b.fillStyle = color;
- b.fillRect(this.x, this.y, width, height);
- } else {
- bullets = bullets.filter(bullets => bullets !== this);
- }
- /*
- if(posX > this.x - Bsize && posX < this.x + this.width && posY > this.y - Bsize && posY < this.y + this.height) {
- }
- */
- }
- this.move = function() {
- this.x += this.dx * timeDilation;
- this.y += this.dy * timeDilation;
- }
- }
- let bullets = [];
- function gun(speed, reload, iammo, width, height, color) {
- }
- function player(Ix, Iy, width, height, color, speed) {
- players.push(this);
- this.x = Ix;
- this.y = Iy;
- this.width = width;
- this.height = height;
- this.speed = speed;
- this.upI = 38;
- this.downI = 40;
- this.leftI = 37;
- this.rightI = 39;
- this.slot1 = "empty";
- this.slot2 = "empty";
- this.slot3 = "empty";
- this.reloading;
- this.health = 100;
- this.healthI = 100;
- this.draw = function() {
- b.fillStyle = color;
- b.fillRect(this.x, this.y, this.width, this.height);
- }
- this.healthbar = function(x,y,width,height,name) {
- this.name = name;
- var tsize = canvas.width / 50;
- b.textAlign = "left";
- b.font = tsize + "px Arial";
- b.fillStyle = "White";
- b.fillText(name + ": ", x, y + 0.04*canvas.height);
- b.fillStyle = "SlateGray";
- b.fillRect(x + 0.1*canvas.width, y, width, height);
- b.fillStyle = color;
- b.fillRect(x + 0.1*canvas.width, y, width * (this.health / this.healthI), height);
- }
- this.remap = function(up, left, down, right) {
- this.upI = up;
- this.leftI = left;
- this.downI = down;
- this.rightI = right;
- }
- this.fire = function(speed, firerate, widthf, heightf, power, color) {
- /*
- if(slot1 == "empty" && slot2 == "empty" && slot3 == "empty") {
- } else if(slot1 == "empty" && slot2 != "empty") {
- slot1 = slot2;
- slot2 = "empty";
- } else if(slot1 == "empty" && slot2 != "empty") {
- slot1 = slot2;
- slot2 = "empty";
- }
- */
- addEventListener("click", () => {
- if(this.reloading != true && document.hasFocus()) {
- click = true;
- }
- });
- if(mouseDown > 0) {
- click = true;
- }
- if(this.reloading != true && click == true) {
- //var ndog = new bullet(0.5*canvas.width + 0.5*this.width - 0.5*width, 0.5*canvas.height + 0.5*this.height - 0.5*height, width, height, "SteelBlue", 10);
- //bullet that moves with player
- var ndog = new bullet(this.x + 0.5*this.width - 0.25*width, this.y + 0.5*this.height - 0.25*height, widthf, heightf, color, speed, "player", power);
- click = false;
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, firerate*1000);
- }
- for(var i = 0; i < bullets.length; i++) {
- if(bullets[i].origin == "player") {
- bullets[i].draw();
- }
- }
- if(this.reloading == true) {
- }
- }
- }
- let players = [];
- function enemy(x, y, width, height, color, dx, dy) {
- enemies.push(this);
- this.x = x;
- this.y = y;
- this.dx = dx;
- this.dy = dy;
- this.width = width;
- this.height = height;
- this.onplat = false;
- this.Ix = x;
- this.Iy = y;
- this.Idx = dx;
- this.Idy = dy;
- this.health = 100;
- this.healthI = this.health;
- this.reloading;
- this.startup = true;
- this.startuptime = 0;
- this.overlaping;
- this.Q = 2;
- this.count = 0;
- //b.fillStyle = color;
- //b.fillRect(this.x, this.y, width, height);
- this.draw = function(color2) {
- if(this.health > 0) {
- if(color2 == null) {
- b.fillStyle = color;
- b.fillRect(this.x, this.y, width, height);
- } else {
- var grd = b.createRadialGradient(this.x + 0.5*this.width, this.y + 0.5*this.height, 0.1*this.width, this.x + 0.6*this.width, this.y + 0.6*this.height, 0.7*this.width);
- grd.addColorStop(0, color2);
- grd.addColorStop(1, color);
- // Fill with gradient
- b.fillStyle = grd;
- b.fillRect(this.x, this.y, width, height);
- }
- for(var i = 0; i < bullets.length; i++) {
- if(bullets[i].origin == "player" && bullets[i].x > this.x - bullets[i].width && bullets[i].x < this.x + this.width && bullets[i].y > this.y - bullets[i].height && bullets[i].y < this.y + this.height) {
- this.health -= bullets[i].power;
- bullets[i].y = Math.pow(10,10);
- }
- }
- //Health Bar
- b.fillStyle = "Black";
- b.fillRect(this.x, this.y - 20, this.width, 10);
- b.fillStyle = "Red";
- b.fillRect(this.x, this.y - 20, this.width * (this.health / this.healthI), 10);
- } else {
- this.y = Math.pow(10,10);
- }
- }
- this.cd = function(power) {
- for(var i = 0; i < players.length; i++) {
- if(players[i].x > this.x - players[i].width && players[i].x < this.x + this.width && players[i].y > this.y - players[i].height && players[i].y < this.y + this.height) {
- players[i].health -= power;
- this.overlaping = true;
- if(players[i].health <= 0) {
- bullets = [];
- players[i].x = 800;
- players[i].y = 400;
- players[i].health = players[i].healthI;
- }
- } else {
- if(this.overlaping == true) {
- this.overlaping = false;
- }
- }
- }
- }
- this.move = function(direction, mspeed, minrange, maxrange) {
- //Determine which player is closest
- this.direction = direction;
- this.mspeed = mspeed;
- this.minrange = minrange;
- this.maxrange = maxrange;
- var dist = [];
- for(var i = 0; i < players.length; i++) {
- //Find distance for each and push it into array
- dist.push(Math.hypot(this.x + 0.5*this.width - players[i].x + 0.5*players[i].width, this.y + 0.5*this.height - players[i].y + 0.5*players[i].height))
- }
- //Find value of minimum distance
- Array.min = function(array) {
- return Math.min.apply(Math, array);
- }
- this.mindist = Array.min(dist);
- //find Index value of minimum distance and use it to find that player
- var a = dist.indexOf(this.mindist);
- this.dx = (players[a].x - this.x) / 100;
- this.dy = (players[a].y - this.y) / 100;
- var length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
- this.dx /= length;
- this.dy /= length;
- this.dx = this.dx * mspeed;
- this.dy = this.dy * mspeed;
- if(direction == "towards" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
- this.x += this.dx * timeDilation;
- this.y += this.dy * timeDilation;
- }
- if(direction == "away" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
- this.x -= this.dx * timeDilation;
- this.y -= this.dy * timeDilation;
- }
- /*
- if(direction == "away and zig-zag" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
- this.x -= this.dx * timeDilation;
- this.y -= this.dy * timeDilation;
- if(this.reloading == false) {
- this.count += 1;
- }as
- if(this.count > 1) {
- this.Q = -this.Q;
- this.count = 0;
- }
- //setTimeout(() => { }, 2000);
- }
- */
- }
- this.fire = function(speed, firerate, widthf, heightf, color, dx, dy) {
- this.startT;
- this.nowT;
- if(this.health > 0) {
- if(this.startup == true) {
- setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate )*1000 / timeDilation);
- }
- if(this.reloading != true && this.startup == false) {
- if(paused == false && document.hasFocus()) {
- var ndog = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, speed, "enemy", 30, dx, dy);
- }
- this.reloading = true;
- this.startT = new Date();
- //setTimeout(() => { this.reloading = false; }, (firerate / timeDilation)*1000);
- }
- for(var i = 0; i < bullets.length; i++) {
- if(bullets[i].origin == "enemy") {
- bullets[i].draw();
- }
- }
- this.nowT = new Date();
- if(this.nowT - this.startT >= firerate / timeDilation * 1000) {
- this.reloading = false;
- }
- }
- }
- this.fourfire = function(speed, firerate, widthf, heightf, color) {
- if(this.startup == true) {
- setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
- }
- if(this.reloading != true && this.startup == false) {
- if(paused == false && document.hasFocus()) {
- var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, 0);
- var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, 0);
- var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, 0, speed);
- var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, 0, -speed);
- }
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, firerate*1000);
- }
- for(var i = 0; i < bullet.list.length; i++) {
- bullet.list[i].draw();
- }
- //decal on shooter
- var size = 0.25*this.width;
- var t = new platform(this.x + 0.5*this.width - 0.5*size, this.y, size, size, color);
- var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
- var b = new platform(this.x + 0.5*this.width - 0.5*size, this.y + this.height - size, size, size, color);
- var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
- }
- this.fourDfire = function(speed, firerate, widthf, heightf, color) {
- if(this.startup == true) {
- setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
- }
- if(this.reloading != true && this.startup == false) {
- if(paused == false && document.hasFocus()) {
- var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, speed);
- var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, -speed);
- var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, speed);
- var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, -speed);
- }
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, firerate*1000);
- }
- for(var i = 0; i < bullet.list.length; i++) {
- bullet.list[i].draw();
- }
- //decal on shooter
- var size = 0.25*this.width;
- var tl = new platform(this.x, this.y, size, size, color);
- var tr = new platform(this.x + this.width - size, this.y, size, size, color);
- var bl = new platform(this.x, this.y + this.height - size, size, size, color);
- var br = new platform(this.x + this.width - size, this.y + this.height - size, size, size, color);
- }
- this.eightfire = function(speed, firerate, widthf, heightf, color) {
- if(this.startup == true) {
- setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
- }
- if(this.reloading != true && this.startup == false) {
- if(paused == false && document.hasFocus()) {
- var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, 0);
- var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, 0);
- var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, 0, speed);
- var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, 0, -speed);
- var bull5 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, speed);
- var bull6 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, -speed);
- var bull7 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, speed);
- var bull8 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, -speed);
- }
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, firerate*1000);
- }
- for(var i = 0; i < bullets.length; i++) {
- if(bullets[i].origin == "enemy") {
- bullets[i].draw();
- }
- }
- //decal on shooter
- var size = 0.25*this.width;
- /*
- var t = new platform(this.x + 0.5*this.width - 0.5*size, this.y, size, size, color);
- var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
- var b = new platform(this.x + 0.5*this.width - 0.5*size, this.y + this.height - size, size, size, color);
- var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
- var tl = new platform(this.x, this.y, size, size, color);
- var tr = new platform(this.x + this.width - size, this.y, size, size, color);
- var bl = new platform(this.x, this.y + this.height - size, size, size, color);
- var br = new platform(this.x + this.width - size, this.y + this.height - size, size, size, color);
- */
- }
- this.horzfire = function(speed, firerate, widthf, heightf, color) {
- if(this.startup == true) {
- setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
- }
- if(this.reloading != true && this.startup == false) {
- if(paused == false && document.hasFocus()) {
- var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, 0);
- var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, 0);
- }
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, firerate*1000);
- }
- for(var i = 0; i < bullet.list.length; i++) {
- bullet.list[i].draw();
- }
- //decal on shooter
- var size = 0.25*this.width;
- var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
- var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
- }
- }
- let enemies = [];
- function bar(x,y,width,height,name, color, numer, denom) {
- this.name = name;
- var tsize = canvas.width / 50;
- b.textAlign = "left";
- b.font = tsize + "px Arial";
- b.fillStyle = "White";
- b.fillText(name + ": ", x, y + 0.04*canvas.height);
- b.fillStyle = "SlateGray";
- b.fillRect(x + 0.1*canvas.width, y, width, height);
- b.fillStyle = color;
- b.fillRect(x + 0.1*canvas.width, y, width * (numer / denom), height);
- }
- function gameLoop() {
- for(var i = 0; i < players.length; i++) {
- if(keyState[players[i].upI]) {
- players[i].y = players[i].y - players[i].speed;
- }
- if(keyState[players[i].downI]) {
- players[i].y = players[i].y + players[i].speed;
- }
- if(keyState[players[i].leftI]) {
- players[i].x = players[i].x - players[i].speed;
- }
- if(keyState[players[i].rightI]) {
- players[i].x = players[i].x + players[i].speed;
- }
- /*
- if(keyState[players[i].upI]) {
- b.save();
- b.translate(player.x - canvas.width / 2, player.y - canvas.height / 2);
- b.fillStyle = "#f03a47";
- b.fillRect(0, 0, canvas.width, canvas.height);
- b.restore();
- }
- */
- }
- if(keyState[73]) {
- inform = true;
- } else {
- inform = false;
- }
- if(keyState[32] && cantFreeze == false) {
- if(freeze > 0) {
- freezeTime = true;
- timeDilation = 0.1;
- freeze -= 0.5;
- } else {
- cantFreeze = true;
- }
- } else {
- freezeTime = false;
- timeDilation = 1;
- if(freeze < maxFreeze) {
- freeze += 0.1;
- }
- if(cantFreeze == true) {
- if(Math.floor(freeze) == maxFreeze) {
- cantFreeze = false;
- }
- }
- }
- setTimeout(gameLoop, 10);
- }
- let p1;
- let p2;
- let ndog;
- let e1;
- let e2;
- let e3;
- let e4;
- let e5;
- let e6;
- let e7;
- let e8;
- let r1;
- function init() {
- b.fillStyle = "#f03a47";
- b.fillRect(-canvas.width, 0, canvas.width, canvas.height);
- p1 = new player(800, 400, 50, 50, "RoyalBlue", 5);
- //p2 = new player(800, 400, 50, 50, "Tomato", 5);
- e1 = new enemy(300, 200, 50, 50, "DarkRed");
- e2 = new enemy(-300, 700, 50, 50, "DarkRed");
- e3 = new enemy(-900, 200, 200, 200, "DarkRed");
- e3.health = 300;
- e3.healthI = 300;
- e4 = new enemy(-2300, 100, 50, 50, "DarkRed");
- e5 = new enemy(-2300, 700, 50, 50, "DarkRed");
- e6 = new enemy(-3000, 300, 50, 50, "DarkRed");
- e7 = new enemy(-3000, 500, 50, 50, "DarkRed");
- e8 = new enemy(-3900, 200, 200, 200, "DarkRed");
- e8.health = 400;
- e8.healthI = 400;
- p1.remap(87, 65, 83, 68);
- bullets = [];
- }
- init();
- setInterval(function () {
- b.save();
- offsetX = -p1.x + canvas.width / 2;
- offsetY = -p1.y + canvas.height / 2;
- b.translate(offsetX, offsetY);
- //b.scale(scalewidth, scaleheight);
- b.clearRect(p1.x - (canvas.width/2), p1.y - (canvas.height/2), canvas.width, canvas.height);
- b.fillStyle = "#66CDAA";
- b.fillRect(-1000, 0, 2000, 800);
- b.fillRect(-2000, 200, 1000, 400);
- b.fillRect(-4000, 0, 2000, 800);
- p1.draw();
- p1.fire(20, 0.1, 30, 30, 10, "RoyalBlue");
- //p2.draw();
- //p2.fire(20, 0.1, 30, 30, 10, "Tomato");
- e1.draw();
- e1.fire(5,1, 50, 50, "Red");
- e1.move("away and zig-zag", 2, 0, 700);
- //console.log(e1.dx * timeDilation * e1.Q);
- e1.cd(1);
- e2.draw();
- e2.fire(5,1, 50, 50, "Red");
- e3.draw();
- e3.fire(20,1, 50, 50, "Red");
- e3.move("away", 2, 100, 900);
- //r1.draw();
- e4.draw();
- e4.fire(7,1, 50, 50, "Red");
- e4.move("towards", 5, 0, 700);
- e5.draw();
- e5.fire(7,1, 50, 50, "Red");
- e5.move("towards", 5, 0, 700);
- e6.draw();
- e6.fire(5,1, 50, 50, "Red");
- e7.draw();
- e7.fire(5,1, 50, 50, "Red");
- e8.draw();
- e8.fire(10,0.4, 50, 50, "Red");
- //p2.draw();
- //p2.fire();
- b.restore();
- if(inform == true) {
- b.textAlign = "left";
- b.font = "20px Arial";
- b.fillStyle = "White";
- var RposX = Math.round(p1.x);
- var RposY = Math.round(p1.y);
- b.fillText("Canvas Proportions: " + canvas.width + ", " + canvas.height, 10, 90);
- b.fillText("P1 Position: " + RposX + ", " + RposY, 50, 120);
- b.fillText(bullets, 50, 150);
- b.fillText(freeze, 50, 180);
- }
- p1.healthbar(0.01*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "P1 Health");
- //p2.healthbar(0.5*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "P2 Health");
- //Freeze Bar
- var Freeze = new bar(0.5*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "Freeze", "PaleTurquoise", freeze, maxFreeze);
- for(var i = 0; i < players.length; i++) {
- if(players[i].health < players[i].healthI && mouseDown < 1 && click == false) {
- players[i].health += 0.1;
- }
- }
- var Tx;
- var Ty;
- var Twidth;
- var Theight;
- //checking if Bullets are overlaping
- for(var i = 0; i < bullets.length; i++) {
- Tx = bullets[i].x;
- Ty = bullets[i].y;
- Twidth = bullets[i].width;
- Theight = bullets[i].height;
- //console.log(Tx);
- for(var j = 0; j < bullets.length; j++) {
- //posX > x - Bsize && posX < x + width && posY > y - Bsize && posY < y + width
- if(j != i && Tx > bullets[j].x - Twidth && Tx < bullets[j].x + bullets[j].width && Ty > bullets[j].y - Theight && Ty < bullets[j].y + bullets[j].width && bullets[i].origin != bullets[j].origin) {
- var item = bullets[j];
- var item2 = bullets[i];
- //bullets = bullets.filter(bullets => bullets !== item);
- //bullets = bullets.filter(bullets => bullets !== item2);
- //bullets = [];
- }
- }
- //Bullets move
- bullets[i].move();
- }
- }, 20);
- gameLoop();
- </script>
- </body>
- <style>
- html, body {
- width: 100%;
- height: 100%;
- margin: 0;
- overflow: hidden;
- }
- #Canvy {
- background-color: #000000;
- }
- body {
- background-color: Black;
- }
- body {
- -webkit-touch-callout: none; /* iOS Safari */
- -webkit-user-select: none; /* Safari */
- -khtml-user-select: none; /* Konqueror HTML */
- -moz-user-select: none; /* Old versions of Firefox */
- -ms-user-select: none; /* Internet Explorer/Edge */
- user-select: none; /* Non-prefixed version, currently
- supported by Chrome, Opera and Firefox */
- }
- </style>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement