cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <h1>Little Game</h1>
<p>Click on a circle to rotate it; connect the pipes and watch everything move.</p>
<div class="center">
  <ul id="board"></ul>
  <div><span>Points: </span><span id="point"></span></div>
  <a id="fast" href="#">Fast mode</a>
  <a id="normal" href="#">Normal mode</a>
  <div class="right">
    <a href="https://twitter.com/share" id="tweet">Tweet Score</a>
  </div>
  <footer>
   <p>Everything is rendered in HTML + CSS, no Canvas used. </p> 
  </footer>
</div>

            
          
!
            
              
ul{
  display:block;
  width:500px;
  height:500px;
  list-style:none;
  
  transform:translateZ(0);
}

li{
  position:relative;
  
  display:block;
  float:left;
  
  background:rgba(60,60,60,1);
  
  width:50px;
  height:50px;
  
  border-radius:100%;
    
  overflow:hidden;
}

li:before{
  position:absolute;
  content:'';
  
  width:50px;
  height:50px;
  
  top:22px;
  left:22px;
  border:5px solid white;
  width:inherit;
  height:inherit;
  border-radius:40%;
  
  box-sizing:border-box;
}

[data-state='1'], [data-state='2'], [data-state='3'], [data-state='4'], li{
    animation:turn .5s forwards;
}
[data-state='2']{
    animation-name:turn1;
}
[data-state='3']{
    animation-name:turn2;
}
[data-state='4']{
    animation-name:turn3;
}
li{
    animation-name:turn3;
}

@keyframes turn{
  0%{
    transform:rotate(0deg);
  }
  100%{
    transform:rotate(90deg);
  }
}
@keyframes turn1{
  0%{
    transform:rotate(90deg);
  }
  100%{
    transform:rotate(180deg);
  }
}
@keyframes turn2{
  0%{
    transform:rotate(180deg);
  }
  100%{
    transform:rotate(270deg);
  }
}
@keyframes turn3{
  0%{
    transform:rotate(270deg);
  }
  100%{
    transform:rotate(360deg);
  }
}

/* Boring */
body{
  font-family:"Helvetica Neue", sans-serif;
}
h1, p{
  text-align:center;
}
h1{
  font-size:2em;
}
.center{
  margin:0 auto;
  width:600px;
}
.right{
  float:right;
}

footer{
  font-size:.8em;
}
            
          
!
            
              var game = {
  points : 0,
  delay  : 510,
  init : function(node, point, tweet, w, h){
    this.map = new Array(w*h);
    
    this.node = node;
    this.point = point;
    this.tweet = tweet;
    
    this.w = w;
    this.h = h;
    
    
    this.node.addEventListener('click', function(e){
      var element = e.srcElement || e.target;
      game.points = 0;
      game.turn(element.dataPos, true);
    }, true); 
       
    this._initNode(); 
  },
    
  turn : function(pos, start){   
    var item = this.map[pos];
    
    if (start === true){
       game.chain(pos, item)
     }else{
      setTimeout(function(){
        game.chain(pos, item);
      }, game.delay); // This have to be in sync with the turn animation .5s
     }
  },
  
  chain : function(pos, item){
    if (item.animationRunning) return; // Only do thing when it's not currently animating. 
                                       // Fixing bugs where strange things can happen
    
    var state = item.state, 
        nextItem, 
        nextPos, 
        allowed;
    
    state = ( item.state + 1 ) % 4;
    
    if (state === 0 || state === 1){
      this.startNext(pos+game.w, [2, 3])
    }
    if (state === 0 || state === 3){
      this.startNext(pos+1, [1,2])
    }
    if (state === 1 || state === 2){
      this.startNext(pos-1, [0,3])
    }
    if (state === 2 || state === 3){
      this.startNext(pos-game.w, [0,1])
    }
    
    item.state = state;
    item.animationRunning = true;
    item.li.setAttribute('data-state', state);
    
    // Just want to make this working. 
    // here i should use the animationend event! 
    setTimeout(function(){item.animationRunning = false;}, 500)
      
    game.addPoints();
  },
  
  startNext : function(nextPos, allowed){
    var nextItem = game.map[nextPos];    
    if (nextItem !== undefined && allowed.indexOf(nextItem.state) !== -1){
      game.turn(nextPos);
    }
  },
  
  addPoints : function(){ 
    game.points++;
    game.tweet.setAttribute('href', "https://twitter.com/intent/tweet?&text="+encodeURIComponent("I scored '"+game.points+"' in this Little Game http://cdpn.io/JjBhk"))
    game.point.innerHTML = game.points;
  },
  
  _initNode : function(){
    var frag = document.createDocumentFragment(),
        b = 0, map = this.map;
    for (var i=0;i<map.length;i++){
      var state = Math.floor(Math.random()*4),
          li   = document.createElement('li'); 
      
          li.setAttribute('data-state', state);
          li.dataPos = i;
      
      map[i] = {state : state, li : li};
      frag.appendChild(li);
    }
    this.node.innerHTML = "";
    this.node.appendChild(frag);
  }
}

var board = document.getElementById('board'),
    point =  document.getElementById('point'),
    tweet =  document.getElementById('tweet');
game.init(board, point, tweet, 10, 10);



var fast =  document.getElementById('fast');
fast.addEventListener('click', function(e){
  game.delay = 0;
  e.preventDefault();
});
var normal = document.getElementById('normal');
normal.addEventListener('click', function(e){
  game.delay = 510;
  e.preventDefault();
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console