Complete Flash Sokoban game in less than 2KB

Read all posts about "" game

Some days ago I blogged about 6 games you must be able to make in less than a day.

Among these games I included Sokoban, and since I don’t remember if I’ve ever tried to make a complete Sokoban game, I did it today.

In less than a day and, above all, in less than 2KB!!

And don’t expect an incomplete prototype… my Sokoban game features:

* 10 levels
* Tiles of different color and shape
* Shared objects to save the games
* Eye-candy effects (being only 2KB!!) to show completed levels and current level
* Copyable level moves to publish your own level solution
* In game instructions
* No external files or other tricks to reduce file size.

As said, in less than 2KB. My latest build is 2,002 bytes that’s less than 2,048.

This is the source code, that wasn’t written with readability or performance in mind, just aiming to stay under 2KB… just an exercise:

package {
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.net.SharedObject;
	public class s extends Sprite {
		var m:Array=[[[1,1,1,1],[1,0,0,1,1,1,1,1],[1,0,2,0,0,3,0,1],[1,0,3,0,0,2,4,1],[1,1,1,0,0,1,1,1],[0,0,1,1,1,1]],[[0,0,1,1,1,1],[0,0,1,0,0,1],[1,1,1,0,0,1,1,1],[1,0,3,5,2,4,0,1],[1,0,0,0,0,0,0,1],[1,1,1,1,1,1,1,1]],[[0,0,1,1,1,1],[1,1,1,0,0,1],[1,0,0,5,4,1],[1,0,0,3,2,1,1],[1,1,1,0,0,0,1],[0,0,1,0,0,0,1],[0,0,1,1,1,1,1]],[[0,1,1,1,1,1,1,1],[1,1,0,4,0,0,0,1],[1,0,3,2,0,0,0,1],[1,0,2,3,1,1,1,1],[1,1,0,0,1],[0,1,0,0,1],[0,1,1,1,1,0]],[[1,1,1,1,1,1,1],[1,0,0,0,2,4,1,1,1],[1,0,0,5,5,5,0,0,1],[1,0,0,0,3,0,0,0,1],[1,1,1,1,1,1,0,0,1],[0,0,0,0,0,1,1,1,1]],[[0,0,1,1,1,1],[0,0,1,0,0,1],[0,0,1,0,0,1],[1,1,1,0,4,1,1],[1,0,5,3,2,0,1],[1,0,0,0,0,0,1],[1,1,0,0,1,1,1],[0,1,1,1,1,0,0]],[[0,0,1,1,1,1],[0,0,1,0,0,1,1,1],[1,1,1,2,3,2,0,1],[1,0,0,3,2,3,0,1],[1,0,0,0,4,1,1,1],[1,0,0,0,0,1],[1,1,1,1,1,1]],[[1,1,1,1],[1,0,0,1],[1,0,4,1],[1,2,3,1,1,1],[1,0,0,0,0,1],[1,2,3,1,0,1],[1,1,0,0,0,1],[0,1,1,1,1,1]],[[1,1,1,1,1,1,1],[1,0,0,1,0,0,1],[1,2,3,1,0,0,1],[1,6,3,0,0,0,1],[1,2,3,0,0,0,1],[1,1,1,1,1,1,1]],[[1,1,1,1,1,1,1,1],[1,0,0,1,0,0,0,1],[1,0,0,0,0,0,0,1],[1,3,3,3,1,1,1,1],[1,6,2,0,2,1],[1,1,1,1,1,1]]];
		public var l,h:int=0;
		public var p,r:Array;
		public var f:Sprite=new Sprite();
		public var g,o:Sprite;
		public var u:TextField = new TextField();
		public var c:SharedObject=SharedObject.getLocal("s");
		public function s():void {
			if (c.data.l==undefined) {
				c.data.l="0000000000";
			}
			for (var i:int=0; i<10; i++) {
				g=new Sprite();
				g.graphics.beginFill(0x00ff00);
				g.alpha=0.2+0.8*c.data.l.charAt(i);
				g.graphics.drawRect(0,0,9,9);
				g.x=130+i*11;
				g.y=35;
				g.name="g"+i;
				addChild(g);
			}
			o = new Sprite();
			o.graphics.beginFill(0xff0000);
			o.graphics.drawRect(0,0,3,3);
			addChild(o);
			o.y=38;
			u.text="ARROWS KEYS: MOVE\n0-9: SELECT LEVEL\nCOMPLETED LEVELS:\nLEVEL MOVES: ";
			u.width=490;
			addChild(u);
			addChild(f);
			d(l);
			stage.addEventListener(KeyboardEvent.KEY_UP, k);
		}
		public function d(l:int):void {
			o.x=133+l*11;
			var n:int;
			r=new Array();
			for (var i:int=0; i47&&h<58) {
						l=h-48;
						removeChild(f);
						f = new Sprite();
						addChild(f);
						d(l);
					}
			}
		}
		public function w(a:int,b:int):void {
			var v:Boolean=false;
			var _z:String="RULD";
			var d:String="";
			if (r[p[0]+b][p[1]+a]==0||r[p[0]+b][p[1]+a]==2) {
				v=true;
			} else {
				if ((r[p[0]+b][p[1]+a]==3||r[p[0]+b][p[1]+a]==5)&&(r[p[0]+2*b][p[1]+2*a]==0||r[p[0]+2*b][p[1]+2*a]==2)) {
					d=f.getChildByName("d"+(p[0]+b)+"_"+(p[1]+a)).name;
					v=true;
				}
			}
			if (v) {
				u.appendText(_z.charAt(h-37));
				f.getChildByName("b").x+=32*a;
				f.getChildByName("b").y+=32*b;
				p[0]+=b;
				p[1]+=a;
				if (d!="") {
					f.getChildByName(d).x+=32*a;
					f.getChildByName(d).y+=32*b;
					f.getChildByName(d).name="d"+(p[0]+1*b)+"_"+(p[1]+1*a);
					r[p[0]][p[1]]-=3;
					r[p[0]+b][p[1]+a]+=3;
					d="";
					if (r.toString().indexOf(2)==-1) {
						c.data.l=c.data.l.substr(0,l)+"1"+c.data.l.substr(l+1);
						getChildByName("g"+l).alpha=1;
					}
				}
			}
		}
	}
}

This class was compiled with "Compress movie" checked and "Include XMP metadata" unchecked

And this is the result:

Move with arrow keys, select levels with 0-9, leave a comment with your solutions... and try to give me an idea about another game to make it fit under 2KB.

If you want to check file size, save this file and see...

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

214 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
// 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