var widthNum = 20;
var heightNum = 15;
var nodLength = 30;
var snake;
var food = { };
var obstacle;
var speed = 0;
var bombNum = 0;
var timer;
var next = { };
var score = 0;
function initSnakePoint(x, y) {
truevar div = document.createElement("img");
truediv.src = "./apple_snake.png";
truediv.className = "snakePoint";
truediv.style.top = y + "px";
truediv.style.left = x + "px";
trueId("Box").appendChild(div);
truereturn div;
}
function createFood() {
truevar foodXY = randomFoodXY();
truevar div = document.createElement("img");
truediv.src = "./apple_snake.png";
truefood.x = foodXY[0];
truefood.y = foodXY[1];
truefood.div = div;
truediv.className = "foodPoint";
truediv.setAttribute("id", "snakePoint");
truediv.style.left = foodXY[0] * nodLength + "px";
truediv.style.top = foodXY[1] * nodLength + "px";
trueId("Box").appendChild(div);
}
function createObstacle() {
truevar num = document.getElementsByName("bombNum");
truefor (var i = 0; i < num.length; i++) {
truetrueif (num[i].checked) {
truetruetruebombNum = num[i].value;
truetruetruebreak;
truetrue}
true}
truevar obstacleNum = bombNum;
truefor (var i = 0; i < obstacleNum; i++) {
truetrueobstacle[i] = {};
truetruevar obstacleXY = randomObstacleXY();
truetrueobstacle[i].x = obstacleXY[0];
truetrueobstacle[i].y = obstacleXY[1];
truetruevar div = document.createElement("img");
truetruediv.src = "./bomb.jpg";
truetrueobstacle[i].div = div;
truetruediv.className = "obstaclePoint";
truetruediv.id = "obstaclePoint";
truetruediv.style.left = obstacleXY[0] * nodLength + "px";
truetruediv.style.top = obstacleXY[1] * nodLength + "px";
truetrueId("Box").appendChild(div);
true}
}
function initGame() {
trueId("start").blur();
truesnake = [];
trueobstacle = [];
truesnake[0] = {};
truefood = {};
truescore = 0;
trueId("start").disabled = true;
trueId("pause").disabled = false;
trueId("reStart").style.display = ""
truevar level = document.getElementsByName("level");
truefor (var i = 0; i < level.length; i++) {
truetrueif (level[i].checked) {
truetruetruespeed = level[i].value;
truetruetruebreak;
truetrue}
true}
trueId("showSpeed").innerHTML = speed;
snake.push(new snakePoint(randomFoodXY()[0], randomFoodXY()[1]));
truetimer = window.setInterval(run, speed);
truecreateObstacle();
truecreateFood();
truelistenKey();
}
function run() {
truevar len = snake.length;
truesnake[len - 1].direction = snake[1].direction;
trueswitch (snake[1].direction) {
truetruecase 0:
truetruetruenext.y = snake[1].y - 1;
truetruetruenext.x = snake[1].x;
truetruetruejudge(0);
truetruetruebreak;
truetruecase 1:
truetruetruenext.y = snake[1].y + 1;
truetruetruenext.x = snake[1].x;
truetruetruejudge(1);
truetruetruebreak;
truetruecase 2:
truetruetruenext.y = snake[1].y;
truetruetruenext.x = snake[1].x - 1;
truetruetruejudge(2);
truetruetruebreak;
truetruecase 3:
truetruetruenext.y = snake[1].y;
truetruetruenext.x = snake[1].x + 1;
truetruetruejudge(3);
truetruetruebreak;
true}
truekey = true;
}
function judge(direction) {
truesnakeHead = snake[1];
truevar through;
truevar mode = document.getElementsByName("through");
truefor (var i = 0; i < mode.length; i++) {
truetrueif (mode[i].checked) {
truetruetruethrough = mode[i].value;
truetruetruebreak;
truetrue}
true}
trueif (through == "yes") {
truetrueif (next.y == -1) {
truetruetruenext.y = 15;
truetrue} else if (next.y == 16) {
truetruetruenext.y = 0;
truetrue} else if (next.x == -1) {
truetruetruenext.x = 20;
truetrue} else if (next.x == 21) {
truetruetruenext.x = 0;
truetrue}
true} else if (through == "no") {
truetrueif (next.y == -1) {
truetruetruegameOver();
truetruetruereturn false;
truetrue} else if (next.y == 16) {
truetruetruegameOver();
truetruetruereturn false;
truetrue} else if (next.x == -1) {
truetruetruegameOver();
truetruetruereturn false;
truetrue} else if (next.x == 21) {
truetruetruegameOver();
truetruetruereturn false;
truetrue}
true}
truefor (var len = obstacle.length, i = 0; i < len - 1; i++) {
truetrueif (next.x == obstacle[i].x && next.y == obstacle[i].y) {
truetruetruetruegameOver();
truetruetruetruereturn false;
truetruetrue}
true}
trueif (next.x == food.x && next.y == food.y) {
truetrueswitch (direction) {
truetruetruecase 0: snakeHead.y = snakeHead.y + 1; break;
truetruetruecase 1: snakeHead.y = snakeHead.y - 1; break;
truetruetruecase 2: snakeHead.x = snakeHead.x + 1; break;
truetruetruecase 3: snakeHead.x = snakeHead.x - 1; break;
truetrue}
truetrueeatFood();
true} else {
truetruevar len = snake.length;
truetruefor (var i = 4; i < len -1; i++) {
if (next.x == snake[i].x && next.y == snake[i].y) {
gameOver();
return false;
}
}
snake[len - 1].x = next.x;
snake[len - 1].y = next.y;
snake[0] = snake[len - 1];
snake.pop(snake[len - 1]);
rank();
change(snake[1]);
}
}
function rank() {
for (var len = snake.length; len > 0; len--) {
truetruesnake[len] = snake[len - 1];
true}
truesnake[0] = { };
}
function change(point) {
truepoint.div.style.left = point.x * nodLength + "px";
truepoint.div.style.top = point.y * nodLength + "px";
}
function snakePoint(x, y, div) {
truethis.x = x;
truethis.y = y;
truethis.direction = parseInt(Math.random() * 4);
truethis.div = div || initSnakePoint(this.x * nodLength, this.y * nodLength);
}
function randomFoodXY() {
truevar isRepeat = true;
truewhile (isRepeat) {
truetrueresult = [parseInt(Math.random() * widthNum), parseInt(Math.random() * heightNum)];
truetrueisRepeat = false;
truetruefor (var len = snake.length, i = 1; i < len - 1; i++) {
truetruetrueif (snake[i].x == result[0] && snake[i].y == result[1]) {
truetruetruetrueisRepeat = true;
truetruetruetruebreak;
truetruetrue}
truetrue}
truetrueif (!isRepeat) {
truetruetruefor (var len = obstacle.length, i = 0; i < len - 1; i++) {
truetruetruetrueif (result[0] == obstacle[i].x && result[1] == obstacle[i].y) {
truetruetruetruetrueisRepeat = true;
truetruetruetruetruebreak;
truetruetruetrue}
truetruetrue}
truetrue}
true}
truereturn result;
}
function randomObstacleXY() {
truevar isRepeat = true;
truewhile (isRepeat) {
truetrueresult = [parseInt(Math.random() * widthNum), parseInt(Math.random() * heightNum)];
truetrueisRepeat = false;
truetruefor (var len = snake.length, i = 1; i < len - 1; i++) {
truetruetrueif (snake[i].x == result[0] && snake[i].y == result[1]) {
truetruetruetrueisRepeat = true;
truetruetruetruebreak;
truetruetrue}
truetrue}
true}
truereturn result;
}
function listenKey() {
truedocument.onkeydown = keyEvent;
}
var key = true;
function keyEvent(e) {
trueif (key == true) {
truetruekey = false;
truetruee = e || window.event;
truetruevar keycode = e.which ? e.which :e.keyCode;
truetrueswitch (e.keyCode) {
truetruetruecase 37: if (snake[1].direction != 3) snake[1].direction = 2; break;
truetruetruecase 38: if (snake[1].direction != 1) snake[1].direction = 0; break;
truetruetruecase 39: if (snake[1].direction != 2) snake[1].direction = 3; break;
truetruetruecase 40: if (snake[1].direction != 0) snake[1].direction = 1; break;
truetrue}
true}
}
function eatFood() {
truesnake[0] = new snakePoint(food.x, food.y, food.div);
truesnake[0].div.className = "snakePoint";
truesnake[0].direction = snake[1].direction;
trueId("snakePoint").src = "./apple_snake.png";
truerank();
truescore += 1;
trueshowScore();
truecreateFood();
}
function gameOver() {
truevar name = prompt("得分:" + score + "\u000d请输入您的名字:(中文字母数字下划线)","游客");
trueif (name) {
truetrueId("nameBox").value = name;
truetrueId("scoreBox").value = score;
Id("submitScore").click();
true} else{ }
trueclearTimeout(timer);
trueremove(food.div);
truefor (var i = 1; i < snake.length; i++) {
remove(snake[i].div);
}
}
function showScore() {
Id("score").innerHTML = score;
if (score % 10 == 0 && speed >= 10) {
truetruespeed = speed - 20;
truetrueId("showSpeed").innerHTML = speed;
truetrueclearTimeout(timer);
truetruetimer = setInterval(run, speed);
true}
}
function reInitGame() {
trueclearTimeout(timer);
trueId("pause").value = "暂停";
truevar div = document.getElementById("Box");
while (div.hasChildNodes()) {
div.removeChild(div.firstChild);
}
trueId("start").blur();
truesnake = [];
truesnake[0] = {};
truefood = {};
truescore = 0;
trueId("start").disabled = true;
trueId("pause").disabled = false;
trueId("reStart").style.display = ""
truevar level = document.getElementsByName("level");
truefor (var i = 0; i < level.length; i++) {
truetrueif (level[i].checked) {
truetruetruespeed = level[i].value;
truetruetrueId("showSpeed").innerHTML = speed;
truetruetruebreak;
truetrue}
true}
truetimer = window.setInterval(run, speed);
truesnake.push(new snakePoint(randomFoodXY()[0], randomFoodXY()[1]));
truecreateObstacle()
truecreateFood();
truelistenKey();
}
function enterSpeed() {
truevar myReg = /^[0-9]{1,4}$/;
trueif (!myReg.test(Id("speed").value)) {
truetrueId("showError").innerHTML = "0-9999";
truetrueId("showError").style.color = "red";
true} else {
truetrueclearTimeout(timer);
truetruespeed = Id("speed").value;
truetruetimer = setInterval(run, speed);
true}
}
function pause() {
trueif (Id("pause").value == "暂停") {
truetrueId("pause").value = "继续";
truetrueId("showSpeed").innerHTML = 0;
truetrueclearTimeout(timer);
true} else if (Id("pause").value == "继续") {
truetrueId("pause").value = "暂停";
truetrueId("showSpeed").innerHTML = speed;
truetruetimer = setInterval(run, speed);
true}
}
function remove(elem) {
trueif (elem) {
truetrueelem.parentNode.removeChild(elem);
true}
}
function Id(id) {
truereturn (typeof id == "string") ? document.getElementById(id) : id;
}