2010-08-01 66 views
4

我已經使用JavaScript編寫的蛇程序.. 的問題是,蛇並沒有增長超過2塊大小....JavaScript數組問題

<html> 
<head> 
<script type="text/javascript"> 

var matrix, body, dir, key, lastx, lasty, start, applex, appley, eat, hal; 


function node(x, y) { 
    this.x = x; 
    this.y = y; 
} 


function draw() { 
    var str; 
    for (var i = 0; i < body.length; i++) { 
     matrix[body[i].x * 50 + body[i].y].bgColor = "black"; 
    } 

} 


function halt() { 
    hal = 1 - hal; 
    if (hal == 0) automove(); 
} 


function check_status() { 
    if (start == 1 && hal == 0) { 

     var ch; 
     if (eat == 1) { 
      do { 
       ch = 0; 
       applex = Math.round(49 * Math.random()); 
       appley = Math.round(49 * Math.random()); 
       for (var i = 0; i < body.length; i++) 
       if (body[i].x == applex && body[i].x == appley) ch = 1; 
      } while (ch == 1); 


      matrix[applex * 50 + appley].bgColor = "blue"; 
      eat = 0; 
     } 



     lastx = body[body.length - 1].x; 
     lasty = body[body.length - 1].y; 
     for (var i = 1; i < body.length; i++) { 
      body[i].x = body[i - 1].x; 
      body[i].y = body[i - 1].y; 
     } 


     if (dir == 1)--body[0].x; 
     else if (dir == -1)++body[0].x; 
     else if (dir == 2)--body[0].y; 
     else if (dir == -2)++body[0].y; 

     if (body[0].x == -1 || body[0].x == 50 || body[0].y == 50 || body[0].y == -1) { 
      alert("GAME OVER!!"); 
      start = 0; 
     } 


     for (var i = 1; i < body.length; i++) { 
      if (body[0].x == body[i].x && body[0].y == body[i].y) { 
       alert("GAME OVER!!"); 
       start = 0; 
       i = 10000; 
      } 
     } 


     if (body[0].x == applex && appley == body[0].y) { 
      eat = 1; 
      body[body.length] = new node(lastx, lasty); 
     } 
     matrix[lastx * 50 + lasty].bgColor = "white"; 
     draw(); 
    } 
} 



function automove() { 
    if (start == 1 && hal == 0) { 
     if (key != -dir) dir = key; 
     check_status(); 
     window.setTimeout("automove()", 200); 
    } 
} 


function init() { 
    start = 1; 
    var x = document.getElementById("mine"); 
    var str = "<table id='tab' align='center' height='500px' cellSpacing='0' cellPadding='0' width='500px' border='4' >"; 


    for (var i = 0; i < 50; i++) { 
     str += "<tr>"; 
     for (var j = 0; j < 50; j++) 
     str += "<td></td>"; 
     str += "</tr>"; 
    } 
    str += "</table>"; 
    x.innerHTML = str; 


    matrix = document.getElementsByTagName("td"); 
    body = new Array(); 
    body[0] = new node(0, 0); 
    draw(); 
    dir = key = -1; 

    eat = 1; 
    v = 0; 
    hal = 0; 
    automove(); 
} 


function keypress(e) { 
    if ((e.keyCode == 38) || ((e.which) && (e.which == 38))) //up 
    key = 1; 
    else if ((e.keyCode == 40) || ((e.which) && (e.which == 40))) //down 
    key = -1; 
    else if ((e.keyCode == 37) || ((e.which) && (e.which == 37))) //left 
    key = 2; 
    else if ((e.keyCode == 39) || ((e.which) && (e.which == 39))) //right 
    key = -2; 
    check_status(); 
} 

</script> 
</head> 
<body onkeydown=keypress(event)> 
<br/> 
<input type="button" onClick="init()" value="play"> 
<input type="button" onClick="halt()" value="pause"> 
<p id="mine"></p> 
<br><h5 id="score"></h5> 
</body> 
</html> 
+0

我不喜歡那個破譯。請你可以縮進(每行前加4個空格)? – Jhong 2010-08-01 15:19:42

+0

現在好嗎? – psy 2010-08-01 15:21:03

+1

在編寫代碼時,您應該加入對代碼添加註釋的做法。這樣的代碼(涉及很多數學)甚至可能很快變得對其原作者感到困惑。 – casablanca 2010-08-01 15:25:49

回答

6

的問題是在這裏:

lastx=body[body.length-1].x; 
lasty=body[body.length-1].y; 
for(var i=1;i<body.length;i++) 
{ 
    body[i].x=body[i-1].x; 
    body[i].y=body[i-1].y; 
} 

在該循環中,body [1]分配給body [0],然後body [2]分配給body [1]等。這意味着從索引1到結尾的所有內容都將設置爲等於body [0],然後body [0]根據方向改變 - 所以只有兩個位置。

查看javascript unshift方法。

您可以用替換循環:

body.unshift(body[0]); 
+1

+1。接得好。當我感到睏倦時, – casablanca 2010-08-01 15:28:27

+0

做了這個程序。 殺死我這個愚蠢的錯誤! – psy 2010-08-01 15:42:01