最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
javascript實現(xiàn)貪吃蛇經(jīng)典游戲的方法

這篇文章主要講解了javascript實現(xiàn)貪吃蛇經(jīng)典游戲的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了瀘縣免費(fèi)建站歡迎大家使用!

js面向?qū)ο缶幊讨澇陨?,供大家參考,具體內(nèi)容如下

首先:面向?qū)ο缶幊?,我們要找到項目中具體的對象,此處為(食物(food),蛇(snake),游戲本身(game))也可不把游戲本身作為對象,邏輯體現(xiàn)出來即可。

接著分析每個對象的具體的屬性及方法:
1)food 對象:屬性有:位置,大小,顏色;方法有:渲染在頁面,隨機(jī)不同位置生成;
2)snake對象:屬性有:位置,大小,總節(jié)數(shù)(計分方便),顏色;方法有:渲染在頁面,移動(移動過程中判斷其它)。
3)game對象:游戲邏輯的編寫;

ok 開敲:

1)簡單的靜態(tài)頁面編寫(地圖)

(1)html




 
 
 
 
 
 
 
 貪吃蛇


 

(2)css(如果用邊框來作為限制的邊界,那么box-sizing屬性是必不可少的(以免食物和蛇頭坐標(biāo)之間存在誤差))

* {
 margin: 0;
 padding: 0;
}

.map {
 position: relative;
 height: 600px;
 width: 800px;
 border: 1px solid #333;
 margin: 0 auto;
 /* 盒子模型去除邊框 */
 box-sizing: border-box;
}

2)food對象編寫(細(xì)節(jié)處含注釋)

//cwen加載頁面所有元素
window.addEventListener('load', function() {

 //cwen自調(diào)用函數(shù),開啟一個新的作用域,避免命名沖突
 (function() {

 //cwen定義全局變量
 //實物數(shù)組
 var elements = [];
 //cwen實物
 function Food(options) {
  options = options || {};
  this.x = options.x || 0;
  this.y = options.y || 0;
  this.width = options.width || 20;
  this.height = options.height || 20;
  this.color = options.color || 'yellow';
 }


 //cwen隨機(jī)數(shù)函數(shù)
 function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
 }
 //cwen渲染
 Food.prototype.render = function(map) {
  //刪除之前的食物
  remove();
  //todo動態(tài)創(chuàng)建div實物
  var div = document.createElement('div');
  map.appendChild(div);
  //把div添加給數(shù)組
  elements.push(div);
  //todo隨機(jī)設(shè)置x,y的值(實物的位置)-----在map中生成隨機(jī)位置
  // ! 值 = Math.floor(Math.random() * 可能值得總數(shù) + 第一個可能的值)
  this.x = getRandom(0, map.offsetWidth / this.width - 1) * this.width;
  this.y = getRandom(0, map.offsetHeight / this.height - 1) * this.height;
  div.style.position = 'absolute';
  div.style.left = this.x + 'px';
  div.style.top = this.y + 'px';
  div.style.width = this.width + 'px';
  div.style.height = this.height + 'px';
  div.style.backgroundColor = this.color;
 }

 function remove() {
  //為了刪除干凈,從索引最大的開始循環(huán)刪除
  for (var i = elements.length - 1; i >= 0; i--) {
  //刪除遍歷到的div
  elements[i].parentNode.removeChild(elements[i]);
  //刪除數(shù)組中的元素1)第幾個開始,2)要刪除個數(shù)
  elements.splice(i, 1);
  }
 }

 //把Food開放出去
 window.Food = Food;
 })()
 
 //cwen測試
 // var map = document.querySelector('.map');
 // var options = { x: 20, y: 20, width: 30, height: 30, color: 'green' };
 // //todo不傳值默認(rèn)為自定義food
 // var food = new Food();
 // food.render(map);
})

3)snake對象編寫()

window.addEventListener('load', function() {
 (function() {
 //記錄蛇的每一節(jié)
 var elements = [];
 //cwen立即執(zhí)行函數(shù),開啟新的作用于,避免命名沖突
 function Snake(options) {
  options = options || {};
  //對象(蛇)每節(jié)的大小
  this.width = options.width || 20;
  this.height = options.height || 20;
  //cwen蛇的總節(jié)數(shù)(計分)
  this.mark = options.mark || 0;
  //對象的移動方向
  this.direction = options.direction || 'right';
  //對象的身體(蛇節(jié))
  this.kont = [{ x: 3, y: 2, color: 'red' }, { x: 2, y: 2, color: 'black' }, { x: 1, y: 2, color: 'black' }];
 }

 //cwen渲染對象
 Snake.prototype.render = function(map) {
  //移除之前的蛇
  remove();
  //循環(huán)輸出對象的身體(蛇節(jié))
  for (var i = 0, len = this.kont.length; i < len; i++) {
  var obj = this.kont[i];
  var div = document.createElement('div');
  map.appendChild(div);
  //將蛇節(jié)添加入數(shù)組
  elements.push(div);
  //添加樣式
  div.style.position = 'absolute';
  div.style.width = this.width + 'px';
  div.style.height = this.height + 'px';
  div.style.left = obj.x * this.width + 'px';
  div.style.top = obj.y * this.height + 'px';
  div.style.backgroundColor = obj.color;
  }
 }

 //cwen控制蛇移動的方法
 //todo傳參food,map 在game中調(diào)用move方法也要傳入相應(yīng)參數(shù)
 Snake.prototype.move = function(food, map) {
  //控制蛇節(jié)的移動(當(dāng)前蛇節(jié)到下一個蛇節(jié)的位置)
  for (var i = this.kont.length - 1; i > 0; i--) {
  this.kont[i].x = this.kont[i - 1].x;
  this.kont[i].y = this.kont[i - 1].y;
  }

  //判斷并控制蛇頭移動,判斷蛇頭移動方向
  var head = this.kont[0];
  switch (this.direction) {
  case 'right':
   head.x += 1;
   break;
  case 'left':
   head.x -= 1;
   break;
  case 'top':
   head.y -= 1;
   break;
  case 'bottom':
   head.y += 1;
   break;
  }


  //蛇頭碰到食物時處理
  // cwen判斷蛇頭是否和食物坐標(biāo)重合
  var headX = head.x * this.width;
  var headY = head.y * this.height;
  if (headX == food.x && headY == food.y) {
  //1,增加蛇節(jié)(找到最后一根蛇節(jié),然后添加給創(chuàng)建的蛇數(shù)組)
  var last = this.kont[this.kont.length - 1];
  this.kont.push({ x: last.x, y: last.y, color: last.color });
  //cwen求出蛇節(jié)的總個數(shù)(計分)
  var mark = this.mark++;
  //2,重新渲染食物
  food.render(map);
  }
 }


 //刪除之前的蛇
 function remove() {
  for (var i = elements.length - 1; i >= 0; i--) {
  elements[i].parentNode.removeChild(elements[i]);
  elements.splice(i, 1);
  }
 }

 //把Snake構(gòu)造函數(shù)暴露出去
 window.Snake = Snake;
 })()
 //測試
 // var map = document.querySelector('.map');
 // var snake = new Snake();
 // snake.render(map);
})

4)game對象編寫,其中一個為無敵版(含細(xì)節(jié)注釋)

window.addEventListener('load', function() {
 (function() {
 //改變計時器內(nèi)this指向
 var that;

 function Game(map) {
  // var options = { x: 20, y: 20, width: 30, height: 30, color: 'green' };
  this.food = new Food();
  this.snake = new Snake();
  this.map = map;
  that = this;
 }
 //cwen渲染
 Game.prototype.start = function() {
  // 1.把食物和蛇渲染到頁面
  this.food.render(this.map);
  this.snake.render(this.map);
  // 2.游戲邏輯編寫
  //讓蛇動起來
  //判斷地圖邊界
  // runSnake();
  //todo判斷玩法(兩種模式,原理一樣)
  goInput();
  //通過鍵盤控制蛇頭方向
  //! keydown();
  //蛇頭碰到食物時處理
  //在snake.js中判斷
 }

 function goInput() {
  var it = prompt('try:\n 經(jīng)典玩法請按1\n 無敵玩法請輸入(博主最帥)\n')
  if (it == 1) {
  runSnake();
  keydown();
  } else if (it == '博主最帥') {
  runSnake1();
  keydown1();
  } else {
  alert('you input could not be found!!!');
  goInput();
  }
 }


 //讓蛇動起來
 function runSnake() {
  var timeId = setInterval(function() {
  // var a = mark;
  that.snake.move(that.food, that.map);
  that.snake.render(that.map);

  //判斷地圖邊界
  var maxX = (that.map.offsetWidth) / that.snake.width;
  var maxY = (that.map.offsetHeight) / that.snake.height;
  var headX = that.snake.kont[0].x;
  var headY = that.snake.kont[0].y;
  if (headX < 0 || headX >= maxX) {
   alert('Game Over ' + '得分為 ' + that.snake.mark);
   clearInterval(timeId);
  } else if (headY < 0 || headY >= maxY) {
   alert('Game Over ' + '成績?yōu)?' + that.snake.mark);
   clearInterval(timeId);
  }
  }, 150)
 }


 //無敵版本蛇運(yùn)動
 function runSnake1() {
  var timeId1 = setInterval(function() {
  that.snake.move(that.food, that.map);
  that.snake.render(that.map);

  //判斷地圖邊界
  var maxX = (that.map.offsetWidth - that.snake.width) / that.snake.width;
  var maxY = (that.map.offsetHeight - that.snake.height) / that.snake.height;
  var headX = that.snake.kont[0].x;
  var headY = that.snake.kont[0].y;

  if (headX < 0) {
   that.snake.kont[0].x = (that.map.offsetWidth - that.snake.width) / that.snake.width;
  } else if (headX > maxX) {
   that.snake.kont[0].x = 0;
  } else if (headY < 0) {
   that.snake.kont[0].y = (that.map.offsetHeight - that.snake.height) / that.snake.height;
  } else if (headY > maxY) {
   that.snake.kont[0].y = 0;
  }
  }, 50)
 }



 //通過鍵盤控制蛇頭方向
 function keydown() {
  document.addEventListener('keydown', function(e) {
  //通過事件對象判斷按了哪個鍵 37left,38top,39right,40bottom
  // console.log(e.keyCode);
  //其在走的同時按下反方向無用
  if (e.keyCode == 37 && that.snake.direction != 'right') {
   that.snake.direction = 'left';
  } else if (e.keyCode == 38 && that.snake.direction != 'bottom') {
   that.snake.direction = 'top';
  } else if (e.keyCode == 39 && that.snake.direction != 'left') {
   that.snake.direction = 'right';
  } else if (e.keyCode == 40 && that.snake.direction != 'top') {
   that.snake.direction = 'bottom';
  }
  });
 }


 function keydown1() {
  document.addEventListener('keydown', function(e) {
  //通過事件對象判斷按了哪個鍵 37left,38top,39right,40bottom
  // console.log(e.keyCode);
  //無敵版本四面八方任你行
  if (e.keyCode == 37) {
   that.snake.direction = 'left';
  } else if (e.keyCode == 38) {
   that.snake.direction = 'top';
  } else if (e.keyCode == 39) {
   that.snake.direction = 'right';
  } else if (e.keyCode == 40) {
   that.snake.direction = 'bottom';
  }
  });
 }


 //把Game開放
 window.Game = Game;
 })()
})

5)main開啟游戲

window.addEventListener('load', function() {
 (function(window, undefind) {
 //測試
 var map = document.querySelector('.map');
 var game = new Game(map);
 game.start();

 })(window, undefined)
})

last but not least
*建議把所有js文件寫在同一個js文件中,可以大大提高加載速度。注意在每個立即執(zhí)行函數(shù)前加上‘ ;',以免出錯。

看完上述內(nèi)容,是不是對javascript實現(xiàn)貪吃蛇經(jīng)典游戲的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)站欄目:javascript實現(xiàn)貪吃蛇經(jīng)典游戲的方法
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/ihpgsd.html