snake.js Example File

Find this file on Gitorious.

  1. var snake = new Array;
  2. var board = new Array;
  3. var links = new Array;
  4. var scheduledDirections = new Array;
  5. var numRows = 1;
  6. var numColumns = 1;
  7. var linkComponent = Qt.createComponent("content/Link.qml"); // XXX should resolve relative to script, not component
  8. var cookieComponent = Qt.createComponent("content/Cookie.qml");
  9. var cookie;
  10. var linksToGrow = 0;
  11. var linksToDie = 0;
  12. var waitForCookie = 0;
  13. var growType = 0;
  14. var skullMovementsBeforeDirectionChange = 0;
  15.  
  16. function rand(n)
  17.  {
  18.   return (Math.floor(Math.random() * n));
  19. }
  20.  
  21. function scheduleDirection(dir)
  22.  {
  23.     direction = dir;
  24.     if(scheduledDirections[scheduledDirections.length-1]!=direction)
  25.         scheduledDirections.push(direction);
  26. }
  27.  
  28. function startNewGame()
  29.  {
  30.     if (state == "starting")
  31.         return;
  32.  
  33.     if (activeGame)  {
  34.         endGame();
  35.         startNewGameTimer.running = true;
  36.         state = "";
  37.         return;
  38.     }
  39.  
  40.     state = "starting";
  41.  
  42.     numRows = numRowsAvailable;
  43.     numColumns = numColumnsAvailable;
  44.     board = new Array(numRows * numColumns);
  45.     snake = new Array;
  46.     scheduledDirections = new Array;
  47.     growType = 0;
  48.  
  49.     skull.z = numRows * numColumns + 1;
  50.  
  51.     for (var i = 0; i < numRows * numColumns; ++i)  {
  52.         if (i < links.length)  {
  53.             var link = links[i];
  54.             link.spawned = false;
  55.             link.dying = false;
  56.         } else  {
  57.             if(linkComponent.status != Component.Ready)  {
  58.                 if(linkComponent.status == Component.Error)
  59.                     console.log(linkComponent.errorString());
  60.                 else
  61.                     console.log("Still loading linkComponent");
  62.                 continue;//TODO: Better error handling?
  63.             }
  64.             var link = linkComponent.createObject(playfield);
  65.             link.z = numRows * numColumns + 1 - i;
  66.             link.type = i == 0 ? 2 : 0;
  67.             link.spawned = false;
  68.             link.dying = false;
  69.             links.push(link);
  70.         }
  71.     }
  72.  
  73.     head = links[0];
  74.     snake.push(head);
  75.     head.row = numRows/2 -1;
  76.     head.column = numColumns/2 -1;
  77.     head.spawned = true;
  78.  
  79.     linksToGrow = 5;
  80.     linksToDie = 0;
  81.     waitForCookie = 5;
  82.     score = 0;
  83.     startHeartbeatTimer.running = true;
  84. }
  85.  
  86. function endGame()
  87.  {
  88.     activeGame = false;
  89.     for(var i in snake)
  90.         snake[i].dying = true;
  91.     if (cookie)  {
  92.         cookie.dying = true;
  93.         cookie = 0;
  94.     }
  95.     lastScore = score;
  96.     highScores.saveScore(lastScore);
  97.     state = "";
  98. }
  99.  
  100. function move()  {
  101.  
  102.     if (!head)
  103.         return;
  104.  
  105.     var dir = direction;
  106.  
  107.     if (scheduledDirections.length)  {
  108.         dir = scheduledDirections.shift();
  109.     }
  110.  
  111.     if (state == "starting")  {
  112.         var turn = (dir - headDirection);
  113.         head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn );
  114.         headDirection = dir;
  115.         return;
  116.     }
  117.  
  118.     var row = head.row;
  119.     var column = head.column;
  120.  
  121.     if (dir == 0)  {
  122.         row = row - 1;
  123.     } else if (dir == 1)  {
  124.         column = column + 1
  125.     } else if (dir == 2)  {
  126.         row = row + 1;
  127.     } else if (dir == 3)  {
  128.         column = column - 1;
  129.     }
  130.  
  131.     //validate the new position
  132.     if (row < 0 || row >= numRows
  133.         || column < 0 || column >= numColumns
  134.         || (row == skull.row && column == skull.column)
  135.         || !isFree(row, column))  {
  136.         var turn = (dir - headDirection);
  137.         head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn );
  138.         headDirection = dir;
  139.         endGame();
  140.         return;
  141.     }
  142.  
  143.     var newLink;
  144.     if (linksToGrow > 0)  {
  145.         --linksToGrow;
  146.         newLink = links[snake.length];
  147.         newLink.spawned = false;
  148.         newLink.rotation = snake[snake.length-1].rotation;
  149.         newLink.type = growType;
  150.         newLink.dying = false;
  151.         snake.push(newLink);
  152.     } else  {
  153.         var lastLink = snake[snake.length-1];
  154.         board[lastLink.row * numColumns + lastLink.column] = undefined;
  155.     }
  156.  
  157.     if (waitForCookie > 0)  {
  158.         if (--waitForCookie == 0)
  159.             createCookie(cookie? (cookie.value+1) : 1);
  160.     }
  161.  
  162.     for (var i = snake.length-1; i > 0; --i)  {
  163.         snake[i].row = snake[i-1].row;
  164.         snake[i].column = snake[i-1].column;
  165.         snake[i].rotation = snake[i-1].rotation;
  166.     }
  167.  
  168.     if (newLink)  {
  169.         newLink.spawned = true;
  170.     }
  171.  
  172.     // move the head
  173.     head.row = row;
  174.     head.column = column;
  175.     board[row * numColumns + column] = head;
  176.  
  177.     var turn = (dir - headDirection);
  178.     head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn );
  179.     headDirection = dir;
  180.  
  181.     var value = testCookie(row, column);
  182.     if (value > 0)  {
  183.         linksToGrow += value;
  184.         score += value;
  185.     }
  186. }
  187.  
  188. function isFree(row, column)
  189.  {
  190.     return board[row * numColumns + column] == undefined;
  191. }
  192.  
  193. function isHead(row, column)
  194.  {
  195.     return head.column == column && head.row == row;
  196. }
  197.  
  198. function testCookie(row, column)
  199.  {
  200.     if (cookie && !cookie.dying && cookie.row == row && cookie.column == column)  {
  201.         var value = cookie.value;
  202.         waitForCookie = value;
  203.         growType = snake[snake.length-1].type == 1 ? 0 : 1;
  204.         cookie.dying = true;
  205.         cookie.z = numRows * numColumns + 2;
  206.         return value;
  207.     }
  208.     return 0;
  209. }
  210.  
  211. function moveSkull()
  212.  {
  213.  
  214.     if (linksToDie > 0)  {
  215.         --linksToDie;
  216.         var link = snake.pop();
  217.         link.dying = true;
  218.         board[link.row * numColumns + link.column] = undefined;
  219.         if (score > 0)
  220.             --score;
  221.         if (snake.length == 0)  {
  222.             endGame();
  223.             return;
  224.         }
  225.     }
  226.  
  227.     var row = skull.row;
  228.     var column = skull.column;
  229.     if (isHead(row, column))  {
  230.         endGame();
  231.         return;
  232.     }
  233.     row +=  skull.verticalMovement;
  234.     column += skull.horizontalMovement;
  235.  
  236.     var attempts = 4;
  237.  
  238.     while (skullMovementsBeforeDirectionChange == 0 || row < 0 || row >= numRows
  239.         || column < 0 || column >= numColumns
  240.         || (!isFree(row, column) && !isHead(row, column)))  {
  241.         var d = rand(8);
  242.         skull.verticalMovement = 0;
  243.         skull.horizontalMovement = 0;
  244.         skullMovementsBeforeDirectionChange = rand(20)+1;
  245.         if (d == 0)  {
  246.             skull.verticalMovement = -1
  247.         } else if (d == 1)  {
  248.             skull.horizontalMovement = -1;
  249.         } else if (d == 2)  {
  250.             skull.verticalMovement = 1
  251.         } else if (d == 3) {
  252.             skull.horizontalMovement = 1;
  253.         } else if (cookie)  {
  254.             var rd = cookie.row - skull.row;
  255.             var rc = cookie.column - skull.column;
  256.             if (Math.abs(rd) > Math.abs(rc))  {
  257.                 skull.verticalMovement = rd > 0 ? 1 : -1;
  258.                 skullMovementsBeforeDirectionChange = Math.abs(rd);
  259.             } else  {
  260.                 skull.horizontalMovement= rc > 0 ? 1 : -1;
  261.                 skullMovementsBeforeDirectionChange = Math.abs(rc);
  262.             }
  263.         }
  264.         row = skull.row + skull.verticalMovement;
  265.         column = skull.column + skull.horizontalMovement;
  266.         if (--attempts == 0)
  267.             return;
  268.     }
  269.  
  270.     skull.row = row;
  271.     skull.column = column;
  272.     --skullMovementsBeforeDirectionChange;
  273.     var value = testCookie(row, column);
  274.     if (value > 0)
  275.         linksToDie += value/2;
  276.  
  277.     if (isHead(row, column))
  278.         endGame();
  279. }
  280.  
  281. function createCookie(value)  {
  282.     if (numRows * numColumns - snake.length < 10)
  283.         return;
  284.  
  285.     var column = rand(numColumns);
  286.     var row = rand(numRows);
  287.     while (!isFree(row, column))  {
  288.         column++;
  289.         if (column == numColumns)  {
  290.             column = 0;
  291.             row++;
  292.             if (row == numRows)
  293.                 row = 0;
  294.         }
  295.     }
  296.  
  297.     if(cookieComponent.status != Component.Ready)  {
  298.         if(cookieComponent.status == Component.Error)
  299.             console.log(cookieComponent.errorString());
  300.         else
  301.             console.log("Still loading cookieComponent");
  302.         return;//TODO: Better error handling?
  303.     }
  304.     cookie = cookieComponent.createObject(head.parent);
  305.     cookie.value = value;
  306.     cookie.row = row;
  307.     cookie.column = column;
  308. }
Notes provided by the Qt Community

No notes