var width = 15; //蛇运动区域宽 var height = 15; //蛇运动区域高 va" />
当前位置:文档之家› 贪吃蛇游戏

贪吃蛇游戏

<html><head><title>贪吃蛇游戏</title><script type="text/javascript">var width = 15; //蛇运动区域宽var height = 15; //蛇运动区域高var snakeLength = 3; //蛇的初始长度var timerId ;var direction = "X++"; //蛇朝哪方向前进,初始化向右var lastMov_id = null; //蛇前进一步时,记录蛇的尾巴上次的位置var score = 0; //得分var randomEgg ; //随机产生的蛋var bgColor = "#00FFE0"; //背景色alltds = document.getElementsByTagName("td"); var snake = new Array();function drawBgColor(){for(i=0;i<alltds.length;i++){alltds[i].style.backgroundColor = bgColor;}}function drawSnake(){if(snake.length == 0){ //初始化蛇的长度for(i=0;i<snakeLength;i++){ snakeId = "0|"+i;snake[i] = snakeId;}}else{getSnakeArray();}for(i=0;i<snake.length;i++){ changeBgColor(snake[i],"red"); }if(lastMov_id != null){changeBgColor(lastMov_id,bgColor);}if(snake[snake.length-1] == randomEgg){ drawEgg();}msg.innerHTML = "得分:"+score;timerId = window.setTimeout("drawSnake()",300); }function getSnakeArray(){if(direction == ""){return;}lastMov_id = snake[0];movIds = snake[snake.length-1].split("|");movIds_Y = parseInt(movIds[0]);movId_X = parseInt(movIds[1]);eval(direction.replace("X","movId_X").replace("Y","movI ds_Y"));newLeaderId = movIds_Y+"|"+movId_X;if(isLoss(newLeaderId)){alert("哈哈,输啦~~");window.location.reload();if(newLeaderId == randomEgg){ //吃到蛋,得分+1,蛇的长度+1score ++;snake.length ++;snake[snake.length-1] = newLeaderId;lastMov_id = null;}else{ //否则蛇向前移动一步for(i=0;i<snake.length;i++){if(i == snake.length-1){snake[i] = newLeaderId;break;}snake[i] = snake[i+1];}}}function isLoss(newId){ //判断是否输了var curid = newId.split("|");cur_Y = curid[0];cur_X = curid[1];if(cur_Y < 0 || cur_X < 0){return true;}if(cur_Y > height-1 || cur_X > width-1){ return true;}for(i=0;i<snake.length-1;i++){if(newId == snake[i])return true;}return false;}function drawEgg(){while(true){ //如果产生的蛋与蛇重合,则从新产生randomEgg = getRandomNum(0,height)+"|"+getRandomNum(0,width);if(contains(randomEgg) == false){break;}}changeBgColor(randomEgg,"blue");}function contains(con){for(i=0;i<snake.length;i++){if(snake[i] == randomEgg)return true;}return false;}function getRandomNum(min,max){var randomNum = parseInt(Math.random()*(max-min)+min);return randomNum;}function changeBgColor(obj,color){if(typeof(obj) == "string"){obj = document.getElementById(obj); }try{obj.style.backgroundColor = color;}catch(e){}}function keyEvent(){oldDirection = direction;var keyNum = event.keyCode;if(keyNum == 87){ //wdirection = "Y--";}else if(keyNum == 83){ //sdirection = "Y++";}else if(keyNum == 65){ //adirection = "X--";}else if(keyNum == 68){ //ddirection = "X++";}if(oldDirection.substr(0,1) == direction.substr(0,1) && oldDirection.substr(1,2) != direction.substr(1,2)){direction = oldDirection; //不能向后退}}function startGame(){drawEgg();drawSnake();startBtn.disabled = true;}</script></head><body><script type="text/javascript">document.write("<table border=solid>");for(i=0;i<height;i++){document.write("<tr>");for(j=0;j<width;j++){document.write("<td id="+(i+"|"+j)+" height=20px width=20px>");document.write("&nbsp;");document.write("</td>");}document.write("</tr>");}document.write("</table>");drawBgColor();document.write("<font id=msg></font>");document.write("<br/><button id=\"startBtn\">开始</button>");startBtn.onclick = function(){startGame();};document.onkeydown = function(){ //监听鼠标事件keyEvent();}</script> </body> </html>。

相关主题