Shuffle an image with BitmapData – Part 3

My tribute (learn how I am calling a clone) to Split Personalities is growing fast and well, and after the first and second steps, now I have something playable.

The aim of the game is to rebuild the image of a stolen painting(in this case the Scream) that was broken into pieces (I just want you to notice that I also made a dramatic background…).

The new features I added are a minimap on the right representing a blueprint of the original painting and the “holes”… some trapdoors that open and close randomly. When a piece of the picture falls into an opened hole, it disappears. When a piece of the picture steps into a closed hole, the hole remains closed until the piece is moved.

It’s a very simple concept but I messed the instructions how to play… I’ll never get a job as a game manual writer… you better check the demo by yourself

Play the demo.

Now I am planning to add **tons** of features and powerups, about 10 levels of increasing difficulty, fancy graphics and release the game

Wanna sponsor the game?

I’ll add MochiAds in the game. However, I also would like to sell four 125×125 buttons in the game. Not in the “splash screen”, I mean into the game itself.

I am selling buttons instead of looking for a sponsor because I am publishing the source code of the game, and sponsors do not want people to see the game in progress or even worse to see the code before the game is finished

If you want to purchase a button (that will link to wherever you want), the price is $50 per button.

Maybe the game will be a flop, maybe not. Anyway, it’s up to you to risk your 50 bucks. The guiderules are the same of every link sale: no heavy images or heavy animations in the buttons, no links to gambling, porno, racist sites and so on.

You can then resell your button how many times you want until the game is ready. When it’s ready, maybe some millions people will see your button while PLAYING (not WAITING for the game to load).

If you want to purchase a button, contact me at info[at]emanueleferonato.com

That’s a very unseen way to ask for a sponsorship but… hey… I am running an experiment! Bet on my game!

Now, the source code… still uncommented but with understandable variable names. You know that once finished I’ll release the complete tutorial as usual.

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
Array.prototype.shuffle = function() {
	for (x=0; x<19; x++) {
		var from = Math.floor(Math.random()*20);
		var to = this[x];
		this[x] = this[from];
		this[from] = to;
	}
};
big_picture = BitmapData.loadBitmap("scream");
_root.createEmptyMovieClip("big_pic_obj", _root.getNextHighestDepth());
big_pic_obj.attachBitmap(big_picture, _root.getNextHighestDepth());
big_pic_obj._visible = false;
sequence = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
save_position = 0;
sequence.shuffle();
field = new Array();
holes = new Array();
for (x=0; x<25; x++) {
	holes[x] = 0;
	field[x] = -1;
}
for (x=1; x<=3; x++) {
	do {
		out = Math.floor(Math.random()*24)+1;
	} while ((holes[out] != 0) or (out<5));
	holes[out] = x;
	hole = _root.attachMovie("out", "out_"+x, _root.getNextHighestDepth(), {_x:(out%5)*80, _y:Math.floor(out/5)*80});
	hole.pos = x;
	_root.attachMovie("glass", "glass_"+x, _root.getNextHighestDepth(), {_x:(out%5)*80, _y:Math.floor(out/5)*80});
	hole.onEnterFrame = function() {
		moving = Math.floor(Math.random()*250);
		if (moving == 0) {
			_root["glass_"+this.pos]._alpha = 100-_root["glass_"+this.pos]._alpha;
		}
	};
}
_root.createEmptyMovieClip("tiles", _root.getNextHighestDepth());
_root.attachMovie("mini", "mini", _root.getNextHighestDepth(), {_x:410});
_root.attachMovie("magnifier", "magnifier", _root.getNextHighestDepth());
_root.attachMovie("fire", "fire", _root.getNextHighestDepth());
_root.attachMovie("border", "border", _root.getNextHighestDepth());
border.onEnterFrame = function() {
	magnifier._visible = false;
	this.gotoAndStop(1);
	if (!dragging) {
		possible_direction = "";
		if (_root._xmouse<400) {
			this._x = Math.floor(_root._xmouse/80)*80;
			this._y = Math.floor(_root._ymouse/80)*80;
			position = Math.floor(_root._xmouse/80)+5*Math.floor(_root._ymouse/80);
			if (field[position]>=0) {
				magnifier._x = 410+(field[position]%5)*40;
				magnifier._y = Math.floor(field[position]/5)*40;
				magnifier._visible = true;
			}
		}
	} else {
		dist_x = _root._xmouse-save_x;
		dist_y = _root._ymouse-save_y;
		angle = Math.atan2(dist_y, dist_x)*180/Math.PI;
		if (angle<45 and angle>-45) {
			this.gotoAndStop(3);
			possible_direction = "right";
		}
		if ((angle<-45) and (angle>-135)) {
			this.gotoAndStop(2);
			possible_direction = "up";
		}
		if (angle<-135 or angle>135) {
			this.gotoAndStop(5);
			possible_direction = "left";
		}
		if (angle<135 and angle>45) {
			this.gotoAndStop(4);
			possible_direction = "down";
		}
	}
};
for (x=0; x<20; x++) {
	picture = tiles.createEmptyMovieClip("small_pic_obj_"+x, tiles.getNextHighestDepth());
	small_picture = new BitmapData(80, 80);
	picture.attachBitmap(small_picture, tiles.getNextHighestDepth());
	small_picture.copyPixels(big_picture, new Rectangle(0+(x%5)*80, 0+Math.floor(x/5)*80, 80, 80), new Point(0, 0));
	picture.id = x;
	picture.falling = false;
	picture.onEnterFrame = function() {
		if (!this.falling) {
			if (this.dir == "right") {
				position = Math.floor(this._x/80)+5*Math.floor(this._y/80);
				if ((position%5 != 4) and (field[position+1] == -1)) {
					this._x += 80;
					field[position] = -1;
					field[position+1] = this.id;
				} else {
					this.dir = "";
				}
			}
			if (this.dir == "down") {
				position = Math.floor(this._x/80)+5*Math.floor(this._y/80);
				if ((position+5<25) and (field[position+5] == -1)) {
					this._y += 80;
					field[position] = -1;
					field[position+5] = this.id;
				} else {
					this.dir = "";
				}
			}
			if (this.dir == "left") {
				position = Math.floor(this._x/80)+5*Math.floor(this._y/80);
				if ((position%5 != 0) and (position != 1) and (field[position-1] == -1)) {
					this._x -= 80;
					field[position] = -1;
					field[position-1] = this.id;
				} else {
					this.dir = "";
				}
			}
			if (this.dir == "up") {
				position = Math.floor(this._x/80)+5*Math.floor(this._y/80);
				if ((position-5>0) and (field[position-5] == -1)) {
					this._y -= 80;
					field[position] = -1;
					field[position-5] = this.id;
				} else {
					this.dir = "";
				}
			}
			position = Math.floor(this._x/80)+5*Math.floor(this._y/80);
			if ((holes[position] != 0) and (_root["glass_"+holes[position]]._alpha == 0) and (this.dir != "")) {
				this.dir = "";
				this.falling = true;
				save_position = position;
			}
		}
		if (this.falling) {
			this._width -= 2;
			this._height -= 2;
			this._x++;
			this._y++;
			this._alpha -= 2;
			if (this._width == 0) {
				this._alpha++;
				this._width = 80;
				this._height = 80;
				sequence.push(field[save_position]);
				field[save_position] = -1;
				this._x = 0;
				this._y = 0;
				this.falling = false;
			}
		}
	};
}
_root.onMouseDown = function() {
	position = Math.floor(_root._xmouse/80)+5*Math.floor(_root._ymouse/80);
	if (position == 0) {
		if (field[1] == -1) {
			picture_in = sequence.shift();
			tiles["small_pic_obj_"+picture_in].dir = "right";
		}
	} else {
		if (field[position] != -1) {
			dragging = true;
			save_x = border._x+50;
			save_y = border._y+50;
		}
	}
};
_root.onMouseUp = function() {
	position = Math.floor(save_x/80)+5*Math.floor(save_y/80);
	tiles["small_pic_obj_"+field[position]].dir = possible_direction;
	dragging = false;
};

176 lines… and I already have a level… give me feedback!

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