Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <div id="space">
  <h1 id="livesCount">5</h1>
  <h1 id="killCount">0</h1>
  <div id="start">START</div>
  <div id="spaceship">
    <div class="wing"></div>
    <div class="wing"></div>
    <div class="flames"></div>
  </div>
</div>

<script type="template/html" id="lazer-tpl">
  <div class="lazer"></div>
</script>

<script type="template/html" id="star-tpl">
  <div class="star"></div>  
</script>

<script type="template/html" id="enemy-tpl">
  <div class="enemy"></div>
</script>
              
            
!

CSS

              
                @import "compass/css3";

body {
  overflow: hidden;
  font-family: Futura, Helvetica, Arial, sans-serif;
}

#space {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #141414;
  @include transform(translate3d(0, 0, 0));
  @include transition(all 0.1s);
  &.started {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: none;
    #start {
      display: none;
    }
    #spaceship {
      display: block; 
    }
  }
  &.hit {
    background: #341414;
    @include box-shadow(inset 0 0 50px rgba(255, 0, 0, 0.6));
  }
}

h1 {
  position: absolute;
  top: 2%;
  left: 2%;
  font-size: 48px;
  line-height: 1;
  margin: 0;
  color: green;
  
  &#livesCount {
    left: auto;
    right: 2%;
    color: red;
  }
}

#start {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -25%;
  margin-top: -60px;
  font-size: 60px;
  line-height: 120px;
  width: 50%;
  text-align: center;
  background: red;
  color: white;
  cursor: pointer;
  &:hover {
    background: #cc0000; 
  }
}

#spaceship {
  display: none;
  position: absolute;
  z-index: 1;
  bottom: 50px;
  left: 50%;
  width: 40px;
  height: 50px;
  border-right: 10px solid #cc0000;
  background: #ff0000;
  @include transform(translate3d(0,0,0));
  &:before,
  &:after {
    @extend %triangle;
    left: 0;
  }
  &:before {
    top: -25px;
    border-bottom: 25px solid #cc0000;
    border-left: 25px solid transparent;
    border-right: 25px solid transparent;
  }
  &:after {
    top: -20px;
    border-bottom: 20px solid #ff0000;
    border-left: 20px solid transparent;
    border-right: 20px solid transparent;
  }
  .wing {
    float: left;
    position: relative;
    left: -10px;
    top: 40px;
    width: 10px;
    height: 20px;
    background: #ff0000;
    
    + .wing {
      float: right;
      left: 20px;
      background: #cc0000;
    }
  }
  .flames {
    position: absolute;
    bottom: 0;
    left:0;
    &:before,
    &:after {
      @extend %triangle;
      border-top: 25px solid rgba(255, 135, 40, 0.7);
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
    }
    &:before {
      left: 10px;
    }
    &:after {
      left: 20px;
    }
    &.burn {
     &:before,
     &:after {
      border-top: 28px solid rgba(255, 100, 0, 0.7);
     }
    }
  }
}

.lazer {
  position: absolute;
  bottom: 51px;
  width: 8px;
  height: 8px;
  background: #12f812;
  @include transition(bottom 0.666s linear);
  &.shot {
   bottom: 100%;
  }
}

.enemy {
  position: absolute;
  top: -50px;
  width: 50px;
  height: 50px;
  background: yellow;
  @include transition(top 4s linear);
  @include transform(translate3d(0,0,0));
  &.moving {
    top: 100%;
  }
  &.exploding {
    @include transition(all 0.1s linear);
    background: red;
    width: 60px;
    height: 60px;
    margin-left: -5px;
    margin-top: -5px;
  }
}

.star {
  position: absolute;
  top: 0;
  width: 4px;
  height: 4px;
  background: rgba(255, 255, 255, 0.3);
  @include transition(transform 2s linear);
  &.fast {
    @include transition(transform 2.5s linear); 
  }
  &.shooting {
    @include transform(translate3d(0, 1200px, 0)); 
  }
}

%triangle {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
}

              
            
!

JS

              
                var spaceshipGame = function() {
  var $space = $('#space');
  var $spaceship = $('#spaceship');
  var lazerTpl = $('#lazer-tpl').html();
  var starTpl = $('#star-tpl').html();
  var enemyTpl = $('#enemy-tpl').html();
  var $livesCount = $('#livesCount');
  var $killCount = $('#killCount');
  var $window = $(window);
  var animationEnd = 'webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend';
  var spawnMap = {};
  var lives, kills, winW, winH, animationLoop, animateSpeed, animateFaster, spawnLoop;
  
  var randomPos = function() {
    return Math.floor((Math.random()*winW)+1)
  };
  
  var animations = function() {
    var $flames = $('.flames');
    
    function animate() {
      var $star = $(starTpl);
      var starPos = randomPos();
      
      $flames.toggleClass('burn');
      $space.append($star);
     $star
        .css('left', starPos)
        .toggleClass('fast', Math.random()<.5);
      setTimeout(function() {
        $star.addClass('shooting');
      }, 5);
      $star.on(animationEnd, function() { $star.remove();
      });
    }
    var animationLoop = setInterval(animate, animateSpeed);
  };
  
  var spawnEnemies = function() {
    spawnLoop = setInterval(newEnemy, 1000);
  }
  
  var newEnemy = function() {
    var $newEnemy = $(enemyTpl);
    var newEnemyPos = Math.max(Math.floor(randomPos()) - 50, 100);
    $space.append($newEnemy);
    $newEnemy.css('left', newEnemyPos);
    spawnMap[newEnemyPos] = $newEnemy;
    setTimeout(function() {
      $newEnemy.addClass('moving');
    }, 10);
    $newEnemy.on(animationEnd, enemyLanded);
  };
  
  var enemyLanded = function() {
    var $enemy = $(this);
    $enemy.remove();
    $space.addClass('hit');
    setTimeout(function() {
      $space.removeClass('hit');
    }, 100);
    delete spawnMap[$enemy.offset().left];
    lives--;
    $livesCount.html(lives);
    if (!lives) gameOver();
  };
  
  var track = function(e) {
    $spaceship.css('left', e.pageX - 25);
  };
  
  var shoot = function(e) {
    var $lazer = $(lazerTpl);
    var lazerPos = e.pageX - 4;
    $space.append($lazer);
    $lazer.css('left', lazerPos);
    setTimeout(function() {
      $lazer.addClass('shot');
    }, 10);
    $.each(spawnMap, function(enemyPos, $enemy) {
      if (enemyPos > lazerPos - 50 && enemyPos < lazerPos + 56 && !$enemy.is('.exploding')) {
        var delayToHit = Math.max(666 - (($enemy.position().top / winW) * 666), 10);
        setTimeout(function() {
          explode($enemy);
          $lazer.remove();
        }, delayToHit);
        delete spawnMap[enemyPos];
        return;
      }
    });
  };
  
  var explode = function($enemy) {
    $enemy.off(animationEnd);
    $enemy.addClass('exploding');
    $enemy.on(animationEnd, function() {
      $enemy.remove();
    });
    kills++;
    $killCount.html(kills);
    if (kills === 99) gameOver(true);
  };
  
  var gameOver = function(win) {
    $('#start').html(win ? 'YOU WIN' : 'RETRY?');
    $space.find('.enemy').remove();
    $space.removeClass('started');
    clearInterval(spawnLoop);
    clearInterval(animationLoop);
    $('.lazer').remove();
    $window.off('mousemove mousedown');
    spawnMap = {};
  };
  
  var init = function() {
    animateSpeed = 66;
    lives = 5;
    kills = 0;
    $livesCount.html(lives);
    $killCount.html(kills);
    winW = $window.width();
    winH = $window.height();
    $space.addClass('started');
    $window.on('mousemove', track);
    $window.on('mousedown', shoot);
    animations();
    spawnEnemies();
  };
  
  return init();
};

$('#start').on('click', spaceshipGame);

              
            
!
999px

Console