Home > Back-end >  Add an score counter in JavaScript
Add an score counter in JavaScript

Time:12-25

During a course I was taught how to develop a little game, but I wanted to take it to the next level and add a score counter. This is the best that I´ve got so far, but the problem is that every time the score increase, the game disappears and the only thing the screen shows is the score.

<meta charset="utf-8">

<canvas width="600" height="400"></canvas>

<h1>Puntos totales:</h1>
<p id="score"></p>


<script type="text/javascript">

var pantalla = document.querySelector("canvas");
var pincel = pantalla.getContext("2d");


pincel.fillStyle = "Lightgrey";
pincel.fillRect(0,0,600,400);

var radio = 15;
var xAleatorio;
var yAleatorio;
var score = 0


function disenharCircunferencia(x,y,radio,color) {

    pincel.fillStyle = color;
    pincel.beginPath();
    pincel.arc(x,y,radio,0,2*Math.PI);
    pincel.fill();

}

function limpiarPantalla(){

    pincel.clearRect(0,0,600,600); //limpiar el canvas, se le pasan las coordenadas del canvas

}   


var x = 0


function disenharObjetivo(x,y){

    disenharCircunferencia(x,y,radio 30,"red");
    disenharCircunferencia(x,y,radio 15,"white");
    disenharCircunferencia(x,y,radio,"red");

}

function sortearPosicion(maximo){

    return Math.floor(Math.random()*maximo); //redondea para abajo

}

function actualizarPantalla(){

    limpiarPantalla();
    xAleatorio = sortearPosicion(600);
    yAleatorio = sortearPosicion(400);
    disenharObjetivo(xAleatorio,yAleatorio);


}

setInterval(actualizarPantalla,5000); //Llamar funciones cada cierto tiempo (en este caso cada 5 milisegundos)

function dispararCentro(evento){

    var x = evento.pageX - pantalla.offsetLeft; //capturo la coordenada x y le resto el offset
    var y = evento.pageY - pantalla.offsetTop; //capturo la coordenada y y le resto el offset

    console.log(evento.pageX)

    if ( (x < xAleatorio   radio) && (x > xAleatorio - radio) && (y < yAleatorio   radio) && (y > yAleatorio - radio) ) {

        score = score   20;
        document.write(score);
        alert("Tiro certero. Puntos acumulados "   score);

    }
    else if ( (x < xAleatorio   (radio 15)) && (x > xAleatorio - (radio 15)) && (y < yAleatorio   (radio 15)) && (y > yAleatorio - (radio 15)) ) {

        score = score   10;
        document.write(score);
        alert("Tiro certero. Puntos acumulados "   score);

    } 
    else if ( (x < xAleatorio   (radio 30)) && (x > xAleatorio - (radio 30)) && (y < yAleatorio   (radio 30)) && (y > yAleatorio - (radio 30)) ) {

        score = score   5;
        document.write(score);
        alert("Tiro certero. Puntos acumulados "   score);
    }
}


pantalla.onclick = dispararCentro;





</script> 

CodePudding user response:

When you document.write, you're replacing the contents of the document, discarding everything. Considering you already have an element with id="score", just use that, and replace every

document.write(score);

with, for example,

document.querySelector("#score").textContent = score;
  • Related