![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
相关问题
CODE: <html> <body style='background-color:#d4d0c8'> <script> window.onload = initGame; var snake; var snakeLength = 3; //蛇的初始长度 var life = 3; //生命 var currentMession = 1; //初始关数 var datagrid = new Array(); var gridWidth = 11; var gridHeight = 16; var foodPerMession = 15; //每局要吃掉的食物数量 var posX; var posY; var grade = 0; ///////////////////////////// var bgcolor = "#D9F6DC"; //背景色 var gridcolor = "#D9F6DC"; //网格颜色 var foodcolor = "#C44D85"; //食物颜色 var snakecolor = "#68A6F9"; //蛇的颜色 var gameRun; var gameStatus; var snakeDirection; var direction = 40; var st; function initGame() { createEnvironment(11,16); } //----------------------------- //-- 画表格,和游戏部件 function createEnvironment(width,height) { oGame = document.createElement("TABLE"); oD = oGame.insertRow().insertCell(); oD.id = "game"; oGame.align = "center"; document.body.appendChild(oGame); for(var n=0;n<4;n++) { var gameInfo = document.createElement("DIV"); gameInfo.style.padding = "0px 5px 1px 5px"; gameInfo.id = "gameInfo".concat(n); gameInfo.style.font = "11.5px Verdana,Tahoma"; gameInfo.style.pixelHeight = 16; document.getElementById("game").appendChild(gameInfo); } oGrid = document.createElement("TABLE"); oGrid.cellPadding = 0; oGrid.cellSpacing = 1; oGrid.align = "center"; oGrid.style.border = "1px solid"; oGrid.style.borderColor= "#666666 #ffffff #ffffff #666666"; oGrid.bgColor = gridcolor; for(var i = 0;i<height;i++) { var or = oGrid.insertRow(); or.style.backgroundColor=bgcolor; var rows = new Array(); for(var c=0;c<width;c++) { var oc = or.insertCell(); oc.style.font="11.5px Verdana"; oc.style.pixelWidth = 15; oc.style.pixelHeight = 15; oc.setAttribute("position",i+":"+c); rows.push(oc); } datagrid.push(rows); } document.getElementById("game").appendChild(oGrid); startBnt = document.createElement("INPUT"); startBnt.type = "BUTTON"; startBnt.id = "startbutton"; startBnt.align = "center"; startBnt.style.width = "100px"; startBnt.value = "start"; startBnt.style.font ="bold 11.5px Verdana"; startBnt.attachEvent("onclick",startGame); document.getElementById("game").appendChild(document.createElement("BR")); document.getElementById("game").appendChild(startBnt); startBnt.focus(); } //游戏初始化,并进入游戏循环 function startGame() { clearTimeout(st); for(var i=0;i<datagrid.length;i++) { for(var j=0;j<datagrid[i].length;j++) { datagrid[i][j].style.backgroundColor = bgcolor; } } startbutton.disabled = true; snake = new Array(snakeLength); ////////////////////////////////////////////////////////////// posY = -1; posX = (datagrid[0].length - 1) / 2; grade = 0; snakeDirection = direction = 40; gameStatus = true; document.getElementById("gameInfo0").innerHTML = "Mession: <B style='color:blue'>" + currentMession + "</B>"; document.getElementById("gameInfo1").innerHTML = "Game Score: <B style='color:"+foodcolor+"'>"+ grade +"</B>"; document.getElementById("gameInfo2").innerHTML = getLife(false); document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Ready.</B>"; alert("Mession "+currentMession); addFood(); runGame(); } //游戏暂停 function pause() { if(gameRun == false)return; gameStatus = false; document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Pause.</B>"; } //游戏暂停后,重新开始 function restart() { if(gameRun == false)return; gameStatus = true; document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Starting.</B>"; } //游戏循环 function runGame() { gameRun = true; var foodEated = false; if(!gameStatus) { setTimeout("runGame()",500); return; } var oPosX = posX; var oPosY = posY; snakeDirection = direction; switch(snakeDirection) { case 37: posX -= 1; break; case 38: posY -= 1; break; case 39: posX += 1; break; case 40: posY += 1; break; } try { document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Starting.</B>"; if(datagrid[posY] == undefined||datagrid[posY][posX] == undefined) { throw false; } // 如果吃到的食物 if(datagrid[posY][posX].style.backgroundColor.toUpperCase() == foodcolor.toUpperCase()) { document.getElementById("gameInfo1").innerHTML = "Game Score: <B style='color:"+foodcolor+"'>"+(grade + 1)+"<B>"; if(++grade >= foodPerMession) { currentMession++; gameRun = null; document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Success!</B>"; startbutton.value = "Next Mession"; startbutton.disabled = false; startbutton.focus(); } else { foodEated = true; } } else { try { //弹出尾巴 snake.shift().style.backgroundColor = bgcolor; } catch(ex) {} } if(datagrid[posY][posX].style.backgroundColor.toUpperCase() == snakecolor.toUpperCase()) { throw false; } datagrid[posY][posX].style.backgroundColor = snakecolor; snake.push(datagrid[posY][posX]); if(foodEated)addFood(); } catch(ex) { gameRun = ex; } finally { if(gameRun == true) { stime = 120 - (5 * currentMession); if(stime < 30)stime = 30; st = setTimeout("runGame()",stime); } else { if(gameRun == false) { document.getElementById("gameInfo2").innerHTML = getLife(true); if(--life <= 0) { life = 3; currentMession = 1; document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Game Over.</B>"; startbutton.value = "Start"; alert("Game Over."); } else { document.getElementById("gameInfo3").innerHTML = "Game Status: <B>Game Failed.</B>"; startbutton.value = "Continue"; } startbutton.disabled = false; startbutton.focus(); } } } } //在游戏面上添加一个食物 function addFood() { x = Math.floor(gridWidth * Math.random()); y = Math.floor(gridHeight * Math.random()); while(datagrid[y][x].style.backgroundColor.toUpperCase() == snakecolor.toUpperCase()) { x = Math.floor(gridWidth * Math.random()); y = Math.floor(gridHeight * Math.random()); } datagrid[y][x].style.backgroundColor = foodcolor; } //返回玩家的游戏次数 function getLife(boolValue) { var lifeInfo = "Life: <b style='color:"+snakecolor+"'>"; l=(boolValue)? 1:0; for(;l<life;l++) { lifeInfo += "■"; } lifeInfo +="</B>"; return lifeInfo; } function document.onkeydown() { switch (event.keyCode) { case 37: if(snakeDirection != 39)direction = 37; break; case 38: if(snakeDirection != 40)direction = 38; break; case 39: if(snakeDirection != 37)direction = 39; break; case 40: if(snakeDirection != 38)direction = 40; break; case 19: // 小键盘的Pause键 if(gameStatus)pause();else restart(); break; default:break; } } /* 1判定是否在范围内 | \ no------------>异常 ^ 2判定是否吃到东西 | /\ | / \ | yes/ \ no | >规定数量? 弹出尾巴 | | | yes/ \ no | next addFood() | mession | | 3判断是否为蛇的颜色 | | | \yes__________________ */ </script> </body> </html> |
提问者:longhorn 08-15 09:09
答复

