简单的拼图小游戏

由于需要,做的这么个简单的小游戏,其实有些事情并不难,就是找个合适的解决方案。
上代码:

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.errors.MemoryError;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;

	public class ImagePuzzle extends Sprite
	{
		private var img:Loader = new Loader();
		private var num:uint = 6;
		private var blocks:Array = new Array(num);
		private var currPosition:Object = {"indexX":num-1,"indexY":num-1};
		private var result:TextField;
		public function ImagePuzzle()
		{
			img.contentLoaderInfo.addEventListener(Event.COMPLETE,cutImg);
			img.load(new URLRequest("16.jpg"));
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyHandle);
			result =  new TextField();
			result.text="进行中。。。";
			result.height = 24;
			result.x = 260;
			addChild(result);
		}

		private function cutImg(event:Event):void{
			var oldImg:Bitmap = Bitmap(img.content);
			var bitImgData:BitmapData = oldImg.bitmapData;
			var blockW:Number =  bitImgData.width/num;
			var blockH:Number =  bitImgData.height/num;
			oldImg.x = bitImgData.width + 20;
			oldImg.y = result.height;
			addChild(oldImg);
			for (var i:Number = 0;i<num;i++){
				blocks[i] = new Array(num);
				for (var j:Number = 0;j<num;j++){
					var block:BitmapData = new BitmapData(blockW,blockH, false, 0x000000);
					var toX:Number = blockW*j;
					var toY:Number = blockH*i;
					var blockImg:Bitmap = new Bitmap(block);
					if(!(i == num-1 && j == num-1)){
						block.copyPixels(bitImgData,
							new Rectangle(toX,toY,blockW,blockH),
							new Point(0,0));
					}
					blockImg.x =   toX;
					blockImg.y =   toY+result.height;;
					blocks[i][j] = blockImg;
					blockImg.name = "blockImg"+i+j;
					addChild(blockImg);
				}
			}
			randomBlock();
		}



		private function swapBlock(temp:Object,currPosition:Object,isJudge:Boolean= true):void{
			var one:Bitmap = blocks[temp.indexX][temp.indexY];
			var two:Bitmap = blocks[currPosition.indexX][currPosition.indexY];
			var tempX:Number = one.x;
			var tempY:Number = one.y;
			one.x = two.x;
			one.y = two.y;
			two.x = tempX;
			two.y = tempY;
			blocks[temp.indexX][temp.indexY] = two;
			blocks[currPosition.indexX][currPosition.indexY] = one;
			if(isJudge){
				if(judgeWin())
					result.text = "YOU WIN !!!";
			}else{
				if(blocks[temp.indexX][temp.indexY].name=="blockImg55"){
					this.currPosition = {"indexX":temp.indexX,"indexY":[temp.indexY]};
				}
				if(blocks[currPosition.indexX][currPosition.indexY].name=="blockImg55"){
					this.currPosition = {"indexX":currPosition.indexX,"indexY":[currPosition.indexY]};
				}
			}
		}

		private function randomBlock():void{
			var temp:Number = Math.floor(Math.random()*num*num)+num*num/2;
			while(temp--){
				swapBlock(
					{"indexX":Math.floor(Math.random()*num),"indexY":Math.floor(Math.random()*num)},
					{"indexX":Math.floor(Math.random()*num),"indexY":Math.floor(Math.random()*num)},
					false
				);
			}
		}

		private function judgeWin():Boolean{
			for(var i:uint=0;i<blocks.length;i++)
				for(var j:uint=0;j0){
				currPosition.indexY--;
			}
			if(keyCode == 40 && currPosition.indexX>0){
				currPosition.indexX--;
			}
			if(keyCode == 37 && currPosition.indexY<(num-1)){
				currPosition.indexY++;
			}
			if(keyCode == 38 && currPosition.indexX<num-1){
				currPosition.indexX++;
			}
			swapBlock(temp,currPosition);
		}
	}
}

Tags:

发表评论

电子邮件地址不会被公开。 必填项已用*标注