var canvas = document.querySelector("canvas"); var ctx = canvas.getContext("2d"); canvas.style.backgroundColor = "rgb(255,255,255)"; var player; var bullets = []; let bulletInterval = 0; 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); ctx.font = "20px Arial"; ctx.fillStyle = "white"; ctx.fillText (this.lives, this.x+6, this.y+17) } } } 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)); bullets.push(new Bullet(500, y2, -2, 0)); bullets.push(new Bullet(x1, -10, 0, 2)); bullets.push(new Bullet(x2, 500, 0, -2)); } for (let i = 0; i < bullets.length; i++) { bullets[i].update(); bullets[i].draw(); if (bullets[i] && bullets[i].x < -10 || bullets[i].x > 490 || bullets[i].y < -10 || bullets[i].y > 490) { bullets.splice(i, 1); } if (bullets[i] && collision(player, bullets[i])) { player.lives--; bullets.splice(i, 1); i--; } } } function collision(first, second) { if (first.x < second.x + second.width && first.x + first.width > second.x && first.y < second.y + second.height && first.y + first.height > second.y) { return true; } } function addScore() { scoreInterval++; if (scoreInterval % 5 == 0) { score++; } ctx.font = "30px Arial"; ctx.fillStyle = "green"; ctx.fillText("Score: " + score, 5, 30); } function runGame() { ctx.clearRect(0, 0, 500, 500) runPlayer(); runBullets(); addScore(); if(!gameOver) { requestAnimationFrame(runGame); } } requestAnimationFrame(runGame);