亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

javascript實現(xiàn)貪吃蛇代碼

時間:2020-11-17 18:21:32 JavaScript 我要投稿

javascript實現(xiàn)貪吃蛇代碼

  在習作的過程中嘗試著貪吃蛇游戲用JS實現(xiàn)了。竟然成功了。

  思路:使用10px*10px的div層擔當“像素”,然后使用40*40矩陣160個“像素”構成了游戲的界面。

  下面是代碼:

  復制代碼 代碼如下:

  // JavaScript Document

  alert("鍵盤的方向鍵控制方向,空格鍵暫停。nLIFE制作nhttp://blog.csdn.net/anhulife");

  // 添加基本的圖形塊,即160個10 * 10的層組成的二維矩陣

  var rowindex = new Array(40);

  var colindex;

  var cell;

  // 圖像單元的定義

  var backcolor = "black";

  for(var i = 0; i < 40; i ++ )

  {

  colindex = new Array(40);

  for(var j = 0; j < 40; j ++ )

  {

  // 設置每個單元的屬性

  cell = document.createElement("div");

  cell.style.backgroundColor = backcolor;

  cell.style.width = "10px";

  cell.style.height = "10px";

  cell.style.position = "absolute";

  cell.style.left = "" + (j * 10 + 100) + "px";

  cell.style.top = "" + (i * 10 + 100) + "px";

  cell.style.overflow = "hidden";

  // 添加單元

  document.body.appendChild(cell);

  // 填充列組

  colindex[j] = cell;

  }

  // 填充行組

  rowindex[i] = colindex;

  }

  // 貪吃蛇類的.定義,基于基本的圖像單元

  function snake()

  {

  // 定義蛇的身體,并初始化

  this.bodycolor = "white";

  this.bodys = new Array();

  for(var i = 20; i < 25; i ++ )

  {

  rowindex[20][i].style.backgroundColor = this.bodycolor;

  // rowindex的第一個坐標是行標,第二是列標

  this.bodys.push(rowindex[20][i]);

  }

  // 定義蛇的頭部坐標,第一個是行標, 第二個是列標

  this.head = [20, 20];

  // 定義蛇的前進方向,0代表左、1代表下、2代表右、3代表上

  this.direct = 0;

  }

  // 移動模塊

  function move()

  {

  // 根據(jù)前進方向計算頭部的坐標

  switch(this.direct)

  {

  case 0 :

  this.head[1] -= 1;

  break;

  case 1 :

  this.head[0] += 1;

  break;

  case 2 :

  this.head[1] += 1;

  break;

  case 3 :

  this.head[0] -= 1;

  break;

  }

  // 判斷是否越界

  if(this.head[0] < 0 || this.head[0] > 39 || this.head[1] < 0 || this.head[1] > 39)

  {

  // 如果越界則返回false

  return false;

  }

  else

  // 如果沒有越界則檢查下一個元素的性質,如果是食物則吃掉,并再生成食物。如果是其自身則返回false

  if(this.head[0] == food[0] && this.head[1] == food[1])

  {

  // 如果是食物

  rowindex[this.head[0]][this.head[1]].style.backgroundColor = this.bodycolor;

  this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);

  score++;

  makefood();

  return true;

  }

  else

  // 如果是它自身

  if(rowindex[this.head[0]][this.head[1]].style.backgroundColor == this.bodycolor)

  {

  if(rowindex[this.head[0]][this.head[1]] == this.bodys.pop())// 如果是它的尾部

  {

  this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);

  return true;

  }

  // 如果不是尾部

  return false;

  }

  // 以上情況都不是

  this.bodys.pop().style.backgroundColor = backcolor;

  rowindex[this.head[0]][this.head[1]].style.backgroundColor = this.bodycolor;

  this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);

  return true;

  }

  snake.prototype.move = move;

  // 生成食物模塊

  var foodcolor = "blue";

  var food = [20, 17];

  rowindex[food[0]][food[1]].style.backgroundColor = foodcolor;

  function makefood()

  {

  var tempfood;

  var tempelement;

  out :

  while(true)

  {

  tempfood = [Math.round(Math.random() * 39), Math.round(Math.random() * 39)];

  tempelement = rowindex[tempfood[0]][tempfood[1]];

  for(var i in s.bodys)

  {

  if(s.bodys[i] == tempelement)

  {

  // 如果隨機生成的食物在蛇的身體上,則跳出繼續(xù)

  continue out;

  }

  // 生成食物成功

  break out;

  }

  }

  food = tempfood;

  rowindex[food[0]][food[1]].style.backgroundColor = foodcolor;

  }

  // 轉向模塊和暫停模塊

  document.onkeydown = turnorstop;

  function turnorstop(event)

  {

  if(window.event != undefined)

  {

  if(parseInt(window.event.keyCode)==32)

  {

  alert("休息一下");

  }

  else

  {

  switch(parseInt(window.event.keyCode))

  {

  case 37 :

  if(s.direct!=2)

  s.direct = 0;

  break;

  case 38 :

  if(s.direct!=1)

  s.direct = 3;

  break;

  case 39 :

  if(s.direct!=0)

  s.direct = 2;

  break;

  case 40 :

  if(s.direct!=3)

  s.direct = 1;

  break;

  }

  }

  }

  else

  {

  if(parseInt(event.which)==32)

  {

  alert("休息一下");

  }

  else

  {

  switch(parseInt(event.which))

  {

  case 37 :

  if(s.direct!=2)

  s.direct = 0;

  break;

  case 38 :

  if(s.direct!=1)

  s.direct = 3;

  break;

  case 39 :

  if(s.direct!=0)

  s.direct = 2;

  break;

  case 40 :

  if(s.direct!=3)

  s.direct = 1;

  break;

  }

  }

  }

  }

  // 啟動游戲模塊

  var s = new snake();

  var time = 60;//蛇的速度指數(shù)

  function startmove()

  {

  if(s.move())

  {

  setTimeout(startmove, time);

  }

  else

  {

  alert("GAME OVERn您的分數(shù)是:"+score+"分");

  }

  }

  //分數(shù)設置

  var score = -1;

  //運行游戲

  startmove();

  在網頁中連接該JS文件即可。