标签:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="../javaScript/createjs/easeljs-0.8.2.min.js"></script> </head> <body> <canvas height="800px" width="800px" id="gameView"></canvas> <script> function Circle(){ createjs.Shape.call(this); /*设置颜色 * */ this.setCorcol = function(colorString){ this.graphics.beginFill(colorString); this.graphics.drawCircle(0,0,25); this.graphics.endFill(); } /* 设置小圆形类型 * type : 1: * 2: * 3: * */ this.setCircleType = function (type){ this._circleType = type; switch (type){ case Circle.TYPE_UNSELECTED: this.setCorcol("#cccccc"); break; case Circle.TYPE_SELECTED: this.setCorcol("#00ff00"); break; case Circle.TYPE_CAT: this.setCorcol("#ff00ff"); break; default : this.setCorcol("#abcdef"); break; } } /* 获取圆形颜色 * */ this.getCircleType = function(){ return this._circleType; } //默认设置颜色 this.setCircleType(Circle.TYPE_UNSELECTED); } //将createjs形状类型传递和Circle Circle.prototype = new createjs.Shape(); Circle.TYPE_UNSELECTED = 1; Circle.TYPE_SELECTED = 2; Circle.TYPE_CAT = 3; //创建并监控容器 var stage = new createjs.Stage(‘gameView‘); createjs.Ticker.setFPS(30); createjs.Ticker.addEventListener("tick",stage); // var gameView = new createjs.Container(); gameView.x=30; gameView.y=30; stage.addChild(gameView); var circleArr = [[],[],[],[],[],[],[],[],[]]; var currentCat;//猫 var MOVE_NONE = -1, MOVE_UP_LEFT=1,MOVE_UP_RIGHT=2,MOVE_LEFT=3,MOVE_RIGHT=4,MOVE_DOWN_RIGHT=5,MOVE_DOWN_LEFT=6; //圆形点击事件 function circleClicked(even){ if(even.target.getCircleType()!=3){ even.target.setCircleType(2); } if(currentCat.indexX==0||currentCat.indexX==8||currentCat.indexY==0||currentCat.indexY==8){ alert("游戏结束!"); return; } var dir = getMoveCat(currentCat); switch (dir){ case MOVE_LEFT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexX-1][currentCat.indexY]; currentCat.setCircleType(Circle.TYPE_CAT); break; case MOVE_UP_LEFT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexY % 2 ? currentCat.indexX:currentCat.indexX-1][currentCat.indexY-1]; currentCat.setCircleType(Circle.TYPE_CAT); break; case MOVE_UP_RIGHT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexY % 2 ? currentCat.indexX:currentCat.indexX+1][currentCat.indexY-1]; currentCat.setCircleType(Circle.TYPE_CAT); break; case MOVE_RIGHT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexX+1][currentCat.indexY]; currentCat.setCircleType(Circle.TYPE_CAT); break; case MOVE_DOWN_LEFT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexY % 2 ? currentCat.indexX:currentCat.indexX][currentCat.indexY+1]; currentCat.setCircleType(Circle.TYPE_CAT); break; case MOVE_DOWN_RIGHT: currentCat.setCircleType(Circle.TYPE_UNSELECTED); currentCat = circleArr[currentCat.indexY % 2 ? currentCat.indexX:currentCat.indexX-1][currentCat.indexY+1]; currentCat.setCircleType(Circle.TYPE_CAT); break; } } function getMoveCat(cat){ var distanceMap = []; var can = true; var x,y; //left for (x=cat.indexX;x>=0;x--){ if(circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_LEFT] = cat.indexX-x; break; } } if(can){ return MOVE_LEFT; } //左上 can=true; var x = cat.indexX,y=cat.indexY; while(true){ if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_UP_LEFT] = cat.indexY -y; break; } if(y%2 ==0){ x--; } y--; if(y<0|| x<0){ break; } } if(can){ return MOVE_UP_LEFT; } //右上 can=true; var x = cat.indexX,y=cat.indexY; while(true){ if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_UP_RIGHT] = cat.indexY -y; break; } if(y%2 ==1){ x++; } y--; if(y<0|| x>8){ break; } } if(can){ return MOVE_UP_RIGHT; } //右 var can = true; for (var x=cat.indexX;x<9;x++){ if(circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_RIGHT] = x-cat.indexX; break; } } if(can){ return MOVE_RIGHT; } //右下 can=true; var x = cat.indexX,y=cat.indexY; while(true){ if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_DOWN_RIGHT] = y- cat.indexY; break; } if(y%2 ==1){ x++; } y++; if(y>8|| x>8){ break; } } if(can){ return MOVE_DOWN_RIGHT; } //左下 can=true; var x = cat.indexX,y=cat.indexY; while(true){ if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_DOWN_LEFT] = y-cat.indexY ; break; } if(y%2 ==0){ x--; } y++; if(y>8|| x<0){ break; } } if(can){ return MOVE_DOWN_LEFT; } var maxDir = -1, maxValue = -1; for (var dir = 0; dir < distanceMap.length; dir++) { if (distanceMap[dir] > maxValue) { maxDir = dir; maxValue = distanceMap[dir]; } } if (maxValue > 1) { return maxDir; } else { return MOVE_NONE; } } /*添加圆形 布局 * */ function addCircles(){ for(var indexY=0; indexY<9; indexY++){ for(var indexX=0; indexX<9; indexX++){ var c = new Circle(); gameView.addChild(c); circleArr[indexX][indexY] =c; c.indexX = indexX; c.indexY = indexY; c.x = indexY%2?indexX*55+25:indexX*55; c.y = indexY*55; if(indexX==4&&indexY==4){ c.setCircleType(Circle.TYPE_CAT); currentCat = c; }else if (Math.random() < 0.3) { c.setCircleType(Circle.TYPE_SELECTED); } c.addEventListener("click",circleClicked); } } } addCircles(); </script> </body> </html>
标签:
原文地址:http://www.cnblogs.com/luanyizixuan/p/5618910.html