Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=">
  <title></title>
</head>
<body>
  <div class="container">
    <h1>Snakes</h1>
    <div class="score">Score: <span>0</span></div>
    <div class="grid">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
    <button class="start">Start/Restart</button>
    <p>Use arrow key to move snake</p>
    <p style="margin:5px; auto;text-align:center;"> ⬅⬆⬇➡ </p>
  </div>

</body>
</html>
              
            
!

CSS

              
                *{
  margin:0;
  padding:0;
}
body{
  background:#2B2B2B;
  font-family:sans-serif;  
}
.container{
  position:absolute;
  top:40%;
  left:50%;
  transform:translate(-50%, -50%);
}
h1{
  color:#fff;
  font-size:3.5rem;
  margin-bottom:5rem;
  text-transform:uppercase;
  color:#5cb85c;
}
p{
  color:#fff;
  font-size:1.5em;
  margin-top: 3rem;
  
}
.score{
  color:#fff;
  font-size:2rem;
}
.start{
  font-size: 18px;
  line-height: 1.3333333;
  border-radius: 6px;
  padding: 10px 30px;
  background-color: #5cb85c;
  color:#fcfeff;
  border-color: transparent;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  margin-top: 20px;
}
.grid{
  width:200px;
  height:200px;
  display:flex;
  flex-wrap:wrap;
  border-style:solid;
  border:5px solid #5cb85c;
  background:#a29d9b;
}
.grid div{
  width:20px;
  height:20px;
}
.snake{
  background:#47484A;
}
.apple{
/*   background:purple; */
  background-image: url('https://images.vexels.com/media/users/3/188899/isolated/lists/b53c80cc51ec7c0d6dd3eec76837cad8-apple-icon-stroke.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size: 20px;
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', () => {
  const squares = document.querySelectorAll('.grid div')
  const scoreDisplay = document.querySelector('span')
  const startBtn = document.querySelector('.start')
  
  const width = 10
  let currentIndex = 0 //so first div is our grid
  let appleIndex = 0 // so first div is our grid
  let currentSnake = [2,1,0] // so the div is our grid being 2 ( or the HEAD), snf 0 nrinh ythr end (TAIL, width all 1's being the body for now on)
  let direction = 1
  let score = 0
  let speed = 0.9
  let intervalTime = 0
  let interval = 0
  
  
  //to start, and restart the game
  function startGame() {
    currentSnake.forEach(index => squares[index].classList.remove('snake'))
    squares[appleIndex].classList.remove('apple')
    clearInterval(interval)
    score = 0
    randomApple()
    direction = 1
    scoreDisplay.innerText = score
    intervalTime = 1000
    currentSnake = [2,1,0]
    currentIndex = 0
    currentSnake.forEach(index => squares[index].classList.add('snake'))
    interval = setInterval(moveOutcomes, intervalTime)
  }
  
  //function that deals with ALL  the over outcomes of the snake
  function moveOutcomes(){
      //deals with snake hitting border and snake hitting self
    if(
      (currentSnake[0] + width >= (width * width) && direction === width ) || // if snake hits bottom 
      (currentSnake[0] % width === width -1 && direction === 1) || //if snake hits right wall 
      (currentSnake[0] % width === 0 && direction === -1) || //if snake hits the left
      (currentSnake[0] - width < 0  && direction === -width) || //if nsake hits the top
      squares[currentSnake[0] + direction].classList.contains('snake') //if snake goes into itself 
    ) {
      return clearInterval(interval) //this will clear the interval if any of the above happen
    }
    
    const tail = currentSnake.pop() // removres last  of the array and show it 
    squares[tail].classList.remove('snake') //remove class of snake from the TAIL
    currentSnake.unshift(currentSnake[0] + direction) //gives direction to the head of snake
    
    //deals withsnake getting 
    if(squares[currentSnake[0]].classList.contains('apple')){
      squares[currentSnake[0]].classList.remove('apple')
      squares[tail].classList.add('snake')
      currentSnake.push(tail)
      randomApple()
      score++
      scoreDisplay.textContent = score
      clearInterval(interval)
      intervalTime = intervalTime * speed
      interval = setInterval(moveOutcomes, intervalTime)
    }
    squares[currentSnake[0]].classList.add('snake')
  }
  
  
  //genterate new apple once apple is eaten
  function randomApple(){
    do{
      appleIndex = Math.floor(Math.random() * squares.length)
    } while(squares[appleIndex].classList.contains('snake')) //making sure apple 
      squares[appleIndex].classList.add('apple')
  }
  
  
  
  //assign functions to keycodes
  function control(e) {
    squares[currentIndex].classList.remove('snake') //we are removing the class of snake from All the squares.
    
    if(e.keyCode === 39){
      direction = 1 // if we press the right arrow on our keyboard, the snake will go right one
    } else if (e.keyCode === 38){
      direction = -width // if we press the up arrow, the snake will go back 
    } else if (e.keyCode === 37){
      direction = -1 // if we press left, the snake will go left one div
    } else if(e.keyCode === 40){
      direction = +width //if we press down, the snake head will instantly appear
    }   
  }
  
  document.addEventListener('keyup', control)
  startBtn.addEventListener('click', startGame)
  
  
  
  
  
})
              
            
!
999px

Console