Search code examples
javascripthtml2d-games

JavaScript point counter for a html snake like game


I'm new to learning JavaScript, I’ve searched many websites, and cannot find a solution so far.

I am making a mini-game on a base of snake, Can anybody help me make a point counter using javascript and html,

You can check out the game here!

https://github.com/LiquidSlime/Worm-game

and i would like to have it done using

<div id="points"<h1></h1></div>
and javascript;
var element = document.getElementById("points")
**Code here**;

hopefully someone can help me out!


Solution

  • You can make a span with the id points

    <span id=points>? Points</span>
    

    Then in js change the innnerText

    const pointElement = document.getElementById('points')
    
    function updatePoints(points) {
      pointElement.innerText = points + ' Points'
    }
    

    Then run the update points

    updatePoints(1)
    
    // If you want to load if from another varible
    
    // get points from somewhere else
    
    updatePoints(points)
    

    When you want it to update something would be like this in your code

    points++;
    updatePoints(points)