let dx = 2.5;//BUMBAS HORIZONTALAIS ATRUMS
let cx = 200;//BUMBAS HORIZONTALA SAKUMA POZICIJA
let cy = 300;// BUMBAS VERTIKALA SAKUMA POZICIJA
let dy = 2.5;// BUMBAS VERT ATRUMS
const radius = 10; //BUMBAS RADIUS
let ry = 245;//PLATFORMAS VERT ATRASANAS VIETA
let rx = 220;// PLATFORMAS HORIZONTALA SAKUMA ATRASANAS VIETA 
let rh = 5;//PLATFORMAS AUGSTUMS(SUS)
let rw = 50;//PLATFORMAS GARUMS
let lives = 3 ;// DZIVIBAS PUNKTI (canvas/2 vai puse no speles ekrana), tiek skaitits pikselos
let score = 0;// REZULTATS
let record = 0;  // rekords

//izveido speles lauku
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
 //lives = canvas.width/2;
 lives = 3;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);

function animateCircle (){//izveido bumbu
   requestAnimationFrame(animateCircle);
   ctx.clearRect(0, 0, canvas.width, canvas.height);
   ctx.fillStyle = 'black';
   ctx.fillRect(0, 0, canvas.width, canvas.height);
   ctx.beginPath();
   ctx.arc(cx, cy, radius, 0, Math.PI * 2, false);
   ctx.lineWidth = 3;
   ctx.strokeStyle = 'white';
   ctx.stroke();
   cx += dx;
   cy += dy;
   if(cx + radius > canvas.width || cx - radius < 0){
     dx = -dx;
   }
   
//Ja bumba atsitas, tad ...  else ...
   if (cy + radius > canvas.height){
    if(cx > rx && cx < rx + rw ){
      dx= dx + 0.25; //paatrina horiz atrumu
      dy = dy + 0.25; //paatrina vert atrumu
      dy = -dy; //lauj atsisties pret platformu
      score += 50;
      document.getElementById("id_scoreboard_number").innerHTML = score;// scoreboard ciparu savieno ar js
    }else {
       reset();   }
     } 
       if(lives < 0){ //speles beigas
           ctx.font = "30px Arial";
           ctx.fillStyle= "red";
           ctx.fillText("Game over!",165,100);
           score = 0;
           dx = 0;
           dy = 0;
          }




          
  if (cy - radius < 0){
       dy = -dy
  }
  if (score > record) {
    record = score; // atjauno rekordu, ja rezultāts ir lielāks
    document.getElementById("id_record_num").innerHTML = record; // pieprasa id
  }
}
function reset(){ //ja izkrit no laukuma (else... darbiba)
  document.getElementById("id_lives_number").innerHTML = lives;




 cy = 10;
 lives =lives - 1;
 dx = 2.5;
 dy = 2.5;
}

function restart(){ //restart poga jeb jauna spele
 cy = 300;
 dx = 2.5;
 dy = 2.5;
 lives = 3;//par plusu vai bez mainas vai vizuali strada vai tehniski strada
document.getElementById("id_lives_number").innerHTML = lives;
score = 0;
document.getElementById("id_scoreboard_number").innerHTML = score;
}
//platforma
function movingBar(){
     requestAnimationFrame(movingBar);
     ctx.fillStyle = 'blue';
     ctx.fillRect(rx, ry, rw, rh); 
}     
//KONTROLES AR BULTINAM
function eventHandler(e){
  switch (e.keyCode) {
  case 37:  //PA KREISI
      rx -= 30; //JUTIGUMS
   if (rx - rw < 0){
     rx = 0;
   }
  break;
  case 39:  //PA LABI
      rx += 30; //JUTIGUMS
   if (rx + rw > canvas.width){
     rx = canvas.width - rw;
    }
  break;
  }}

//palaiz js
function init (){
 animateCircle();
 movingBar();
}
// pareiza seciba palaiz kodu
init();
window.addEventListener('keydown', eventHandler, true);
 let button = document.getElementById('button');
 button.addEventListener('click', restart);




