A “very old” Flash Sokoban prototype

Read all posts about "" game

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

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