feat(game): Game is near to be finished
This commit is contained in:
2 files changed
+41
-6
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in new issue
Block a user