A “very old” Flash Sokoban prototype
This is a Sokoban prototype I made in 2005 after reading some tutorials on gotoandplay.it.
It does not look that awesome and I am afraid the code sucks a bit, but I think it could be a good start for a project.
I plan to work on a Flash version of Sokoban someday, meanwhile you could have a look to my Javascript version.
Just in case you don’t know what is Sokoban…
From Wikipedia: Sokoban (warehouse keeper) is a transport puzzle in which the player pushes boxes around a maze, viewed from above, and tries to put them in designated locations. Only one box may be pushed at a time, not two, and boxes cannot be pulled. As the puzzle would be extremely difficult to create physically, it is usually implemented as a video game.
I won’t comment the code because it’s an old work, I will do it when I’ll create a new version.
Anyway it’s not that hard to understand, specially if you are an old time reader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | map = new Array();
diamond_map = new Array();
create_map();
draw_map();
_root.attachMovie("item", "sprite", 10000);
sprite._x = 40;
sprite._y = 20;
sprite.px = 2;
sprite.py = 1;
sprite.speed = 4;
sprite.steps = (20/sprite.speed);
sprite.moving = false;
sprite.count = 0;
function create_map() {
map[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1);
map[1] = new Array(1, 0, 0, 0, 0, 0, 0, 1);
map[2] = new Array(1, 0, 0, 0, 1, 0, 0, 1);
map[3] = new Array(1, 0, 2, 0, 0, 0, 0, 1);
map[4] = new Array(1, 0, 0, 0, 1, 2, 0, 1);
map[5] = new Array(1, 0, 0, 2, 0, 0, 0, 1);
map[6] = new Array(1, 0, 0, 0, 0, 0, 0, 1);
map[7] = new Array(1, 1, 1, 1, 1, 1, 1, 1);
}
function draw_map() {
pos = 0;
n_diamond = 0;
for (var y = 0; y<20; y++) {
for (var x = 0; x<20; x++) {
pos = x+(y*20);
map_value = map[y][x];
switch (map_value) {
case 0 :
_root.attachMovie("ground", "ground_"+pos, pos);
_root["ground_"+pos]._x = x*20;
_root["ground_"+pos]._y = y*20;
break;
case 1 :
_root.attachMovie("wall", "tile_"+pos, pos);
_root["tile_"+pos]._x = x*20;
_root["tile_"+pos]._y = y*20;
break;
case 2 :
_root.attachMovie("ground", "ground_"+pos, pos);
_root["ground_"+pos]._x = x*20;
_root["ground_"+pos]._y = y*20;
n_diamond++;
_root.attachMovie("diamond", "diamond_"+n_diamond, 10000+n_diamond);
_root["diamond_"+n_diamond]._x = x*20;
_root["diamond_"+n_diamond]._y = y*20;
diamond_map[y*20+x] = n_diamond;
// relative position of diamonds
break;
}
}
}
}
sprite.onEnterFrame = function() {
if (!this.moving) {
if (Key.isDown(Key.LEFT)) {
try_to_go = "left";
}
if (Key.isDown(Key.RIGHT)) {
try_to_go = "right";
}
if (Key.isDown(Key.UP)) {
try_to_go = "up";
}
if (Key.isDown(Key.DOWN)) {
try_to_go = "down";
}
switch (try_to_go) {
case "left" :
destination = map[this.py][this.px-1];
if (destination == 2) {
// if there is a diamond...
destination = map[this.py][this.px-2];
if (destination == 0) {
this.pushing_diamond = true;
this.diamond_to_push = diamond_map[this.py*20+this.px-1];
}
}
if (destination == 0) {
if (this.pushing_diamond) {
map[this.py][this.px-1] = 0;
map[this.py][this.px-2] = 2;
diamond_map[this.py*20+this.px-2] = diamond_map[this.py*20+this.px-1];
diamond_map[this.py*20+this.px-1] = 0;
}
this.px--;
dir = "left";
this.moving = true;
}
break;
case "right" :
destination = map[this.py][this.px+1];
if (destination == 2) {
// if there is a diamond...
destination = map[this.py][this.px+2];
if (destination == 0) {
this.pushing_diamond = true;
this.diamond_to_push = diamond_map[this.py*20+this.px+1];
}
}
if (destination == 0) {
if (this.pushing_diamond) {
map[this.py][this.px+1] = 0;
map[this.py][this.px+2] = 2;
diamond_map[this.py*20+this.px+2] = diamond_map[this.py*20+this.px+1];
diamond_map[this.py*20+this.px+1] = 0;
}
this.px++;
dir = "right";
this.moving = true;
}
break;
case "up" :
destination = map[this.py-1][this.px];
if (destination == 2) {
// if there is a diamond...
destination = map[this.py-2][this.px];
if (destination == 0) {
this.pushing_diamond = true;
this.diamond_to_push = diamond_map[(this.py-1)*20+this.px];
}
}
if (destination == 0) {
if (this.pushing_diamond) {
map[this.py-1][this.px] = 0;
map[this.py-2][this.px] = 2;
diamond_map[(this.py-2)*20+this.px] = diamond_map[(this.py-1)*20+this.px];
diamond_map[(this.py-1)*20+this.px] = 0;
}
this.py--;
dir = "up";
this.moving = true;
}
break;
case "down" :
destination = map[this.py+1][this.px];
if (destination == 2) {
// if there is a diamond...
destination = map[this.py+2][this.px];
if (destination == 0) {
this.pushing_diamond = true;
this.diamond_to_push = diamond_map[(this.py+1)*20+this.px];
}
}
if (destination == 0) {
if (this.pushing_diamond) {
map[this.py+1][this.px] = 0;
map[this.py+2][this.px] = 2;
diamond_map[(this.py+2)*20+this.px] = diamond_map[(this.py+1)*20+this.px];
diamond_map[(this.py+1)*20+this.px] = 0;
}
this.py++;
dir = "down";
this.moving = true;
}
break;
}
try_to_go = "";
// dont want sprite to keep on moving
}
if (this.moving) {
this.moving = true;
if (this.count>=this.steps) {
this.moving = false;
if (this.pushing_diamond) {
this.pushing_diamond = false;
}
this.count = 0;
} else {
switch (dir) {
case "left" :
this._x -= this.speed;
if (this.pushing_diamond) {
_root["diamond_"+this.diamond_to_push]._x -= this.speed;
}
break;
case "right" :
this._x += this.speed;
if (this.pushing_diamond) {
_root["diamond_"+this.diamond_to_push]._x += this.speed;
}
break;
case "up" :
this._y -= this.speed;
if (this.pushing_diamond) {
_root["diamond_"+this.diamond_to_push]._y -= this.speed;
}
break;
case "down" :
this._y += this.speed;
if (this.pushing_diamond) {
_root["diamond_"+this.diamond_to_push]._y += this.speed;
}
break;
}
this.count++;
}
}
}; |
Play the game with arrow keys…
… Then download the source and suggest something
They can be easily customized to meet the unique requirements of your project.

























This post has 7 comments
Maciek
lol nice game;]
Maciek
I really would like to see a soft body engine tho;]
Eblup
can you make some AI or a A* pathfinding tut plz. it would really bring a lot of support back.
shiv1411
hi!
as usual gr8 and incomplete. I think you should publish all parts of a particular tut in one go. Anyways gr8 tut!
shiv1411
hey Emanuele,
Can I suggest you something?
Would you make a game like cricket or football in Flash. I know its hard, but u r a genius!
Try using cute graphics as well.
Thomas
Woohoo!!!! I don’t know why, but I always end up on the end of my seat thinking “Oh man! How’s he gonna code this function!”
And then it’s solved and I’m all happy!
hans heidmann
hey Emanuele this is awesome, have you ever played the windows 95 bulldozer game thats included in the 24 games for windows 95 by expert games? I am really interested in making a really nice and classic sokoban game like that so please make that sokoban tutorial you were talking about on the top of this page :)