溫馨提示×

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

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

教你使用原生JavaScrip制作一個(gè)貪吃蛇游戲

發(fā)布時(shí)間:2020-11-05 15:11:56 來(lái)源:億速云 閱讀:149 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)教你使用原生JavaScrip制作一個(gè)貪吃蛇游戲,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

1.HTML部分,東西很少

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>貪吃蛇</title>
 <style>
 *{
  margin: 0px;
  padding: 0px;
 }
 td{
  border-radius: 30%;
 }
 </style>
 
</head>
<body>
 <script type="text/javascript" src="./snake.js"></script>
</body>
</html>

2.JavaScript部分

var arrayBox = new Array(); // 存放單元格
var arraySnake = new Array(); // 存放蛇 
var food; // 食物
var snakeHead; // 蛇頭
var key = true; // 判斷頁(yè)面是否需要初始化
var timekey; // 運(yùn)動(dòng)定時(shí)器常量

function newGame() {
 bgInit();
 arrayBoxInit();
 initSnake();
 randomFood();
}

newGame();
document.onclick = function () {
 if (key) {
  gameStart(arraySnake);
  key = false;
 }
}



// 記錄的單元格數(shù)組初始化
function arrayBoxInit() {
 for (var y = 0; y < 20; y++) {
  arrayBox[y] = new Array();
  for (var x = 0; x < 20; x++) {
   arrayBox[y][x] = 0;
  }
 }
}

// 背景初始化
function bgInit() {
 var table = document.createElement("table");
 table.style = "border-spacing:0px; border:1px solid #3c3c3c;margin:10% auto";
 for (var y = 0; y < 20; y++) {
  var tr = document.createElement("tr");
  for (var x = 0; x < 20; x++) {
   var td = "<td class='box-" + y + "-" + x + "' style='width: 20px; height: 20px;'></td>";
   tr.innerHTML += td;
  }
  table.appendChild(tr);
 }
 document.body.insertBefore(table, document.getElementsByTagName("script")[0]);
}

// 初始化蛇
function initSnake() {
 var x = Math.floor(Math.random() * 20);
 var y = Math.floor(Math.random() * 20);
 var initA = document.getElementsByClassName("box-" + y + "-" + x)[0];
 snakeHead = initA; //蛇頭
 var b = initBFn(x, y);
 var initB = document.getElementsByClassName(b)[0];
 arraySnake.push(initB);
 arraySnake.push(initA);
 initA.style.backgroundColor = "#9c9c9c";
 initB.style.backgroundColor = "#9c9c9c";
 arrayBox[y][x] = 1;
 arrayBox[b.split("-")[1]][b.split("-")[2]] = 1;
}

// 初始化蛇身
function initBFn(x, y) {
 if (x != 19 && x != 0) {
  if (y != 19 && y != 0) {
   if (Math.random() > 0.5) {
    if (Math.random() > 0.5) {
     return "box-" + y + "-" + (x + 1);
    } else {
     return "box-" + y + "-" + (x - 1);
    }
   } else {
    if (Math.random() > 0.5) {
     return "box-" + (y + 1) + "-" + x;
    } else {
     return "box-" + (y - 1) + "-" + x;
    }
   }
  } else if (y == 0) {
   if (Math.random() > 0.5) {
    return "box-0-" + (x + 1);
   } else {
    return "box-0-" + (x - 1);
   }
  } else if (y == 19) {
   if (Math.random() > 0.5) {
    return "box-19-" + (x + 1);
   } else {
    return "box-19-" + (x - 1);
   }
  }
 } else if (x == 0) {
  if (y != 19 && y != 0) {
   if (Math.random() > 0.5) {
    return "box-" + (y + 1) + "-0";
   } else {
    return "box-" + (y - 1) + "-0";
   }
  } else if (y == 0) {
   if (Math.random() > 0.5) {
    return "box-1-0";
   } else {
    return "box-0-1";
   }
  } else if (y == 19) {
   if (Math.random() > 0.5) {
    return "box-18-0";
   } else {
    return "box-19-1";
   }
  }
 } else if (x == 19) {
  if (y != 19 && y != 0) {
   if (Math.random() > 0.5) {
    return "box-" + (y + 1) + "-19";

   } else {
    return "box-" + (y - 1) + "-19";
   }
  } else if (y == 0) {
   if (Math.random() > 0.5) {
    return "box-1-19";
   } else {
    return "box-0-18";
   }
  } else if (y == 19) {
   if (Math.random() > 0.5) {
    return "box-18-19";
   } else {
    return "box-19-18";
   }
  }
 }
}

// 隨機(jī)產(chǎn)生食物食物
function randomFood() {
 var x = Math.floor(Math.random() * 20);
 var y = Math.floor(Math.random() * 20);
 if (!arrayBox[y][x]) {
  document.getElementsByClassName("box-" + y + "-" + x)[0].style = "background-color:#9c9c9c;border-radius:50%";
  arrayBox[y][x] = 1;
  food = document.getElementsByClassName("box-" + y + "-" + x)[0];
 } else {
  addSnakeLength();
 }
}

// 開(kāi)始游戲
function gameStart(arraySnake) {
 var Ax = arraySnake[1].className.split("-")[2];
 var Ay = arraySnake[1].className.split("-")[1];
 var Bx = arraySnake[0].className.split("-")[2];
 var By = arraySnake[0].className.split("-")[1];
 if (Ay == By) {
  if (Ax > Bx) {
   moveRight();
  } else {
   moveLeft()
  }
 } else {
  if (Ay > By) {
   moveDown()
  } else {
   moveUp()
  }
 }
}


// 初始化運(yùn)動(dòng),(方向:右左上下)
function moveRight() {
 timekey = setInterval(function () {
  var nextBox = document.getElementsByClassName("box-" + arraySnake[arraySnake.length - 1].className.split("-")[1] + "-" + (parseInt(arraySnake[arraySnake.length - 1].className.split("-")[2]) + 1))[0];
  if (nextBox) {
   arrayBox[arraySnake[arraySnake.length - 1].className.split("-")[1]][(parseInt(arraySnake[arraySnake.length - 1].className.split("-")[2]) + 1)] = 1;
   nextBox.style.backgroundColor = "#9c9c9c";
   arraySnake.push(nextBox);
   arrayBox[arraySnake[0].className.split("-")[1]][arraySnake[0].className.split("-")[2]] = 0;
   arraySnake[0].style.backgroundColor = "#fff";
   arraySnake.shift();
   eatFood();
  }
 }, 200);
}

function moveLeft() {
 timekey = setInterval(function () {
  var nextBox = document.getElementsByClassName("box-" + arraySnake[arraySnake.length - 1].className.split("-")[1] + "-" + (parseInt(arraySnake[arraySnake.length - 1].className.split("-")[2]) - 1))[0];
  if (nextBox) {
   arrayBox[arraySnake[arraySnake.length - 1].className.split("-")[1]][(parseInt(arraySnake[arraySnake.length - 1].className.split("-")[2]) - 1)] = 1;
   nextBox.style.backgroundColor = "#9c9c9c";
   arraySnake.push(nextBox);
   arrayBox[arraySnake[0].className.split("-")[1]][arraySnake[0].className.split("-")[2]] = 0;
   arraySnake[0].style.backgroundColor = "#fff";
   arraySnake.shift();
   eatFood();
  }
 }, 200);
}

function moveUp() {
 timekey = setInterval(function () {
  var nextBox = document.getElementsByClassName("box-" + (parseInt(arraySnake[arraySnake.length - 1].className.split("-")[1]) - 1) + "-" + arraySnake[arraySnake.length - 1].className.split("-")[2])[0];
  if (nextBox) {
   arrayBox[(parseInt(arraySnake[arraySnake.length - 1].className.split("-")[1]) - 1)][arraySnake[arraySnake.length - 1].className.split("-")[2]] = 1;
   nextBox.style.backgroundColor = "#9c9c9c";
   arraySnake.push(nextBox);
   arrayBox[arraySnake[0].className.split("-")[1]][arraySnake[0].className.split("-")[2]] = 0;
   arraySnake[0].style.backgroundColor = "#fff";
   arraySnake.shift();
   eatFood();
  }
 }, 200);
}

function moveDown() {
 timekey = setInterval(function () {
  var nextBox = document.getElementsByClassName("box-" + (parseInt(arraySnake[arraySnake.length - 1].className.split("-")[1]) + 1) + "-" + arraySnake[arraySnake.length - 1].className.split("-")[2])[0];
  if (nextBox) {
   arrayBox[(parseInt(arraySnake[arraySnake.length - 1].className.split("-")[1]) + 1)][arraySnake[arraySnake.length - 1].className.split("-")[2]] = 1;
   nextBox.style.backgroundColor = "#9c9c9c";
   arraySnake.push(nextBox);
   arrayBox[arraySnake[0].className.split("-")[1]][arraySnake[0].className.split("-")[2]] = 0;
   arraySnake[0].style.backgroundColor = "#fff";
   arraySnake.shift();
   eatFood();
  }
 }, 200);
}

// 鍵盤操作拐彎
document.addEventListener("keydown", function (e) {
 if (e.code == "ArrowDown") {
  turnDown();
 } else if (e.code == "ArrowUp") {
  turnUp();
 } else if (e.code == "ArrowLeft") {
  turnLeft();
 } else if (e.code == "ArrowRight") {
  turnRight();
 }
}, false);

// 下拐
function turnDown() {
 if (arraySnake[arraySnake.length - 1].className.split("-")[1] == arraySnake[arraySnake.length - 2].className.split("-")[1]) {
  clearInterval(timekey);
  moveDown();
 }
}
// 下拐
function turnUp() {
 if (arraySnake[arraySnake.length - 1].className.split("-")[1] == arraySnake[arraySnake.length - 2].className.split("-")[1]) {
  clearInterval(timekey);
  moveUp();
 }
}
// 左拐
function turnLeft() {
 if (arraySnake[arraySnake.length - 1].className.split("-")[2] == arraySnake[arraySnake.length - 2].className.split("-")[2]) {
  clearInterval(timekey);
  moveLeft();
 }
}
// 右拐
function turnRight() {
 if (arraySnake[arraySnake.length - 1].className.split("-")[2] == arraySnake[arraySnake.length - 2].className.split("-")[2]) {
  clearInterval(timekey);
  moveRight();
 }
}
// 蛇吃食物
function eatFood() {
 var temp = food;
 if (arrayBox[food.className.split("-")[1]][food.className.split("-")[2]] == 0) {
  randomFood();
  addSnakeLength(temp);
 }
}
// 增加長(zhǎng)度在蛇尾
function addSnakeLength(temp) {
 arraySnake.unshift(temp);
}

以上就是教你使用原生JavaScrip制作一個(gè)貪吃蛇游戲,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

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

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

AI