From f6022fb3f0df87b3bd9a635e8f725261351d4a58 Mon Sep 17 00:00:00 2001 From: fkDeath Date: Tue, 12 May 2026 11:23:16 +0200 Subject: [PATCH] feat(game): Game is near to be finished --- public/scripts/hell-bullet.js | 39 +++++++++++++++++++++++++++++++++-- public/scripts/navbar.js | 8 +++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/public/scripts/hell-bullet.js b/public/scripts/hell-bullet.js index 53afe5a..56d4a44 100644 --- a/public/scripts/hell-bullet.js +++ b/public/scripts/hell-bullet.js @@ -2,14 +2,18 @@ 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; @@ -25,6 +29,9 @@ class Player { 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) } } } @@ -75,7 +82,7 @@ class Bullet { this.y += this.speedY; } this.draw = function() { - ctx.fillStyle = "red;" + ctx.fillStyle = "red"; ctx.fillRect(this.x, this.y, this.width, this.height); } } @@ -88,17 +95,45 @@ function runBullets() { 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(-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); } diff --git a/public/scripts/navbar.js b/public/scripts/navbar.js index 7ecc70e..d48ed4e 100644 --- a/public/scripts/navbar.js +++ b/public/scripts/navbar.js @@ -1,9 +1,9 @@ -const MENUBUTTON = document.querySelector (".nav-toggler") -const NAVIGATION = document.querySelector ("nav") +const MENUBUTTON = document.querySelector (".nav-toggler") // Get the div nav-toggler +const NAVIGATION = document.querySelector ("nav") // get the div nav -MENUBUTTON.addEventListener("click", toggleNav) +MENUBUTTON.addEventListener("click", toggleNav) // Listen when the user click on the button -function toggleNav(){ +function toggleNav(){ // show or no the menu MENUBUTTON.classList.toggle("active") NAVIGATION.classList.toggle("active") } \ No newline at end of file