var canvas = document.querySelector("canvas"); var ctx = canvas.getContext("2d"); canvas.style.backgroundColor = "rgb(255,255,255)"; var player; var bullets = []; let score = 0; let scoreInterval = 0; let pressedKeyX = "none"; let pressedKeyY = "none"; let gameOver = false; class Player { constructor() { this.x = 240; this.y = 240; this.width = 20; this.height = 20; this.lives = 3; this.update = function() { if (this.lives <= 0) { gameOver = true; } }; this.draw = function() { ctx.fillStyle = "blue"; ctx.fillRect(this.x, this.y, this.width, this.height); } } } player = new Player; function runPlayer() { player.update(); player.draw(); if (pressedKeyX == "A" && player.x > 0) { player.x -= 2.5; } if (pressedKeyX == "D" && player.x < 480) { player.x += 2.5; } if (pressedKeyY == "W" && player.y > 0) { player.y -= 2.5; } if (pressedKeyY == "S" && player.y < 480) { player.y += 2.5; } } window.addEventListener("keydown", function(e) { if (e.code === "KeyW" && pressedKeyY == "none") pressedKeyY = "W"; else if (e.code === "KeyS" && pressedKeyY == "none") pressedKeyY = "S"; else if (e.code === "KeyA" && pressedKeyX == "none") pressedKeyX = "A"; else if (e.code === "KeyD" && pressedKeyX == "none") pressedKeyX = "D"; }); window.addEventListener("keyup", function(e) { if (e.code === "KeyW" && pressedKeyY == "W") pressedKeyY = "none"; else if (e.code === "KeyS" && pressedKeyY == "S") pressedKeyY = "none"; else if (e.code === "KeyA" && pressedKeyX == "A") pressedKeyX = "none"; else if (e.code === "KeyD" && pressedKeyX == "D") pressedKeyX = "none"; }); class Bullet { constructor (x, y, speedX, speedY) { this.x = x; this.y = y; this.width = 10; this.height = 10; this.speedX = speedX; this.speedY = speedY; this.update = function() { this.x += this.speedX; this.y += this.speedY; } this.draw = function() { ctx.fillStyle = "red;" ctx.fillRect(this.x, this.y, this.width, this.height); } } } function runBullets() { bulletInterval++; if (bulletInterval % 50 == 0) { let y1 = Math.floor(Math.random ()* (491-0)) + 0; let y2 = Math.floor(Math.random ()* (491-0)) + 0; let x1 = Math.floor(Math.random ()* (491-0)) + 0; let x2 = Math.floor(Math.random ()* (491-0)) + 0; bullets.push(new Bullet(-10, y1, 2, 0)) } for (let i = 0; i < bullets.length; i++) { bullets[i].update(); bullets[i].draw(); } } function runGame() { ctx.clearRect(0, 0, 500, 500) runPlayer(); if(!gameOver) { requestAnimationFrame(runGame); } } requestAnimationFrame(runGame);