Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Doom</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 ammo = 1000;
- var ammomax = 30;
- 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;
- })
- addEventListener('resize', () => {
- canvas.width = innerWidth;
- canvas.height = innerHeight;
- init();
- })
- function bullet(Ix, Iy, width, height, color, speed, 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.target;
- if(dx == null && dy == null) {
- this.dx = (mouse.x - this.Ix - 0.5*this.width) / 100;
- this.dy = (mouse.y - this.Iy - 0.5*this.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 && this.x > -this.width && this.y < canvas.height && this.y > -this.height) {
- this.x += this.dx;
- this.y += this.dy;
- b.fillStyle = color;
- b.fillRect(this.x, this.y, width, height);
- }
- /*
- if(posX > this.x - Bsize && posX < this.x + this.width && posY > this.y - Bsize && posY < this.y + this.height) {
- }
- */
- }
- }
- let bullets = [];
- function gun(speed, reload, iammo, width, height, color) {
- }
- function player(Ix, Iy, width, height, color, dx, dy) {
- players.push(this);
- this.x = Ix;
- this.y = Iy;
- this.width = width;
- this.height = height;
- this.dx = dx;
- this.dy = dy;
- this.upI = 38;
- this.downI = 40;
- this.leftI = 37;
- this.rightI = 39;
- this.slot1 = "empty";
- this.slot2 = "empty";
- this.slot3 = "empty";
- this.reloading;
- this.draw = function() {
- b.fillStyle = color;
- b.fillRect(this.x, this.y, this.width, this.height);
- }
- this.remap = function(up, left, down, right) {
- this.upI = up;
- this.leftI = left;
- this.downI = down;
- this.rightI = right;
- }
- this.fire = function() {
- /*
- 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(this.reloading != true && click == true && ammo > 0) {
- if(bullets.length > 20) {
- bullets = [];
- }
- //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.5*width, this.y + 0.5*this.height - 0.5*height, width, height, "SteelBlue", 5);
- ammo -= 1;
- click = false;
- this.reloading = true;
- setTimeout(() => { this.reloading = false; }, 0100);
- }
- for(var i = 0; i < bullets.length; i++) {
- bullets[i].draw();
- }
- }
- }
- let players = [];
- function gameLoop() {
- for(var i = 0; i < players.length; i++) {
- if(keyState[players[i].upI]) {
- players[i].y = players[i].y - players[i].dy;
- }
- if(keyState[players[i].downI]) {
- players[i].y = players[i].y + players[i].dy;
- }
- if(keyState[players[i].leftI]) {
- players[i].x = players[i].x - players[i].dx;
- }
- if(keyState[players[i].rightI]) {
- players[i].x = players[i].x + players[i].dx;
- }
- /*
- 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;
- }
- setTimeout(gameLoop, 10);
- }
- let p1;
- let p2;
- function init() {
- b.fillStyle = "#f03a47";
- b.fillRect(0, 0, canvas.width, canvas.height);
- p1 = new player(0.5*canvas.width, 0.5*canvas.height, 20, 20, "white", 5, 5);
- p2 = new player(0.5*canvas.width, 0.5*canvas.height, 20, 20, "white", 5, 5);
- p1.remap(87, 65, 83, 68);
- bullets = [];
- }
- //resizeGame();
- init();
- setInterval(function () {
- b.save();
- b.translate(-p1.x + canvas.width / 2, -p1.y + canvas.height / 2);
- b.clearRect(p1.x - (canvas.width/2), p1.y - (canvas.height/2), canvas.width, canvas.height);
- b.fillStyle = "#f03a47";
- b.fillRect(0, 0, canvas.width, canvas.height);
- if(document.hasFocus() == false) {
- var keyState = {};
- }
- p1.draw();
- p1.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, 60);
- b.fillText("P1 Position: " + RposX + ", " + RposY, 50, 90);
- }
- //Ammo Tracker
- var tsize = canvas.width / 50;
- b.textAlign = "right";
- b.font = tsize + "px Arial";
- b.fillStyle = "White";
- b.fillText(ammo + " / " + ammomax, canvas.width - 0*tsize, canvas.height - 0.1*tsize);
- //p1.draw();
- //p1.fire();
- }, 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