Pandaban – Build HTML5 Sokoban game with Panda2 and play it with keyboard or by swiping

Read all posts about "" game
Did you try Panda2 Game Engine? It’s a HTML5 game engine which comes with a great IDE, I already showed you how to build a Tipsy Tower prototype with it and now I just built a complete Sokoban prototype with it. You should know I am a Sokoban fan, you can find plenty of tutorials in different programming languages showing how to build a Sokoban game, and now it’s time to show you the Panda2 prototype. Look at the IDE in action: And now, the game itself:
You should know the rules, control the character with ARROW keys or by swiping. In the source code, still uncommented, you can find a lot of useful features, such as: * mouse input detection * keyboard detection * sprite sheets * tweens And I also improved it a bit making it less redundant compared to my previous versions, have a look:
game.module(
    "game.main"
)
.body(function() {
    
    var gameOptions = {
        tileSize: 40,
        animationSpeed: 200
    }
    
    var level = [
        [1,1,1,1,1,1,1,1],
        [1,0,0,1,1,1,1,1],
        [1,0,0,1,1,1,1,1],
        [1,0,0,0,0,0,0,1],
        [1,1,4,2,1,3,0,1],
        [1,0,0,0,1,0,0,1],
        [1,0,0,0,1,1,1,1],
        [1,1,1,1,1,1,1,1]
    ]
    
    var EMPTY = 0;
    var WALL = 1;
    var SPOT = 2;
    var CRATE = 3;
    var PLAYER = 4;
    
    game.addAsset("tiles.png");

    game.createScene("Main", {
        
        init: function() {
            this.canMove = true;
            this.drawLevel();
        },
        
        drawLevel: function(){
            this.sheet = new game.SpriteSheet("tiles.png", gameOptions.tileSize, gameOptions.tileSize);
            this.crates = [];
            this.crates.length = 0;
            for (var i = 0; i < level.length; i++) {
                this.crates[i] = [];
                for (var j = 0; j < level[i].length; j++) {
                    this.crates[i][j] = null;
                    var item = level[i][j];
                    switch(item){
                        case PLAYER:
                        case PLAYER + SPOT:
                            this.player = this.sheet.frame(item);
                            this.player.position.set(j * gameOptions.tileSize, i * gameOptions.tileSize);
                            this.player.addTo(this.stage);
                            this.player.zIndex = 1;
                            this.player.posX = j;
                            this.player.posY = i;
                            var tile = this.sheet.frame(item - PLAYER);
                            tile.position.set(j * gameOptions.tileSize, i * gameOptions.tileSize);
                            tile.addTo(this.stage);
                            tile.zIndex = 0;
                            break;
                        case CRATE:
                        case CRATE + SPOT:
                            this.crates[i][j] = this.sheet.frame(item);
                            this.crates[i][j].position.set(j * gameOptions.tileSize, i * gameOptions.tileSize);
                            this.crates[i][j].addTo(this.stage);
                            this.crates[i][j].zIndex = 1;
                            var tile = this.sheet.frame(item - CRATE);
                            tile.position.set(j * gameOptions.tileSize, i * gameOptions.tileSize);
                            tile.addTo(this.stage);
                            tile.zIndex = 0;
                            break;
                        default:
                            var tile = this.sheet.frame(item);
                            tile.position.set(j * gameOptions.tileSize, i * gameOptions.tileSize);
                            tile.addTo(this.stage);
                            tile.zIndex = 0;
                            
                    }
                }
            }
            game.scene.stage.children.sort(this.depthCompare);
        },
        
        swipe: function(d){
            switch(d){
                case "RIGHT":
                    this.checkMove(1, 0);
                    break;
                case "LEFT":
                    this.checkMove(-1, 0);
                    break;
                case "UP":
                    this.checkMove(0, -1);
                    break;
                case "DOWN":
                    this.checkMove(0, 1);
                    break;
            }
        },
        
        checkMove: function(deltaX, deltaY){
            if(this.canMove){
                if(this.isWalkable(this.player.posX + deltaX, this.player.posY + deltaY)){
                    this.movePlayer(deltaX, deltaY, false);
                    return;
                }
                if(this.isCrate(this.player.posX + deltaX, this.player.posY + deltaY)){
                    if(this.isWalkable(this.player.posX + 2 * deltaX, this.player.posY + 2 * deltaY)){
                        this.movePlayer(deltaX, deltaY, true);
                        return;
                    }
                }    
            }
        },
        
        isWalkable: function(posX, posY){
            return level[posY][posX] == EMPTY || level[posY][posX] == SPOT;
        },
        
        isCrate: function(posX, posY){
            return level[posY][posX] == CRATE || level[posY][posX] == CRATE + SPOT;
        },
        
        movePlayer: function(deltaX, deltaY, pushCrate){
            this.canMove = false;
            var playerTween = new game.Tween(this.player.position);
            playerTween.to({
                x: this.player.x + deltaX * gameOptions.tileSize,
                y: this.player.y + deltaY * gameOptions.tileSize
            }, gameOptions.animationSpeed);
            playerTween.start();
            playerTween.onComplete(function() {
                level[this.player.posY][this.player.posX] -= PLAYER;
                this.player.posX += deltaX;
                this.player.posY += deltaY;
                level[this.player.posY][this.player.posX] += PLAYER;
                if(pushCrate){
                    this.crates[this.player.posY + deltaY][this.player.posX + deltaX] = this.crates[this.player.posY][this.player.posX];
                    this.crates[this.player.posY][this.player.posX] = null;
                    level[this.player.posY][this.player.posX] -= CRATE;
                    level[this.player.posY + deltaY][this.player.posX + deltaX] += CRATE;
                    this.crates[this.player.posY + deltaY][this.player.posX + deltaX].texture = this.sheet.textures[level[this.player.posY + deltaY][this.player.posX + deltaX]];
                }
                this.player.texture = this.sheet.textures[level[this.player.posY][this.player.posX]];
                this.canMove = true;
            }.bind(this));
            if(pushCrate){
                var crateTween = new game.Tween(this.crates[this.player.posY + deltaY][this.player.posX + deltaX].position);
                crateTween.to({
                    x: this.crates[this.player.posY + deltaY][this.player.posX + deltaX].x + deltaX * gameOptions.tileSize,
                    y: this.crates[this.player.posY + deltaY][this.player.posX + deltaX].y + deltaY * gameOptions.tileSize
                }, gameOptions.animationSpeed);
                crateTween.start();    
            }
    	},
        
        depthCompare: function (a, b){
            if (a.zIndex < b.zIndex) return -1;
            if (a.zIndex > b.zIndex) return 1;
            return 0;
        },
        
        update: function() {
            if (game.keyboard.down("LEFT")){
                this.checkMove(-1, 0);
            }
            if (game.keyboard.down("RIGHT")){
                this.checkMove(1, 0);
            }
            if (game.keyboard.down("UP")){
                this.checkMove(0, -1);
            }
            if (game.keyboard.down("DOWN")){
                this.checkMove(0, 1);
            }
        }
        
    });

});

Give a try to Panda2, you will have a lot of fun with it, and download the source code of the complete project.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers