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>Space Invaders</h1>
     <h3>Score: <span id="result"></span></h3>
  <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></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>
    <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>
  <ul>
    <li>move ship Left and Right arrow keys</li>
    <li>spacebar to shoot</li>
  </ul>
    
  </div>

 
</body>
</html>
              
            
!

CSS

              
                *{
  margin:0;
  padding:0;
}
body{
  background:#2B2B2B;
}
h1{
  color:#fcfeff;
  font-size: 3.3em;
  line-height: 1.2;
  margin-bottom:10px;
}

h3{
  color:#fff;
}

ul{
  display:felx;
  justify-content:center;
  margin-top:10px;
  color:#fcfeff;
  font-size:1.8rem;
  text-decoration:none;
  list-style:none;
  text-align:center;
  font-family:sans-serif;
}
li{
  margin-top:10px;
}
.container{
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%);
}


.grid{
  display:flex;
  flex-wrap:wrap;
  margin:0 auto;
  border: solid 5px #5cb85c;
  width:300px;
  height:300px;
  background-image: url('https://img.freepik.com/free-vector/sky-night-background-many-stars_135176-451.jpg?size=626&ext=jpg');
  background-position: center;
  background-size: 300%;
  background-repeat: repeat-y;
  background-position-y: -3rem;
  animation: ani 30s ease-in-out infinite;
  animation-direction: alternate-reverse;
}

@keyframes ani{
  0%{
    background-position-y: -3rem;
  }
  100%{
    background-position-y: 20rem;
  }
}


.grid div{
  width:20px;
  height:20px;  
}

  .shooter{
    position: relative;
    display:block;
    width:20px;
    height:20px;
    background:transparent;
  }
  .shooter::after{
    content:"🚀";
    position:absolute;
    width:20px;
    height:20px;
    position:absolute;
    top:0;
    left:0;
    right:0;
    margin:0 auto;
    display:block;

  }
  .invader{
    position: relative;
    display:block;
    width:20px;
    height:20px;
    background:transparent;
  }
  .invader::after{
    content:"👾";
    position:absolute;
    width:20px;
    height:20px;
    position:absolute;
    top:0;
    left:0;
    right:0;
    margin:0 auto;
    display:block;
  }
.laser{
  background:transparent;
  position:relative;
}
.laser::after{
  content:"";
  position:absolute;
  width:2px;
  height:5px;
  position:absolute;
  top:0;
  left:0;
  right:0;
  margin:0 auto;
  display:block;
  background:orange; 
}

              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', () => {
  const squares = document.querySelectorAll('.grid div')
  const resultDisplay = document.querySelector('#result')
  let width = 15
  let currentShooterIndex = 202
  let currentInvaderIndex = 0
  let alienInvadersTakenDown = []
  let result = 0
  let direction = 1
  let invaderId
  
  
  // define the alien invaders
   const alienInvaders = [
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
     15,16,17,18,19,20,21,22,23,24,
     30,31,32,33,34,35,36,37,38,39
   ]
   
  //draw the alien invaders
  alienInvaders.forEach( invader => squares[currentInvaderIndex + invader].classList.add('invader'))

  //draw the shooter
  squares[currentShooterIndex].classList.add('shooter')
  
  //move the shooter along a line
  function moveShooter(e){
    squares[currentShooterIndex].classList.remove('shooter')
    switch(e.keyCode){
      case 37:
      if(currentShooterIndex % width !==0) currentShooterIndex -=1
      break
      
      case 39:
      if(currentShooterIndex % width < width -1) currentShooterIndex +=1
      break 
    }
    squares[currentShooterIndex].classList.add('shooter')  
  }
  document.addEventListener('keydown', moveShooter)
  
  //move the alien invaders
  function moveInvaders() {
    const leftEdge = alienInvaders[0] % width === 0
    const rightEdge = alienInvaders[alienInvaders.length -1] % width === width -1
    
    if((leftEdge && direction === -1)  || (rightEdge && direction === 1)){
      direction = width
    }else if(direction === width){
      if (leftEdge) direction = 1
      else direction = -1
    }
    for (let i = 0; i <= alienInvaders.length -1; i++ ){
      squares[alienInvaders[i]].classList.remove('invader')
    }
    for (let i = 0; i <= alienInvaders.length -1; i++ ){
      alienInvaders[i] += direction
    }
      for (let i = 0; i <= alienInvaders.length -1; i++ ){
        if(!alienInvadersTakenDown.includes(i)){
          squares[alienInvaders[i]].classList.add('invader')
        }
    }
  
  }
  
  //deside a game over
  if(squares[currentShooterIndex].classList.contains('invader', 'shooter')){
    resultDisplay.textContent = 'Game Over'
    squares[currentShooterIndex].classList.add('boom')
    clearInterval(invaderId)
  }
  
  for(let i = 0; i <= alienInvaders.length -1; i++){
    if(alienInvaders[i] > (squares.length - (width-1))){
      resultDisplay.textContent ='Game Over'
      clearInterval(invaderId)
    }
    
    //decide a win
    if(alienInvadersTakenDown.length === alienInvaders.length){
      resultDisplay.textContent = "You Win"
      clearInterval(invaderId)
    }
  }
  invaderId = setInterval(moveInvaders, 500)
  
  
  //shoot at aliens
  function shoot(e){
    let laserId
    let currentLaserIndex = currentShooterIndex
    
    //move the laser from the shooter to the alien Invader
    function moveLaser(){
      squares[currentLaserIndex].classList.remove('laser')
      currentLaserIndex -= width
      squares[currentLaserIndex].classList.add('laser')
      if(squares[currentLaserIndex].classList.contains('invader')){
        squares[currentLaserIndex].classList.remove('laser')
        squares[currentLaserIndex].classList.remove('invader')
        squares[currentLaserIndex].classList.add('boom')
        
        setTimeout(() => squares[currentLaserIndex].classList.remove('boom'), 250 )
        clearInterval(laserId)
        
        const alienTakenDown = alienInvaders.indexOf(currentLaserIndex)
        alienInvadersTakenDown.push(alienTakenDown)
        result++
        resultDisplay.textContent = result
      }
      
      if(currentLaserIndex < width){
        clearInterval(laserId)
        setTimeout(() => squares[currentLaserIndex].classList.remove('laser'), 100)
      }
    }
    
    
    switch(e.keyCode){
      case 32:
        laserId = setInterval(moveLaser, 100)
        break
    }
  }
  
  
    document.addEventListener('keyup', shoot)
  
  
  
  
  
  
  
  
  })
              
            
!
999px

Console