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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <html>
  <head>
    <title>12puzzle</title>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script>
  </head>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create,render: render });

var PIECE_WIDTH = 200,
    PIECE_HEIGHT = 200,
    BOARD_COLS,
    BOARD_ROWS;
var piecesIndex = 0;
var title;
var puzzle;
var ent = true;
var timer;
var total = 0;
var cursors;
var piecesGroup,
    piecesAmount,
    shuffledIndexArray = [];
var land;

function preload() {
    game.load.spritesheet("background", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1579145/ryukoku2.jpg", PIECE_WIDTH, PIECE_HEIGHT);
    
    game.load.image("mihon","https://s3-us-west-2.amazonaws.com/s.cdpn.io/1579145/ryukoku2.jpg");

} 
     
function create() {
  
  land = game.add.sprite(0,0,'mihon');
  puzzle = game.add.text(game.world.centerX,
  200, '12puzzle!',{font: "40px Arial", fill: "#ffffff", align: "center"});
  puzzle.anchor.setTo(0.5,0.5);
  title = game.add.text(game.world.centerX,
  400,'- click to start -',{font: "40px Arial", fill: "#ffffff", align: "center"});
  game.input.onDown.add(kiri);
  title.anchor.setTo(0.5,0.5);
 
  timer = game.time.create(false);
     
  timer.loop(1000,updateCounter,this);
  
}
     
function kiri()
{
  if(ent){
  prepareBoard();
  ent = false;
  timer.start();
  title.visible = false;
  puzzle.visible = false;
  land.kill();
  }
  
  
}
function updateCounter(){
         
  total++;
}

function render(){
  
  game.debug.text('time: ' + total,32,64); 
  
}

function prepareBoard() {

    var i, j,piecesIndex =0,
        piece;
 
      BOARD_COLS = Math.floor(game.world.width / PIECE_WIDTH);
      BOARD_ROWS = Math.floor(game.world.height / PIECE_HEIGHT);

      piecesAmount = BOARD_COLS * BOARD_ROWS;

      shuffledIndexArray = createShuffledIndexArray();

      piecesGroup = game.add.group();

      for (i = 0; i < BOARD_ROWS; i++)
     {
          for (j = 0; j < BOARD_COLS; j++)
          {
              if (shuffledIndexArray[piecesIndex]) {
                  piece = piecesGroup.create(j * PIECE_WIDTH, i * PIECE_HEIGHT, "background", shuffledIndexArray[piecesIndex]);
              }
              else { 
                  piece = piecesGroup.create(j * PIECE_WIDTH, i * PIECE_HEIGHT);
                  piece.black = true;
              }
              piece.name = 'piece' + i.toString() + 'x' + j.toString();
              piece.currentIndex = piecesIndex;
              piece.destIndex = shuffledIndexArray[piecesIndex];
              piece.inputEnabled = true;
              piece.events.onInputDown.add(selectPiece, this);
              piece.posX = j;
              piece.posY = i;
              piecesIndex++;
          }
      }
  
}

function selectPiece(piece) {

    var blackPiece = canMove(piece);

    
    if (blackPiece) {
        movePiece(piece, blackPiece);
    }

}

function canMove(piece) {

    var foundBlackElem = false;

    piecesGroup.children.forEach(function(element) {
        if (element.posX === (piece.posX - 1) && element.posY === piece.posY && element.black ||
            element.posX === (piece.posX + 1) && element.posY === piece.posY && element.black ||
            element.posY === (piece.posY - 1) && element.posX === piece.posX && element.black ||
            element.posY === (piece.posY + 1) && element.posX === piece.posX && element.black) {
            foundBlackElem = element;
            return;
        }
    });

    return foundBlackElem;
}

function movePiece(piece, blackPiece) {

    var tmpPiece = {
        posX: piece.posX,
        posY: piece.posY,
        currentIndex: piece.currentIndex
    };

    game.add.tween(piece).to({x: blackPiece.posX * PIECE_WIDTH, y: blackPiece.posY * PIECE_HEIGHT}, 300, Phaser.Easing.Linear.None, true);

    
    piece.posX = blackPiece.posX;
    piece.posY = blackPiece.posY;
    piece.currentIndex = blackPiece.currentIndex;
    piece.name ='piece' + piece.posX.toString() + 'x' + piece.posY.toString();

   
    blackPiece.posX = tmpPiece.posX;
    blackPiece.posY = tmpPiece.posY;
    blackPiece.currentIndex = tmpPiece.currentIndex;
    blackPiece.name ='piece' + blackPiece.posX.toString() + 'x' + blackPiece.posY.toString();

    
    checkIfFinished();
}

function checkIfFinished() {

    var isFinished = true;

    piecesGroup.children.forEach(function(element) {
        if (element.currentIndex !== element.destIndex) {
            isFinished = false;
            return;
        }
    });

    if (isFinished) {
        showFinishedText();
    }

}

function showFinishedText() {
 
    var style = { font: "40px Arial", fill: "#000", align: "center"};

    var text = game.add.text(game.world.centerX, game.world.centerY, "Congratulations!", style);

    text.anchor.set(0.5);
  
    timer.stop();
}

function createShuffledIndexArray() {

    var indexArray = [];

    for (var i = 0; i < piecesAmount; i++)
    {
        indexArray.push(i);
    }

    return shuffle(indexArray);

}

function shuffle(array) {

    var counter = array.length,
        temp,
        index;

    while (counter > 0)
    {
        index = Math.floor(Math.random() * counter);

        counter--;

        temp = array[counter];
        array[counter] = array[index];
        array[index] = temp;
    }

    return array;
    
}

              
            
!
999px

Console