Talking about Sokoban game, Actionscript 3, Flash, Game development and PROgramming.
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...
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.