arkanoid

by Aboubacar Bah

HTML

<div id="field">
 <div class="quille"></div>
    
    <div class="quille2"></div>
    
    <div class="quille3"></div>
    
    <div class="quille4"></div>
    
    <div class="quille5"></div>
    
     <div class="quille6"></div>
     
     <div class="quille7"></div>
     
     <div class="quille8"></div>
     
     <div class="quille9"></div>
     
     <div class="quille10"></div>
     
     <div class="quille11"></div>
     
      <div class="quille12"></div>

      <div class="quille13"></div>
      
      <div class="quille14"></div>

      <div class="quille15"></div>
      
      <div class="quille16"></div>
      
      <div class="quille17"></div>

      <div class="quille18"></div>
      
      <div class="quille19"></div>
      
      <div class="quille20"></div>

      <div class="quille21"></div>
      
      <div class="quille22"></div>
      
      <div class="quille23"></div>
      
      <div class="quille24"></div>
      
      <div class="quille25"></div>
      
      <div class="quille26"></div>


    <div id="ball"></div>
    <div id="output"></div>
    <div id="attemptNode">2</div>
    <div id="scoreNode">0</div>
    <div id="strikeNode">0</div>
</div>

CSS

#field {
  height: 600px;
  width: 600px;
  border: 1px solid #ccc;;
  background-color: #dddddd;
}

#ball {
  border: 1px solid red;
  height: 20px;
  width: 20px;
  background-color: darkred;
  position: relative;
  border: 1px solid #955;
  border-radius: 50%;
  top: 570px;
  left: 190px;
}


.quille {
    width: 10px;
    height: 380px;
    margin: 1px;
    left: 20px;
    top : 180px;
    border: 1px solid;
    position: absolute;
}

.quille {
    background-color: silver;
    border-radius: 3px;
}

.quille2 {
    width: 120px;
    height: 10px;
    margin: 1px;
    display: flex;
    top : 170px;
    left : 20px;
    border: 1px solid;
    position: absolute;
}

.quille2 {
    background-color: silver;
    border-radius: 3px;
}

.quille3 {
    width: 10px;
    height: 50px;
    margin: 1px;
    top : 118px;
    left : 130px;
    position: absolute;
    border: 1px solid;
}

.quille4 {
    width: 60px;
    height: 10px;
    margin: 1px;
    top : 108px;
    left : 130px;
    position: absolute;
    border: 1px solid;
}


.quille5 {
    width: 10px;
    height: 100px;
    margin: 1px;
    top : 120px;
    left : 180px;
    position: absolute;
    border: 1px solid;
}


.quille6 {
    width: 250px;
    height: 10px;
    margin: 1px;
    top : 210px;
    left : 180px;
    position: absolute;
    border: 1px solid;
}

.quille6 {
    background-color: silver;
    border-radius: 3px;
}


.quille5 {
    background-color: silver;
    border-radius: 3px;
}


.quille4 {
    background-color: silver;
    border-radius: 3px;
}


.quille3 {
    background-color: silver;
    border-radius: 3px;
}

.quille7 {
    width: 10px;
    height: 250px;
    margin: 1px;
    top : 220px;
    left : 420px;
    position: absolute;
    border: 1px solid;
}

.quille7 {
    background-color: silver;
    border-radius: 3px;
}

.quille8 {
    width: 90px;
    height: 10px;
    margin: 1px;
    top : 460px;
    left : 328px;
    position: absolute;
    border: 1px solid;
}


.quille9 {
   ...

JavaScript

document.addEventListener("keydown", moveBall, false);

function moveBall(evt) {
  if (evt.which == 37 || evt.keyCode == 37) {
    mvleft();
  } else if (evt.which == 38 || evt.keyCode == 38) {
    mvup();
  } else if (evt.which == 39 || evt.keyCode == 39) {
    mvright();
  } else if (evt.which == 40 || evt.keyCode == 40) {
    mvdown();
  }
};

// initial location of the ball, and step size
var step = 5;
var bTop = 500;
var bLeft = 190;

// utility functions that will move the ball

// get a reference to the ball and field
var ball = document.getElementById("ball");
var field = document.getElementById("field");

// get the width and height of the field and ball
var fWidth = field.clientWidth;
var fHeight = field.clientHeight;
var bWidth = ball.offsetWidth;
var bHeight = ball.offsetHeight;


function mvup() {
  if (bTop - step >= 0) {
    bTop -= step;
  } else {
    bTop = 0;
  }
  ball.style.top = bTop + "px";
}

function mvdown() {
  if (bTop + step < fHeight - bHeight) {
    bTop += step;
  } else {
    bTop = fHeight - bHeight;
  }
  ball.style.top = bTop + "px";
}

function mvleft() {
  if (bLeft - step >= 0) {
  	bLeft -= step;
	} else {
  	bLeft = 0;
  }
  ball.style.left = bLeft + "px";
}

function mvright() {
	if (bLeft + step < fWidth - bWidth){
  	bLeft += step;
  } else {
  	bLeft = fWidth - bWidth;
  }
  ball.style.left = bLeft + "px";
}