<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var a = new Array();
//N为格子边长
N = 10;
curI = 0;
curJ = 0;
//障碍物测试,可转食物
boomIJ = 100;
var curTd;
var curIJ = 2;
var body = new Array();
function move1(curI, curJ) {
//alert(curI+" "+curJ+" "+boomIJ)
//上版这里触雷游戏结束
// if (curI * N + curJ == boomIJ) {
// alert("game over!");
// return;
// }
//此版改为吃到食物身体加长且先进行判断
if (curI * N + curJ == boomIJ) {
//扩充身子的位置
body.push(boomIJ)
//return;
//吃完长身子后再次出现新食物
rdmBoom(null);
}
//将蛇尾颜色置空
document.getElementById("td" + (body[body.length - 1] + 1)).style.background = "";
for (var i = body.length - 1; i >= 0; i--) {
//身子存的是位置
body[i] = body[i - 1];
}
body[0] = curI * N + curJ;
//重构身子
for (var i = 0; i < body.length; i++) {
var idNum = body[i] + 1;
curTd = document.getElementById(("td" + idNum));
curTd.style.background = "green";
}
}
function init() {
//初始化活动场地
for (var i = 0; i < N; i++) {
a[i] = new Array();
for (var j = 0; j < N; j++) {
a[i][j] = 0;
}
}
var table = document.getElementById("table1");
var n = N;
for (var i = 0; i < n; i++) {
var tr = document.createElement("tr");
for (var j = 0; j < n; j++) {
var td = document.createElement("td");
td.setAttribute("id", "td" + (i * n + j + 1));
td.addEventListener("click", function() {
check(this);
});
td.style.background = "";
td.innerHTML = "" + (i * n + j + 1);
tr.appendChild(td);
}
table.appendChild(tr);
}
// var num = Math.floor(Math.random() * 9) + 1;
// var idNum = "td" + num;
// curTd = document.getElementById(idNum);
var idNum = "td" + (curIJ + 1);
curTd = document.getElementById(idNum);
curTd.style.background = "green";
curI = parseInt(curIJ / N);
curJ = parseInt(curIJ % N);
body.push(2);
body.push(1);
body.push(0);
for (var i = body.length - 1; i >= 0; i--) {
var tdnum = "td" + (body[i] + 1);
//alert(document.getElementById(tdnum).innerHTML);
document.getElementById(tdnum).style.background = "green";
}
//初始随机食物
rdmBoom(null);
}
function check(td) {
//boomIJ=parseInt(td.parentElement.rowIndex)*N+parseInt(td.cellIndex));
i = parseInt(td.parentElement.rowIndex);
j = parseInt(td.cellIndex);
boomIJ = i * N + j;
td.style.background = "red";
//alert(boomIJ);
}
//将上版手动设置食物改为随机出现
function rdmBoom(td){
//var okFood=True
while(true){
var num = Math.floor(Math.random() * N*N) +1;
var i=0;
for(;i<body.length;i++){
if(body[i]==num){
break;
}
}
if(i==body.length){
boomIJ = num;
var tdnum = "td" + (boomIJ+1);
//alert(document.getElementById(tdnum).innerHTML);
document.getElementById(tdnum).style.background = "red";
break;
}
}
}
function turn(event) {
var e = event;//|| window.event || arguments.callee.caller.arguments[0];
//alert(e.keyCode);
if (e && e.keyCode == 38) {
if (curI - 1 >= 0) {
curI = curI - 1;
move1(curI, curJ);
}
//alert('38=上键');
}
if (e.keyCode == 39) {
// alert('39=右键');
if (curJ + 1 < N) {
curJ = curJ + 1;
move1(curI, curJ);
}
}
if (e.keyCode == 37) {
//alert('37=左键');
if (curJ - 1 >= 0) {
curJ = curJ - 1;
move1(curI, curJ);
}
}
if (e && e.keyCode == 40) {
//alert('40=下键');
if (curI + 1 < N) {
curI = curI + 1;
move1(curI, curJ);
}
}
}
window.onload = init;
</script>
</head>
<body οnkeydοwn="turn(event)">
<table id="table1" style="width: 50%; height: 300px;" border="1">
</table>
</body>
</html>