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

              
                <body data-ng-app="game" data-ng-controller="GameController">

<div class="container clearfix">

  <div class="header" data-ng-class="{visible: gameStarted}">
    <div class="score clearfix">
      <div class="score-option" data-ng-class="{increased: timeIncreased, decreased: timeDecreased, fail: time<3, win: win}">
        <div>Time Left</div>
        <div class="score-value">{{time}}</div>
      </div>
      <div class="score-option" data-ng-class="{increased: triesIncreased, decreased: triesDecreased, fail: tries<3, win: win}">
        <div>Tries Left</div>
        <div class="score-value">{{tries}}</div>
      </div>
      <div class="score-option" data-ng-class="{win: win}">
        <div>Unmatched Pairs</div>
        <div class="score-value">{{unmatched}}</div>
      </div>
    </div>
  </div>

  <div class="gameboard clearfix" data-ng-class="{gameover: gameFinished, win: win, fail: fail}">

    <div class="dimmer"></div>

    <div class="gameover-popup" data-ng-click="newGame()">
      <div class="title" data-ng-show="win">You won!</div>
      <div class="title" data-ng-show="fail">You loose!</div>
      <span><i class="ico"></i> Play Again</span>
    </div>

    <div data-ng-repeat="image in randomImages" class="card" data-ng-class="{flipped: !!image.flipped}" data-ng-click="flip(image)">
      <div class="card-frontside"><img data-ng-src="{{image.src}}" /></div>
      <div class="card-backside"></div>
    </div>
  </div>
</div>

<div class="showroom" data-ng-class="{visible: gameFinished}">
  <div class="row clearfix">
    <div class="title"><a href="https://icons8.com/web-app/"><strong>Download</strong> these images</a></div>
    <div class="image" data-ng-repeat="image in images">
      <a href="{{image.url}}"><img data-ng-src="{{image.src}}" /></a>
      <a href="{{image.url}}">{{image.name}}</a>
    </div>
  </div>
  <div class="row">
    <div class="title"><a href="https://api.icons8.com/">Get images using <strong>Icons8 API</strong></a></div>
      <pre><code># <a href="https://api.icons8.com/manual/latest">get latest icons</a>
curl "<a href="https://api.icons8.com/api/iconsets/latest?amount=50">https://api.icons8.com/api/iconsets/latest?amount=50</a>"

# <a href="https://api.icons8.com/manual/icon">get these icons</a>
curl "<a href="https://api.icons8.com/api/iconsets/icon?id={{iconsIds()}}">https://api.icons8.com/api/iconsets/icon?id={{iconsIds()}}</a>"</code></pre>
  </div>
</div>
</body>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Noto+Sans:400,700');
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro');

body {
    font-family: 'Noto Sans', sans-serif;
    font-size: 18px;
    margin: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

*, *:before, *:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

.gameboard {
    position: relative;
    margin: auto;
    width: 432px;
    height: 432px;

    perspective: 1000;
}

.clearfix:after {
    content: '';
    display: block;
    clear: both;
}

.card {
    float: left;
    width: 104px;
    height: 104px;

    padding: 2px;
    margin: 2px;
    position: relative;
    border-right: 2px #fff solid;
    border-bottom: 2px #fff solid;

    transform-style: preserve-3d;
    transition: all .25s ease-in-out;

    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
}
    .card:hover {
        box-shadow: 0 5px 10px rgba(0,0,0,0.2);
    }
    .card-frontside, .card-backside {
        position: absolute;
        z-index: 1;
        width: 100px;
        height: 100px;
        background-size: 100%;
        background-repeat: no-repeat;
        background-position: 50% 50%;
    }
    .card-frontside {
        transform: rotateY(0deg);
    }
    .card-backside {
        z-index: 2;
        background-color: #fff;
        background-image: url(https://maxcdn.icons8.com/iOS7/PNG/100/Programming/help_filled-100.png);
        backface-visibility: hidden;
        transform: rotateY(-180deg);
    }
    .card:hover .card-backside {
        background-image: url(https://maxcdn.icons8.com/iOS7/PNG/100/Programming/help-100.png);
    }
    .card.flipped {
        transform: rotateY(180deg);
    }

.gameboard .dimmer {
    background: rgba(255, 255, 255, 0.5);
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    display: none;
    filter: blur(20px);
    -webkit-filter: blur(20px);
}

.gameover-popup {
    display: none;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -80px;
    margin-top: -48px;
    width: 160px;
    height: 96px;
    z-index: 15;
    line-height: 48px;
    text-align: center;
    background: #8BC34A;
    border: 1px solid #43A047;
    color: #fff;
    border-radius: 5px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out;
    cursor: pointer;
}
    .gameover-popup:hover {
        -webkit-transform: translateY(-5px);
        transform: translateY(-2px);
        box-shadow: 0 5px 5px rgba(0,0,0,0.2);
    }
        .gameover-popup:hover .ico {
            -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=2)";
            -webkit-transform: rotate(180deg);
            -ms-transform: rotate(180deg);
            transform: rotate(180deg);
        }
    .gameover-popup .title {
    }
    .gameover-popup .ico {
        display: inline-block;
        float: left;
        width: 48px;
        height: 48px;
        background-image: url(https://maxcdn.icons8.com/Color/PNG/48/Baby/paper_windmill-48.png);
        background-size: 100%;
        transition: all .25s ease-in-out;
    }
    .gameboard.gameover .card {
        filter: blur(2px);
        -webkit-filter: blur(2px);
    }

.gameboard.gameover .gameover-popup {
    display: block;
}
.gameboard.gameover .dimmer {
    display: block;
    z-index: 10;
}
.gameboard.gameover.win .dimmer {
    background: rgba(255, 255, 255, 0.5);
}
.gameboard.gameover.fail .dimmer {
    background: rgba(0, 0, 0, 0.5);
}
.gameboard.gameover.fail .gameover-popup {
    text-align: center;
    background: #FF3D00;
    border: 1px solid #FFB69F;
}

.header {
    height: 80px;
    margin: 0 auto;
    position: relative;
}
    .header .score {
        min-width: 416px;
        height: 80px;
        position: absolute;
        left: 0;
        right: 0;
        top: -100%;
        transition: all .25s ease-in-out;
    }
    .header.visible .score {
        top: 0;
    }
    .header .score .score-option {
        padding-top: 10px;
        width: 33.333%;
        float: left;
        text-align: center;
    }
    .header .score .score-value {
        height: 2em;
        line-height: 125%;
        vertical-align: middle;
    }
        .header .score-option.increased {
            /*background: rgba(194,207,138, 0.3);*/
        }
            .header .score-option.increased .score-value {
                font-weight: bold;
                color: rgb(194,207,138);
                font-size: 125%;
            }
        .header .score-option.decreased {
            /*background: rgba(249,204,157, 0.3);*/
        }
            .header .score-option.decreased .score-value {
                font-weight: bold;
                color: rgb(249,204,157);
                font-size: 125%;
            }
        .header .score-option.alert {
            /*background: rgba(255,23,68, 0.2);*/
        }
            .header .score-option.fail .score-value {
                font-weight: bold;
                color: rgb(255,23,68);
            }
        .header .score-option.win {
            background: rgba(194,207,138, 0.3);
        }
            .header .score-option.win .score-value {
                font-weight: bold;
                color: rgb(194,207,138);
                font-size: 125%;
            }

.showroom {
    background: #eee;
    color: #999;
    padding: 0 40px;
    position: absolute;
    z-index: 100;
    bottom: -100%;
    left: 0;
    right: 0;
    transition: all .25s ease-in-out;
}
    .showroom.visible {
        bottom: 0;
    }
    .showroom .row {
        margin-top: 20px;
    }
    .showroom .image {
        float: left;
        width: 96px;
        height: 96px;
        padding: 2px;
        margin: 2px;
        font-size: 14px;
        text-align: center;
        border: 1px solid #aaa;
        border-radius: 3px;
        background: #fff;
        overflow: hidden;
    }
        .showroom .image img {
            width: 48px;
            height: 48px;
        }
    .showroom .title a,
    .showroom .image a {
        display: block;
        color: #333;
        text-decoration: none;
    }
    .showroom code {
        font-family: 'Source Code Pro', monospace;
    }
    .showroom pre {
        background: #333;
        padding: 10px;
        font-size: 14px;
        color: #f0f0f0;
        overflow: hidden;
    }
        .showroom pre a {
            color: #7FDDE9;
        }


@media (max-width: 480px) {
    body {
        font-size: 14px;
    }

    .gameboard {
        width: 224px;
        height: 224px;
    }

    .header .score-option > div {
        display: inline;
        line-height: 1;
        font-size: 1em;
    }

    .gameboard .card {
        width: 52px;
        height: 52px;
    }

    .card-frontside, .card-backside {
        width: 50px;
        height: 50px;
    }

    .card-backside {
        background-size: 50%;
    }

    .gameboard img {
        width: 48px;
        height: 48px;
    }

    .gameover-popup {
        margin-left: -60px;
        margin-top: -38px;
        width: 120px;
        height: 76px;
        line-height: 36px;
        border-radius: 2px;
    }

    .gameover-popup .ico {
        width: 24px;
        height: 24px;
    }
}
              
            
!

JS

              
                  /**
   * Shuffles array in place.
   * @param {Array} a items The array containing the items.
   */
  function shuffle(a){
    var j, x, i;
    for (i = a.length; i; i -= 1){
      j = Math.floor(Math.random() * i);
      x = a[i - 1];
      a[i - 1] = a[j];
      a[j] = x;
    }
  }

  /**
   *
   * @param images
   */
  function preload(images){
    for(i in images){
      var img = new Image();
      img.src = images[i];
    }
  }

  preload([
    'https://maxcdn.icons8.com/iOS7/PNG/100/Programming/help-100.png',
    'https://maxcdn.icons8.com/iOS7/PNG/100/Programming/help_filled-100.png',
    'https://maxcdn.icons8.com/Color/PNG/48/Baby/paper_windmill-48.png'
  ]);

  angular
    .module('game', [])
    .service('images', ApiIcons8Service)
    .controller('GameController', GameController)
  ;

  ApiIcons8Service.$inject = ['$http'];
  function ApiIcons8Service($http){
    // Unwrapping the icons images from the API response payload
    function handleSuccess(response){
      var x2js = new X2JS(),
          xmlData = response.data,
          jsonData = x2js.xml_str2json(xmlData),
          images = [],
          icons = jsonData.icons8.result.latest.platform.icon;

      angular.forEach(icons, function(icon){
        var image = {
          id: icon._id,
          name: icon._name,
          url: 'https://icons8.com' + icon._url,
          src: icon.png.png[2]._link
        };
        images.push(image);
      });
      return images;
    }

    return {
      getImages: function(count){
        return $http
          .get('https://api.icons8.com/api/iconsets/latest?platform=color&amount=' + count)
          .then(handleSuccess)
        ;
      }
    };
  }

  GameController.$inject = ['$scope', 'images', '$timeout', '$interval'];
  function GameController($scope, images, $timeout, $interval){
    $scope.images = [];
    $scope.randomImages = [];

    // stats
    $scope.time = 0;
    $scope.tries = 0;
    $scope.count = 8;
    $scope.matched = 0;
    $scope.unmatched = 0;

    // for animation
    $scope.gameStarted = false;
    $scope.gameFinished = false;
    $scope.timeIcreased = false;
    $scope.timeDecreased = false;
    $scope.triesIcreased = false;
    $scope.triesDecreased = false;
    $scope.win = false;
    $scope.fail = false;

    var _flippedImages = [],
      _unflipTimer = 0,
      _gameTimer = 0,
      _triesLimit = 12,
      _triesBonus = 4,
      _timeLimit = 24,
      _timeBonus = 5;

    $scope.iconsIds = function(){
      var ids = [];
      angular.forEach($scope.images, function(image){
        ids.push(image.id);
      });
      return ids.join(',')
    };

    $scope.newGame = function(){
      $scope.images = [];
      $scope.randomImages = [];
      $scope.time = _timeLimit;
      $scope.tries = _triesLimit;
      $scope.matched = 0;
      $scope.unmatched = 8;
      $scope.gameStarted = false;
      $scope.gameFinished = false;
      $scope.win = false;
      $scope.fail = false;

      _flippedImages = [];
      _unflipTimer = 0;
      _gameTimer = 0;

      images.getImages($scope.count * 6).then(function(images){
        shuffle(images);
        $scope.images = images.slice(0, $scope.count);
        $scope.randomImages = prepareImages($scope.images);
      });
    };

    $scope.newGame();

    $scope.flip = function(image){
      startGame();
      autoUnflip();

      if (!image.matched && image.flipped)
      {
        image.flipped = false;
        _flippedImages.push(image);
        checkMatches();
        changeTries(-1);

        if (_flippedImages.length == 2)
        {
          if (0 !== _unflipTimer)
            $timeout.cancel(_unflipTimer);
          _unflipTimer = $timeout(autoUnflip, 1000);
        }
      }
    };

    function unflip(images){
      if (0 !== _unflipTimer)
        $timeout.cancel(_unflipTimer);

      angular.forEach(images, function(image){
        if (!image.matched && !image.flipped)
          image.flipped = true;
      });
    }

    function autoUnflip(){
      if (_flippedImages.length > 1)
      {
        var images = _flippedImages.slice(0,2);
        _flippedImages = _flippedImages.slice(2,_flippedImages.length);
        unflip(images);
      }
    }

    function startGame(){
      if(!$scope.gameStarted)
      {
        $scope.gameStarted = true;
        _gameTimer = $interval(function(){
          changeTime(-1);
        }, 1000);
      }
    }

    function checkMatches(){
      if (_flippedImages.length<2)
        return;
      var img1 = _flippedImages[0],
        img2 = _flippedImages[1];
      if (img1.pair == img2.pair)
      {
        _flippedImages = _flippedImages.slice(2,_flippedImages.length);

        img1.matched = img2.matched = true;
        $scope.matched++;
        $scope.unmatched--;
        changeTries(_triesBonus);
        changeTime(_timeBonus);
      }
    }

    function changeTries(val){
      $scope.tries += val;

      if (val>0)
      {
        $scope.triesIcreased = true;
      } else
      {
        $scope.triesDecreased = true;
      }

      $timeout(function(){
        $scope.triesIcreased = false;
        $scope.triesDecreased = false;
      }, 500);

      checkGameOver();
    }

    function changeTime(val){
      $scope.time += val;

      if (val>0)
      {
        $scope.timeIcreased = true;
      } else
      {
        $scope.timeDecreased = true;
      }

      $timeout(function(){
        $scope.timeIcreased = false;
        $scope.timeDecreased = false;
      }, 500);

      checkGameOver();
    }

    function checkGameOver(){
      if ($scope.unmatched == 0)
      {
        $scope.gameFinished = true;
        $scope.win = true;
      } else if ($scope.time <= 0)
      {
        $scope.gameFinished = true;
        $scope.fail = true;
      } else if ($scope.tries <= 0)
      {
        $scope.gameFinished = true;
        $scope.fail = true;
      }

      if ($scope.gameFinished)
      {
        if (0 !== _gameTimer)
          $interval.cancel(_gameTimer);
        if (0 !== _unflipTimer)
          $timeout.cancel(_unflipTimer);
      }
    }

    function prepareImages(images){
      var preparedImages = [],
        pair = 0;
      angular.forEach(images, function(image){
        var imageClone1 = {}, imageClone2 = {};

        angular.copy(image, imageClone1);

        imageClone1.pair = pair++;
        imageClone1.matched = false;
        imageClone1.flipped = true;

        angular.copy(imageClone1, imageClone2);

        preparedImages.push(imageClone1);
        preparedImages.push(imageClone2);
      });

      shuffle(preparedImages);
      return preparedImages;
    }
  }
              
            
!
999px

Console