Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Perfectionism game, Flash and Game development.

This is the last step.

And the end of this one, we’ll have our complete Perfectionism game

The score

You get one point every time you move a square into a ring, and you lose one point every time you move a square out of a ring

To show the score, I just created a new movieclip with a dynamic text area into it just like in the picture

Perfectionism

As you can see, the movieclip is called and linked as score while the text area is instanced as scoretxt

Now, the actionscript:

_root.createEmptyMovieClip("rays",2);
_root.attachMovie("next_level","next_level",3,{_x:400, _y:450});
_root.attachMovie("prev_level","prev_level",4,{_x:50, _y:450});
_root.attachMovie("score","score",5);
level = new Array();
level[1] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
level[2] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
level[3] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
level[4] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 2, 1, 1, 2, 0, 1);
level[20] = new Array(0, 2, 1, 0, 0, 2, 2, 2, 2, 1, 1, 1, 0, 0, 1, 0, 2, 0, 2, 0, 2, 2, 1, 0, 1, 2, 2, 1, 1, 0, 0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 2, 1, 2, 2);
level[21] = new Array(0, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 0, 0, 1, 1, 1, 2, 1, 1, 1, 0, 1, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 0, 0, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0);
current_level = 1;
points = 0;
play_level(current_level);
function play_level(current_level) {
	_root.attachMovie("grid","grid",1,{_x:90, _y:90});
	swapping = false;
	from = -1;
	to = -1;
	nextlevel = false;
	prevlevel = false;
	horizontal_swap = false;
	vertical_swap = false;
	horizontal_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	horizontal_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	for (x=0; x<64; x++) {
		if (level[current_level][x] == 1 or level[current_level][x] == 3) {
			grid.attachMovie("cell","cell_"+x,grid.getNextHighestDepth(),{_x:(x%8)*40, _y:Math.floor(x/8)*40});
		}
		if (level[current_level][x] == 2 or level[current_level][x] == 3) {
			at = grid.attachMovie("atom", "atom_"+x, grid.getNextHighestDepth(), {_x:(x%8)*40, _y:Math.floor(x/8)*40});
			at.pos = x;
			at.moving = 0;
			at.onEnterFrame = function() {
				if (this.moving == 0) {
					if (swapping) {
						if (horizontal_swap) {
							if ((this.pos>=from*8) and (this.pos<=from*8+7)) {
								this.moving = (to-from)*40;
							}
							if ((this.pos>=to*8) and (this.pos<=to*8+7)) {
								this.moving = (from-to)*40;
							}
						}
						if (vertical_swap) {
							for (x=0; x<8; x++) {
								if (this.pos == x*8+from) {
									this.moving = (to-from)*40;
								}
							}
							for (x=0; x<8; x++) {
								if (this.pos == x*8+to) {
									this.moving = (from-to)*40;
								}
							}
						}
					}
				}
				else {
					if (this.moving>0) {
						if (horizontal_swap) {
							this._y += 10;
						}
						if (vertical_swap) {
							this._x += 10;
						}
						this.moving -= 10;
					}
					if (this.moving<0) {
						if (horizontal_swap) {
							this._y -= 10;
						}
						if (vertical_swap) {
							this._x -= 10;
						}
						this.moving += 10;
					}
					if (this.moving == 0) {
						if (level[current_level][this.pos] == 3) {
							points--;
						}
						level[current_level][this.pos] -= 2;
						this.pos = Math.round(this._x/40)+Math.round(this._y/40)*8;
						level[current_level][this.pos] += 2;
						if (level[current_level][this.pos] == 3) {
							points++;
						}
						score.scoretxt.text = "Score: "+points;
						reset_swap();
					}
				}
			};
		}
	}
	for (x=1; x<=8; x++) {
		hs = grid.attachMovie("hswapper", "hswapper_"+x, grid.getNextHighestDepth(), {_x:-20, _y:40*x-20});
		hs.pos = x;
		hs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					horizontal_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						vertical_click[x] = 0;
					}
				}
				else {
					this.gotoAndStop(1);
					horizontal_hover[this.pos-1] = 0;
				}
				if (horizontal_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
		vs = grid.attachMovie("vswapper", "vswapper_"+x, grid.getNextHighestDepth(), {_x:40*x-20, _y:-20});
		vs.pos = x;
		vs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					vertical_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						horizontal_click[x] = 0;
					}
				}
				else {
					this.gotoAndStop(1);
					vertical_hover[this.pos-1] = 0;
				}
				if (vertical_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
	}

	rays.onEnterFrame = function() {
		if (!swapping) {
			horizontal_swap = false;
			vertical_swap = false;
			this.clear();
			clicks = 0;
			this.lineStyle(2,0x00ff00);
			for (x=0; x<8; x++) {
				if ((horizontal_hover[x] == 1) or (horizontal_click[x] == 1)) {
					this.moveTo(90,x*40+110);
					this.lineTo(410,x*40+110);
					if (horizontal_click[x] == 1) {
						clicks++;
						horizontal_swap = true;
					}
				}
				if ((vertical_hover[x] == 1) or (vertical_click[x] == 1)) {
					this.moveTo(x*40+110,90);
					this.lineTo(x*40+110,410);
					if (vertical_click[x] == 1) {
						clicks++;
						vertical_swap = true;
					}
				}
			}
			if (clicks == 2) {
				from = -1;
				to = -1;
				swapping = true;
				if (horizontal_swap) {
					for (x=0; x<8; x++) {
						if (horizontal_click[x] == 1) {
							if (from == -1) {
								from = x;
							}
							else {
								to = x;
							}
						}
					}
					if (!pieces_on_row(from) and !pieces_on_row(to)) {
						reset_swap();
					}
				}
				if (vertical_swap) {
					for (x=0; x<8; x++) {
						if (vertical_click[x] == 1) {
							if (from == -1) {
								from = x;
							}
							else {
								to = x;
							}
						}
					}
					if (!pieces_on_column(from) and !pieces_on_column(to)) {
						reset_swap();
					}
				}
			}
		}
	};
}
_root.onMouseDown = function() {
	for (x=0; x<8; x++) {
		if (horizontal_hover[x] == 1) {
			horizontal_click[x] = 1-horizontal_click[x];
		}
		if (vertical_hover[x] == 1) {
			vertical_click[x] = 1-vertical_click[x];
		}
	}
	if (nextlevel) {
		grid.removeMovieClip();
		current_level++;
		play_level(current_level);
	}
	if (prevlevel) {
		grid.removeMovieClip();
		current_level--;
		play_level(current_level);
	}
};
function pieces_on_row(row) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][row*8+x]>=2) {
			res = true;
		}
	}
	return (res);
}
function pieces_on_column(col) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][col+x*8]>=2) {
			res = true;
		}
	}
	return (res);
}

function reset_swap() {
	for (x=0; x<8; x++) {
		horizontal_click[x] = 0;
		vertical_click[x] = 0;
		swapping = false;
	}
}
next_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		nextlevel = true;
	}
	else {
		nextlevel = false;
		this.gotoAndStop(1);
	}
};
prev_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		prevlevel = true;
	}
	else {
		prevlevel = false;
		this.gotoAndStop(1);
	}
};

Line 4: Attaching the score movieclip into the movie

Line 28: Declaring a new variable called points that will contain the score

Line 96: When the square stopped moving, I check if before moving the square was inside a ring. It's easy: I just have to check if the level array element at pos attribute (that has not been updated yet) was 3

Line 97: In this case, subtract 1 to points variable

Line 102: Now I check if the square moved into a ring. Now that I have the pos value updated at line 100, I have to check if the level array element at pos attribute (that now is updated) is 3

Line 103: In this case, add 1 to points variable

Line 105: Updating the text in the scoretext text area into the score movieclip.

And now the game has score too!

Now, we must allow the player to make only 100 moves (swapping rows/columns and changing level count as a move) and fix a little bug that allows the player to go to previous level even if there isn't any previous level (when he's playing level 1) and go to next level even if there isn's any next level (when he's playing level 21)

Only 100 moves

After creating the moves movieclip, in the same way as for the score, just calling and linking the movieclip as moves and instancing the text area as movestxt, this is the actionscript:

_root.createEmptyMovieClip("rays",2);
_root.attachMovie("next_level","next_level",3,{_x:400, _y:450});
_root.attachMovie("prev_level","prev_level",4,{_x:50, _y:450});
_root.attachMovie("score","score",5);
_root.attachMovie("moves","moves",6);
level = new Array();
level[1] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,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 2, 2, 2, 2, 0, 2, 0, 0, 2, 0, 1, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 1, 1, 1, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 2, 0, 1, 2, 0, 0, 1, 0, 1, 1);
level[18] = new Array(1, 2, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 1, 0, 1, 2, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 2, 0, 2, 1, 2, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 2, 1);
level[19] = new Array(1, 1, 2, 0, 0, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 2, 1, 0, 1, 2, 2, 1, 0, 0, 2, 2, 1, 0, 2, 1, 2, 2, 1, 1, 1, 2, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 2, 1, 0, 0, 2, 1, 0, 2, 2, 1, 1, 0, 2, 1, 1, 2, 0, 1);
level[20] = new Array(0, 2, 1, 0, 0, 2, 2, 2, 2, 1, 1, 1, 0, 0, 1, 0, 2, 0, 2, 0, 2, 2, 1, 0, 1, 2, 2, 1, 1, 0, 0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 2, 1, 2, 2);
level[21] = new Array(0, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 0, 0, 1, 1, 1, 2, 1, 1, 1, 0, 1, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 0, 0, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0);
current_level = 1;
points = 0;
turns = 100;
play_level(current_level);
function play_level(current_level) {
	if (current_level == 1) {
		prev_level._visible = false;
	}
	else {
		prev_level._visible = true;
	}
	if (current_level == 21) {
		next_level._visible = false;
	}
	else {
		next_level._visible = true;
	}
	_root.attachMovie("grid","grid",1,{_x:90, _y:90});
	swapping = false;
	from = -1;
	to = -1;
	nextlevel = false;
	prevlevel = false;
	horizontal_swap = false;
	vertical_swap = false;
	horizontal_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	horizontal_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	for (x=0; x<64; x++) {
		if (level[current_level][x] == 1 or level[current_level][x] == 3) {
			grid.attachMovie("cell","cell_"+x,grid.getNextHighestDepth(),{_x:(x%8)*40, _y:Math.floor(x/8)*40});
		}
		if (level[current_level][x] == 2 or level[current_level][x] == 3) {
			at = grid.attachMovie("atom", "atom_"+x, grid.getNextHighestDepth(), {_x:(x%8)*40, _y:Math.floor(x/8)*40});
			at.pos = x;
			at.moving = 0;
			at.onEnterFrame = function() {
				if (this.moving == 0) {
					if (swapping) {
						if (horizontal_swap) {
							if ((this.pos>=from*8) and (this.pos<=from*8+7)) {
								this.moving = (to-from)*40;
							}
							if ((this.pos>=to*8) and (this.pos<=to*8+7)) {
								this.moving = (from-to)*40;
							}
						}
						if (vertical_swap) {
							for (x=0; x<8; x++) {
								if (this.pos == x*8+from) {
									this.moving = (to-from)*40;
								}
							}
							for (x=0; x<8; x++) {
								if (this.pos == x*8+to) {
									this.moving = (from-to)*40;
								}
							}
						}
					}
				}
				else {
					if (this.moving>0) {
						if (horizontal_swap) {
							this._y += 10;
						}
						if (vertical_swap) {
							this._x += 10;
						}
						this.moving -= 10;
					}
					if (this.moving<0) {
						if (horizontal_swap) {
							this._y -= 10;
						}
						if (vertical_swap) {
							this._x -= 10;
						}
						this.moving += 10;
					}
					if (this.moving == 0) {
						if (level[current_level][this.pos] == 3) {
							points--;
						}
						level[current_level][this.pos] -= 2;
						this.pos = Math.round(this._x/40)+Math.round(this._y/40)*8;
						level[current_level][this.pos] += 2;
						if (level[current_level][this.pos] == 3) {
							points++;
						}
						score.scoretxt.text = "Score: "+points;
						reset_swap();
					}
				}
			};
		}
	}
	for (x=1; x<=8; x++) {
		hs = grid.attachMovie("hswapper", "hswapper_"+x, grid.getNextHighestDepth(), {_x:-20, _y:40*x-20});
		hs.pos = x;
		hs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					horizontal_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						vertical_click[x] = 0;
					}
				}
				else {
					this.gotoAndStop(1);
					horizontal_hover[this.pos-1] = 0;
				}
				if (horizontal_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
		vs = grid.attachMovie("vswapper", "vswapper_"+x, grid.getNextHighestDepth(), {_x:40*x-20, _y:-20});
		vs.pos = x;
		vs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					vertical_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						horizontal_click[x] = 0;
					}
				}
				else {
					this.gotoAndStop(1);
					vertical_hover[this.pos-1] = 0;
				}
				if (vertical_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
	}

	rays.onEnterFrame = function() {
		if (!swapping) {
			horizontal_swap = false;
			vertical_swap = false;
			this.clear();
			clicks = 0;
			this.lineStyle(2,0x00ff00);
			for (x=0; x<8; x++) {
				if ((horizontal_hover[x] == 1) or (horizontal_click[x] == 1)) {
					this.moveTo(90,x*40+110);
					this.lineTo(410,x*40+110);
					if (horizontal_click[x] == 1) {
						clicks++;
						horizontal_swap = true;
					}
				}
				if ((vertical_hover[x] == 1) or (vertical_click[x] == 1)) {
					this.moveTo(x*40+110,90);
					this.lineTo(x*40+110,410);
					if (vertical_click[x] == 1) {
						clicks++;
						vertical_swap = true;
					}
				}
			}
			if (clicks == 2) {
				if (turns>0) {
					turns--;
					moves.movestxt.text = "Moves: "+turns;
					from = -1;
					to = -1;
					swapping = true;
					if (horizontal_swap) {
						for (x=0; x<8; x++) {
							if (horizontal_click[x] == 1) {
								if (from == -1) {
									from = x;
								}
								else {
									to = x;
								}
							}
						}
						if (!pieces_on_row(from) and !pieces_on_row(to)) {
							reset_swap();
						}
					}
					if (vertical_swap) {
						for (x=0; x<8; x++) {
							if (vertical_click[x] == 1) {
								if (from == -1) {
									from = x;
								}
								else {
									to = x;
								}
							}
						}
						if (!pieces_on_column(from) and !pieces_on_column(to)) {
							reset_swap();
						}
					}
				}
				else {
					reset_swap();
				}
			}
		}
	};
}
_root.onMouseDown = function() {
	for (x=0; x<8; x++) {
		if (horizontal_hover[x] == 1) {
			horizontal_click[x] = 1-horizontal_click[x];
		}
		if (vertical_hover[x] == 1) {
			vertical_click[x] = 1-vertical_click[x];
		}
	}
	if ((nextlevel) and (current_level != 21) and (turns>0)) {
		turns--;
		moves.movestxt.text = "Moves: "+turns;
		grid.removeMovieClip();
		current_level++;
		play_level(current_level);
	}
	if ((prevlevel) and (current_level != 1) and (turns>0)) {
		turns--;
		moves.movestxt.text = "Moves: "+turns;
		grid.removeMovieClip();
		current_level--;
		play_level(current_level);
	}
};
function pieces_on_row(row) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][row*8+x]>=2) {
			res = true;
		}
	}
	return (res);
}
function pieces_on_column(col) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][col+x*8]>=2) {
			res = true;
		}
	}
	return (res);
}

function reset_swap() {
	for (x=0; x<8; x++) {
		horizontal_click[x] = 0;
		vertical_click[x] = 0;
		swapping = false;
	}
}
next_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		nextlevel = true;
	}
	else {
		nextlevel = false;
		this.gotoAndStop(1);
	}
};
prev_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		prevlevel = true;
	}
	else {
		prevlevel = false;
		this.gotoAndStop(1);
	}
};

Line 5: Attaching the moves movieclip

Line 30: Declaring a variable called turns, that will store the number of moves left, and setting it to 100

Line 33: If we are playing level 1...

Line 34: ... then don't show the previous level button

Line 37: If not, then show the previous level button

Lines 39-44: Same thing as lines 33-38, just for the next level button when we are playing on level 21

Please notice that this method will only make the buttons invisible, but they will remain clickable.

Line 195: All the code that previously was executed when the player clicked on two buttons, now it will be executed only if turns is greater than zero (the player did not spend all moves)

Line 196: If the player switched rows/columns, then drecrease turns variable

Line 197: Update the number of moves left shown onscreen

Lines 232-234: If turns is zero, then call the function reset_swap... nothing happens and the player is not longer able to swap rows/columns

Line 248: Now the condition to advance level is: nextlevel is true (as before) and the current level is not the last one (21st) and turns is greater than zero (the player has moves left)

Line 249: if the player changes the level, then decrease turns variable

Line 250: Update the number of moves left shown onscreen

Lines 255-257: Same concept as lines 248-250, just for the previous level

And finally the game is complete

Now you can optimize the code, make your custom levels, add an intro, a game over screen, a highscore tables, and whatever you want, but we made the complete game in just 300 lines of code.

Download the source code and tell me if you made some interesting game out of it.

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