Home » Javascript » Reduce score when missed a target in JavaScript

Reduce score when missed a target in JavaScript

Posted by: admin August 19, 2018 Leave a comment

Questions:

I want to remove 1 point from the player score when the player had missed the zombie. I tried something with a score function which is called after the setTimeout () but it doesn’t work.

The idea is that the zombie appears, stays on screen, and then disappears if the player doesn’t click on it. If it happens, -1 to the score.

window.onload = build;

/* Variables*/
var zombie;
var timer;
var invasion;
var score = 10;
var sound1 = new Audio("sound/magnum.mp3");
var sound2 = new Audio("sound/crossbow.mp3");
var sound3 = new Audio("sound/baseball-bat.mp3");

/* Var survived = a time to count the time survived before score = 0*/
document.querySelector('.score__counter__number').innerHTML = 10;
var kill = true;

/* Add event listener 'click' on gameboard__zombie, call hitzombie() which 
is set to false */
function build() {
zombie = document.querySelectorAll('.gameboard__zombie');
  for (var x = 0; x < zombie.length; x++) {
    zombie[x].addEventListener('click', hitzombie, false);
  }
}

/* Launch the game */
function start() {
popup();
}

/* Set kill to true, a random window will create a zombie in a random time. The zombie disappears, call hidezombie(), also in a random time */
function popup() {
kill = true;
invasion = zombie[Math.floor(Math.random() * zombie.length)];
invasion.classList.add('zombie--popup');
var zTime = 2000;
timer = setTimeout(hidezombie, zTime);
}

/* The class which set opacity to 1 in css is removed, and popup() is called to create a new zombie */
function hidezombie() {
  if (score >= 1) {
    invasion.classList.remove('zombie--popup');
    popup();
    score();
    weapon();
  } else {
    end();
  }

}
/* Reduce score -1 when a zombie disappears.*/
function score() {
score--;
document.querySelector('.score__counter__number').innerHTML = score;
}

/* Remove the zombie, kill = false to avoid multiply clicks, score -1, call popup() to create a new zombie */
function hitzombie() {
  if (score > 7) {
    sound1.play();
    event.target.classList.remove('zombie--popup');
    if (kill) {
        kill = false;
        score = score;
    }
  } else if (score <= 7) {
    sound2.play();
    event.target.classList.remove('zombie--popup');
    if (kill) {
        kill = false;
        score = score;
    }
  } else if (score >= 3 || score >= 1) {
    sound3.play();
    event.target.classList.remove('zombie--popup');
    if (kill) {
        kill = false;
        score = score;
    }
    popup();
  } else {
    end();
}

/* Switch weapon image depending on the score */
function weapon() {
  if (score == 7) {
    document.getElementsById('weapon__image').src = "images/DarylCrossbow.png";
  }
  if (score == 3) {
    document.getElementsById('weapon__image').src = "images/NeganBat.png";
  }
}

/* End the game */
function end() {
invasion.classList.remove('zombie--popup');

timeSurvived = document.querySelector('.score__survive');
timeSurvived.style.fontSize = "5rem";

var blinkSpeed = 3000;
var blinkInterval = setInterval(function () {
    var ele = document.querySelector('.score__start');
    ele.style.visibility = (ele.style.visibility == 'hidden' ? '' : 'hidden');
}, blinkSpeed);
}
Answers: