Untitled


SUBMITTED BY: Guest

DATE: Oct. 25, 2016, 3:33 a.m.

FORMAT: Text only

SIZE: 7.2 kB

HITS: 1030

  1. document.body.style.margin = PAGEMARGIN + "px";
  2. document.body.style.background = PAGECOLOUR;
  3. document.body.style.textAlign = "center";
  4. var pageTitle = document.createElement("title");
  5. pageTitle.innerHTML = TITLE;
  6. document.head.appendChild(pageTitle);
  7. var pageHeading = document.createElement("div");
  8. pageHeading.style.width = (PLAYAREAWIDTH - 20) + "px";
  9. pageHeading.style.color = MAINFONTCOLOUR;
  10. pageHeading.style.background = HEADINGBGCOLOUR;
  11. pageHeading.style.textAlign = "center";
  12. pageHeading.style.font = "bold 20px Verdana,Geneva,sans-serif";
  13. pageHeading.style.margin = "0px auto 0px auto";
  14. pageHeading.style.padding = "10px 10px 10px 10px";
  15. pageHeading.innerHTML = TITLE;
  16. document.body.appendChild(pageHeading);
  17. var scoreBox = document.createElement("div");
  18. scoreBox.style.width = (PLAYAREAWIDTH - 20) + "px";
  19. scoreBox.style.color = MAINFONTCOLOUR;
  20. scoreBox.style.background = HEADINGBGCOLOUR;
  21. scoreBox.style.textAlign = "center";
  22. scoreBox.style.font = "14px Verdana,Geneva,sans-serif";
  23. scoreBox.style.margin = "0px auto 0px auto";
  24. scoreBox.style.padding = "0px 10px 10px 10px";
  25. scoreBox.innerHTML = "Score: 0";
  26. document.body.appendChild(scoreBox);
  27. // Create the playing area
  28. var playArea = document.createElement("div");
  29. playArea.style.width = PLAYAREAWIDTH + "px";
  30. playArea.style.height = PLAYAREAHEIGHT + "px";
  31. playArea.style.margin = "0px auto 0px auto";
  32. playArea.style.background = PLAYAREACOLOUR;
  33. playArea.style.position = "relative";
  34. playArea.style.zIndex = PLAYAREAZINDEX;
  35. document.body.appendChild(playArea);
  36. // Display the instructions
  37. var instructions = document.createElement("div");
  38. instructions.style.width = (PLAYAREAWIDTH - 14) + "px";
  39. instructions.style.color = MAINFONTCOLOUR;
  40. instructions.style.background = INSBGCOLOUR;
  41. instructions.style.textAlign = "center";
  42. instructions.style.font = "14px Verdana,Geneva,sans-serif";
  43. instructions.style.margin = "0px auto 0px auto";
  44. instructions.style.padding = "7px";
  45. instructions.innerHTML = "<B>S</B> &nbsp;Start &nbsp; &nbsp; &#9664; &nbsp;Left &nbsp; &nbsp; &#9654; &nbsp;Right";
  46. document.body.appendChild(instructions);
  47. // Create the road
  48. var roadPieces = []; // an array of the rectangles that make up the road
  49. var roadPiece = 0; // will serve as the index of the roadPieces array
  50. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  51. {
  52. roadPieces[roadPiece] = document.createElement("div");
  53. roadPieces[roadPiece].style.width = ROADWIDTH + "px";
  54. roadPieces[roadPiece].style.height = ANIMDELTA + "px";
  55. roadPieces[roadPiece].style.background = ROADCOLOUR;
  56. roadPieces[roadPiece].style.position = "absolute";
  57. roadPieces[roadPiece].style.zIndex = ROADZINDEX;
  58. roadPieces[roadPiece].style.left = ROADINITPOS + "px";
  59. roadPieces[roadPiece].style.top = (roadPiece * ANIMDELTA) + "px";
  60. playArea.appendChild(roadPieces[roadPiece]);
  61. }
  62. var carOnPiece = Math.floor(3/4*NUMROADPIECES); // constant in this version
  63. // Create the car (the manipulation below results in a triangle)
  64. var car = document.createElement("div");
  65. car.style.width = BLOCKSIZE + "px";
  66. car.style.height = BLOCKSIZE + "px";
  67. car.style.background = CARCOLOUR;
  68. car.style.position = "absolute";
  69. car.style.zIndex = CARZINDEX;
  70. car.style.left = CARINITPOS + "px";
  71. car.style.top = roadPieces[carOnPiece].style.top;
  72. playArea.appendChild(car);
  73. // Listen for key presses
  74. var gameIsRunning = false;
  75. var changeInCarPos = 0; // used for steering
  76. document.onkeydown = checkDown;
  77. function checkDown(e)
  78. {
  79. if (e.keyCode === 37) changeInCarPos = -ANIMDELTA; // 37 left cursor
  80. else if (e.keyCode === 39) changeInCarPos = ANIMDELTA; // 39 right cursor
  81. else if (e.keyCode === 83 && !gameIsRunning) // 83 is S
  82. {
  83. gameIsRunning = true;
  84. runGame();
  85. }
  86. }
  87. document.onkeyup = checkLift;
  88. function checkLift(e)
  89. {
  90. if
  91. (
  92. (e.keyCode === 37 && changeInCarPos < 0) || // 37 left cursor
  93. (e.keyCode === 39 && changeInCarPos > 0) // 39 right cursor
  94. )
  95. changeInCarPos = 0;
  96. }
  97. // The game loop
  98. function runGame()
  99. {
  100. var score = 0; // The score
  101. var timeDelta = INITTIMEDELTA; // Initial delay between each frame
  102. var flashNumber = NUMFLASHES; // Flashes left on death animation
  103. var carLeftEdge = CARINITPOS;
  104. var roadLeftEdges = [];
  105. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  106. roadLeftEdges[roadPiece] = ROADINITPOS;
  107. var changedLeftRoadEdge = ROADINITPOS;
  108. var roadDiceThrow = 0;
  109. var changeInRoadPos = 0; // note: changeInCarPos is defined above
  110. // reset positions of the road and car
  111. render();
  112. // start difficulty timer
  113. var difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  114. function increaseDifficulty()
  115. {
  116. if (gameIsRunning)
  117. {
  118. if (timeDelta > MINTIMEDELTA) timeDelta--;
  119. difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  120. }
  121. }
  122. // start the game loop
  123. var frame = setTimeout(gameLoop, timeDelta);
  124. function gameLoop()
  125. {
  126. // calculate movement
  127. carLeftEdge += changeInCarPos;
  128. if (carLeftEdge < 0)
  129. carLeftEdge = 0;
  130. else if (carLeftEdge > (PLAYAREAWIDTH - BLOCKSIZE))
  131. carLeftEdge = PLAYAREAWIDTH - BLOCKSIZE;
  132. roadDiceThrow = Math.random();
  133. if (roadDiceThrow < ROADGOESLEFT) changeInRoadPos = -ANIMDELTA;
  134. else if (roadDiceThrow >= ROADGOESRIGHT) changeInRoadPos = ANIMDELTA;
  135. else changeInRoadPos = 0;
  136. changedLeftRoadEdge += changeInRoadPos;
  137. if (changedLeftRoadEdge < 0)
  138. changedLeftRoadEdge = 0;
  139. else if (changedLeftRoadEdge > (PLAYAREAWIDTH - ROADWIDTH))
  140. changedLeftRoadEdge = PLAYAREAWIDTH - ROADWIDTH;
  141. for (roadPiece = NUMROADPIECES - 1; roadPiece >= 1; roadPiece--)
  142. roadLeftEdges[roadPiece] = roadLeftEdges[roadPiece - 1];
  143. roadLeftEdges[0] = changedLeftRoadEdge;
  144. // render new physical state
  145. render();
  146. // collision detection
  147. if (
  148. (carLeftEdge < roadLeftEdges[carOnPiece]) ||
  149. (carLeftEdge + BLOCKSIZE > roadLeftEdges[carOnPiece] + ROADWIDTH)
  150. )
  151. {
  152. gameIsRunning = false;
  153. frame = setTimeout(deathAnimation, FLASHPERIOD);
  154. }
  155. // scoring
  156. score++;
  157. // loop again if the game is still going
  158. if (gameIsRunning) frame = setTimeout(gameLoop, timeDelta);
  159. }
  160. function render()
  161. {
  162. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  163. roadPieces[roadPiece].style.left = roadLeftEdges[roadPiece] + "px";
  164. car.style.left = carLeftEdge + "px";
  165. //scoreBox.innerHTML = "Score: " + score + " (" + timeDelta + ")";
  166. scoreBox.innerHTML = "Score: " + score;
  167. }
  168. function deathAnimation()
  169. {
  170. if (flashNumber % 2 == 0)
  171. car.style.background = FLASHCOLOUR;
  172. else
  173. car.style.background = CARCOLOUR;
  174. flashNumber--;
  175. if (flashNumber > 0 && !gameIsRunning)
  176. frame = setTimeout(deathAnimation, FLASHPERIOD);
  177. else
  178. car.style.background = CARCOLOUR;
  179. }
  180. }

comments powered by Disqus