Talking about Maze game, Actionscript 3, Flex, Game development and Users contributions.
After publishing Perfect maze generation – tile based version written in Php, I got some emails asking for an AS3 version.
Pedro Taranto made the AS3 porting in less than a day, and here’s what you’ll get:
Click on the maze to generate a new one
And this is the source code:
package br.com.pedrotaranto
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.geom.Point;
[SWF(backgroundColor = "#FFFFFF", width = '800', height = '600', frameRate = "30")]
public class TiledMazeGen extends Sprite
{
//constants
private const MAZE_WIDTH : uint = 20;
private const MAZE_HEIGHT : uint = 20;
private const TILE_SIZE : uint = 10;
private const START_COLOR : uint = 0xFF0000;
private const FINISH_COLOR : uint = 0x00FF00;
private const WALL_COLOR : uint = 0x000000;
private const WALKABLE_COLOR : uint = 0xDDDDDD;
//directions
private const NORTH : String = "N";
private const SOUTH : String = "S";
private const EAST : String = "E";
private const WEST : String = "W";
//variables
private var _width : uint;
private var _height : uint;
private var _maze : Array;
private var _moves : Array;
private var _start : Point;
private var _finish : Point;
private var _container : Sprite;
public function TiledMazeGen () : void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(MouseEvent.CLICK, _generate);
_width = MAZE_WIDTH * 2 + 1;
_height = MAZE_HEIGHT * 2 + 1;
_start = new Point(1, 1);
//_finish = new Point(_height - 2, _width - 2);
_container = new Sprite();
_generate();
}
private function _generate ( event : MouseEvent = null ) : void
{
_initMaze();
_createMaze();
_drawMaze();
}
private function _initMaze () : void
{
_maze = new Array(_width);
for ( var x : int = 0; x < _height; x++ )
{
_maze[x] = new Array(_height);
for ( var y : int = 0; y < _width; y++ )
{
_maze[x][y] = true;
}
}
_maze[_start.x][_start.y] = false;
}
private function _createMaze () : void
{
var back : int;
var move : int;
var possibleDirections : String;
var pos : Point = _start.clone();
_moves = new Array();
_moves.push(pos.y + (pos.x * _width));
while ( _moves.length )
{
possibleDirections = "";
if ((pos.x + 2 < _height ) && (_maze[pos.x + 2][pos.y] == true) && (pos.x + 2 != false) && (pos.x + 2 != _height - 1) )
{
possibleDirections += SOUTH;
}
if ((pos.x - 2 >= 0 ) && (_maze[pos.x - 2][pos.y] == true) && (pos.x - 2 != false) && (pos.x - 2 != _height - 1) )
{
possibleDirections += NORTH;
}
if ((pos.y - 2 >= 0 ) && (_maze[pos.x][pos.y - 2] == true) && (pos.y - 2 != false) && (pos.y - 2 != _width - 1) )
{
possibleDirections += WEST;
}
if ((pos.y + 2 < _width ) && (_maze[pos.x][pos.y + 2] == true) && (pos.y + 2 != false) && (pos.y + 2 != _width - 1) )
{
possibleDirections += EAST;
}
if ( possibleDirections.length > 0 )
{
move = _randInt(0, (possibleDirections.length - 1));
switch ( possibleDirections.charAt(move) )
{
case NORTH:
_maze[pos.x - 2][pos.y] = false;
_maze[pos.x - 1][pos.y] = false;
pos.x -=2;
break;
case SOUTH:
_maze[pos.x + 2][pos.y] = false;
_maze[pos.x + 1][pos.y] = false;
pos.x +=2;
break;
case WEST:
_maze[pos.x][pos.y - 2] = false;
_maze[pos.x][pos.y - 1] = false;
pos.y -=2;
break;
case EAST:
_maze[pos.x][pos.y + 2] = false;
_maze[pos.x][pos.y + 1] = false;
pos.y +=2;
break;
}
_moves.push(pos.y + (pos.x * _width));
}
else
{
back = _moves.pop();
pos.x = int(back / _width);
pos.y = back % _width;
}
}
}
private function _drawMaze () : void
{
var tile : Sprite;
if ( contains(_container) )
{
removeChild(_container)
}
_container = new Sprite();
addChild(_container);
for ( var x : int = 0; x < _height; x++ )
{
for ( var y : int = 0; y < _width; y++ )
{
tile = (_maze[x][y] == true) ? _drawTile(WALL_COLOR) : _drawTile(WALKABLE_COLOR);
tile.x = x * TILE_SIZE;
tile.y = y * TILE_SIZE;
_container.addChild(tile);
}
}
//start tile
tile = _drawTile(START_COLOR);
tile.x = _start.x * TILE_SIZE;
tile.y = _start.y * TILE_SIZE;
_container.addChild(tile);
//finish tile
/*tile = _drawTile(FINISH_COLOR);
tile.x = _finish.x * TILE_SIZE;
tile.y = _finish.y * TILE_SIZE;
_container.addChild(tile);*/
}
private function _drawTile ( color : uint ) : Sprite
{
var tile : Sprite = new Sprite();
tile.graphics.beginFill(color);
tile.graphics.drawRect(0, 0, TILE_SIZE, TILE_SIZE);
tile.graphics.endFill();
return tile;
}
private function _randInt ( min : int, max : int ) : int
{
return int((Math.random() * (max - min + 1)) + min);
}
}
}
Download the source and enjoy!
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.