溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

html5怎么實現(xiàn)神經(jīng)貓游戲

發(fā)布時間:2022-03-03 13:43:21 來源:億速云 閱讀:96 作者:iii 欄目:web開發(fā)

本文小編為大家詳細(xì)介紹“html5怎么實現(xiàn)神經(jīng)貓游戲”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“html5怎么實現(xiàn)神經(jīng)貓游戲”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

  游戲設(shè)計思路

  從貓當(dāng)前的位置找六個方向中可通行的鄰居,然后從這些鄰居出發(fā),再找它們各自的可通行鄰居,一直這樣找下去,一邊找的過程中,一邊判斷當(dāng)前已經(jīng)找到的鄰居中有沒有處在游戲區(qū)邊緣的,如果有,那么尋找過程提前結(jié)束,判斷結(jié)果是:貓沒有被圍住。如果直到所有的可通行鄰居都找到了,里面都沒有處在游戲區(qū)邊緣的,那么判斷結(jié)果是:貓被圍住了。

  參考代碼

  cat.html

  app.js

  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;

  //定義7種狀態(tài) 表示 移動位置

  var MOVE_NONE = -1,MOVE_LEFT = 0,MOVE_UP_LEFT = 1,MOVE_UP_RIGHT = 2,MOVE_RIGHT = 3,MOVE_DOWN_RIGHT = 4,MOVE_DOWN_LEFT = 5;

  function getMoveDir(cat){

  //分別判斷能走的位置

  var distanceMap = [];

  //left

  var can = true;

  for (var 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;

  }

  //left up

  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] = can.indexY-y;

  break;

  }

  if(y%2 == 0){

  x--;

  }

  y--;

  if(y<0 ||x<0){

  break;

  }

  }

  if(can){

  return MOVE_UP_LEFT;

  }

  //right up

  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] = can.indexY-y;

  break;

  }

  if(y%2 == 1){

  x++;

  }

  y--;

  if(y <0||x>8){

  break;

  }

  }

  if(can){

  return MOVE_UP_RIGHT;

  }

  //right

  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;

  }

  //ritht down

  can = true;

  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;

  }

  //left down

  can = true;

  x= cat.indexX,y = cat.indexY;

  while(true){

  if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){

  can = false;

  distanceMap[MOVE_DOWN_LEFT] = y -cat.index;

  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

  if(distanceMap[dir]>maxValue){

  maxValue = distanceMap[dir];

  maxDir = dir;

  }

  }

  if(maxValue > 1){

  return maxDir;

  }else{

  return MOVE_NONE;

  }

  }

  function circleClicked(event){

  if(event.target.getCircleType() != Circle.TYPE_CAT){

  event.target.setCircleType(Circle.TYPE_SELECTED);

  }else{

  return;

  }

  //表示碰到邊緣 游戲結(jié)束

  if(currentCat.indexX == 0 ||currentCat.indexX == 8 ||currentCat.indexY==0 ||currentCat.indexY==8){

  alert("游戲結(jié)束");

  return;

  }

  var dir = getMoveDir(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+1:currentCat.indexX][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_RIGHT:

  currentCat.setCircleType(Circle.TYPE_UNSELECTED);

  currentCat = circleArr[currentCat.indexY%2?currentCat.indexX+1:currentCat.indexX][currentCat.indexY+1];

  currentCat.setCircleType(Circle.TYPE_CAT)

  break;

  case MOVE_DOWN_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;

  //沒有方向走 游戲結(jié)束

  default:

  alert("游戲結(jié)束");

  }

  }

  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;

  //因為Y軸是 一前一后 所有判斷一下 Y%2

  c.x = indexY%2?indexX*55+25:indexX*55;

  c.y = indexY * 55;

  if(indexX == 4 && indexY == 4){

  //中間出現(xiàn)一只貓

  c.setCircleType(3);

  currentCat = c;

  }else if(Math.random() <0.1){

  //讓頁面上隨機出現(xiàn) 不能走的方框 方便圍這只貓

  c.setCircleType(Circle.TYPE_SELECTED);

  }

  //添加事件

  c.addEventListener("click",circleClicked);

  }

  }

  }

  addCircles();

  Circle.js

  function Circle(){

  createjs.Shape.call(this);

  this.setCircleType = function(type){

  this._circleType = type;

  switch (type){

  //沒有點擊過的顏色

  case Circle.TYPE_UNSELECTED:

  this.setColor("#cccccc");

  break;

  //點擊過的顏色

  case Circle.TYPE_SELECTED:

  this.setColor("#ff6600");

  break;

  //貓的顏色

  case Circle.TYPE_CAT:

  this.setColor("#0000ff");

  break;

  }

  }

  this.setColor = function(colorString){

  this.graphics.beginFill(colorString);

  this.graphics.drawCircle(0,0,25);

  this.graphics.endFill();

  }

  this.getCircleType = function(){

  return this._circleType;

  }

  this.setCircleType(1);

  }

  Circle.prototype = new createjs.Shape();

  //三種狀態(tài) 表示 一個為點擊之后的 一個點擊之前 一個是貓

  Circle.TYPE_UNSELECTED = 1;

  Circle.TYPE_SELECTED = 2;

  Circle.TYPE_CAT = 3;

讀到這里,這篇“html5怎么實現(xiàn)神經(jīng)貓游戲”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI