demos/declarative/snake/content/snake.js
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    35     if (heartbeat.running) {
    35     if (heartbeat.running) {
    36         endGame();
    36         endGame();
    37         startNewGameTimer.running = true;
    37         startNewGameTimer.running = true;
    38         return;
    38         return;
    39     }
    39     }
       
    40 
    40     numRows = numRowsAvailable;
    41     numRows = numRowsAvailable;
    41     numColumns = numColumnsAvailable;
    42     numColumns = numColumnsAvailable;
    42     board = new Array(numRows * numColumns);
    43     board = new Array(numRows * numColumns);
    43     snake = new Array;
    44     snake = new Array;
    44     scheduledDirections = new Array;
    45     scheduledDirections = new Array;
   147         newLink.type = growType;
   148         newLink.type = growType;
   148         newLink.dying = false;
   149         newLink.dying = false;
   149         snake.push(newLink);
   150         snake.push(newLink);
   150     } else {
   151     } else {
   151         var lastLink = snake[snake.length-1];
   152         var lastLink = snake[snake.length-1];
   152         board[lastLink.row * numColumns + lastLink.column] = Undefined;
   153         board[lastLink.row * numColumns + lastLink.column] = undefined;
   153     }
   154     }
   154 
   155 
   155     if (waitForCookie > 0) {
   156     if (waitForCookie > 0) {
   156         if (--waitForCookie == 0)
   157         if (--waitForCookie == 0)
   157             createCookie(cookie? (cookie.value+1) : 1);
   158             createCookie(cookie? (cookie.value+1) : 1);
   183     }
   184     }
   184 }
   185 }
   185 
   186 
   186 function isFree(row, column)
   187 function isFree(row, column)
   187 {
   188 {
   188     return board[row * numColumns + column] == Undefined;
   189     return board[row * numColumns + column] == undefined;
   189 }
   190 }
   190 
   191 
   191 function isHead(row, column)
   192 function isHead(row, column)
   192 {
   193 {
   193     return head.column == column && head.row == row;
   194     return head.column == column && head.row == row;
   211 
   212 
   212     if (linksToDie > 0) {
   213     if (linksToDie > 0) {
   213         --linksToDie;
   214         --linksToDie;
   214         var link = snake.pop();
   215         var link = snake.pop();
   215         link.dying = true;
   216         link.dying = true;
   216         board[link.row * numColumns + link.column] = Undefined;
   217         board[link.row * numColumns + link.column] = undefined;
   217         if (score > 0)
   218         if (score > 0)
   218             --score;
   219             --score;
   219         if (snake.length == 0) {
   220         if (snake.length == 0) {
   220             endGame();
   221             endGame();
   221             return;
   222             return;