Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Sokoban game, Flash and Game development.

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

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

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.