Home > Back-end >  Subtract an image that is representing a life in a game
Subtract an image that is representing a life in a game

Time:10-01

Im try to code a mini game. The game has two boxes you can click on. The boxes are assigned a random number between 1 and 2.

When the game starts, you have 5 lives. These lives are represented as 5 pictures of a small human.

When the player gets 3,5 and 7 correct points you get a extra life. Which i have achieved.

Now im trying to subtract a life everytime the player clicks on the wrong box.

function setRandomNumber(){ randomNumber = Math.floor( Math.random() * 2 1);

        if( randomNumber === 1){

            numberOfRightAnswersSpan.innerHTML = `${  correctNumber}`;
           
            outputDiv.innerHTML = `Det er riktig svar.`;

            if( correctNumber === 3 || correctNumber === 5 || correctNumber === 7){
                numberOfLivesDiv.innerHTML  = `<img src="images/person1.jpg"/>`

            }
            }
            else{

            numberOfWrongAnswersSpan.innerHTML = `${  wrongNumber}`;
            outputDiv.innerHTML = `Det er feil svar.`;
            
        }
        
    }

CodePudding user response:

I made a few changes to your code, but tried to keep it fairly similar to your original. I changed your template literals to strings. I changed your .innerHTML modifications to .innerText modifications. I refactored some whitespace to make your if statement more readable. Instead of adding the img element by appending the html string, I used the .appendChild method and createElement method to make the img elements. I also added a class to the element to make it easier to grab later.

function setRandomNumber() {
    randomNumber = Math.floor(Math.random() * 2   1);
    if (randomNumber === 1) {
        numberOfRightAnswersSpan.innerText =   correctNumber;
        outputDiv.innerText = "Det er riktig svar.";
        if (
          correctNumber === 3 ||
          correctNumber === 5 ||
          correctNumber === 7
        ) {
            const newLife = document.createElement("img");
            newLife.src = "images/person1.jpg";
            newLife.classList.add("life");
            numberOfLivesDiv.appendChild(newLife);
        }
    } else {
        numberOfWrongAnswersSpan.innerText =   wrongNumber;
        outputDiv.innerText = "Det er feil svar.";

        const aLife = document.querySelector(".life");
        aLife.remove();
    }
}

It is important to note at this point there is not logic to check for how many life exists, or what to do when lives run out. Eventually you may hit an error because there is not element with a class of life.

CodePudding user response:

I would re-render the lives every time the number of lives changes.

let number_of_lives = 5;
const lives_container = document.getElementById("user-lives");

function draw_lives(){
  lives_container.innerHTML = "";
  for(let i = 0; i < number_of_lives; i  ){
    const img = document.createElement("img");
    img.setAttribute("alt", "life.png");
    const new_life = img;
    lives_container.appendChild(new_life);
  };
  if(number_of_lives <= 0) lives_container.innerText = number_of_lives;
};

function remove_life() {
  number_of_lives--;
  draw_lives();
};

function add_life() {
  number_of_lives  ;
  draw_lives();
};


draw_lives();
#user-lives > * {
  margin: 10px;
  background: #7f7;
}
<div id="user-lives"></div>
<button onclick="add_life();">Add life</button>
<button onclick="remove_life();">Remove life</button>

  • Related