Untitled


SUBMITTED BY: Guest

DATE: Oct. 27, 2016, 3:38 a.m.

FORMAT: Text only

SIZE: 9.1 kB

HITS: 1487

  1. /*
  2. "driving.js" is a simple driving simulator written to introduce students to
  3. the basics of game programming.
  4. Author: zac
  5. Date: October 2016
  6. Usage: This script is invoked by loading "driving.html" in a browser.
  7. */
  8. // Game Parameters
  9. /* Note that ANIMDELTA should be a factor of BLOCKSIZE (1 for per-pixel
  10. animation, higher for performance, BLOCKSIZE for charm) */
  11. const BLOCKSIZE = 20; // Most dimensions are multiples of this
  12. const ANIMDELTA = 20; // Should be a factor of BLOCKSIZE (read above)
  13. const DIFFICULTYPERIOD = 2000; // Period between difficulty increases in ms
  14. const INITTIMEDELTA = 60; // Initial delay between frames in ms
  15. const MINTIMEDELTA = 5; // Browser is highly unlikely to go any faster
  16. const TITLE = "i am the one";
  17. const PAGEMARGIN = 20;
  18. const PAGECOLOUR = "LIME";
  19. const MAINFONTCOLOUR = "AQUAMARINE";
  20. const HEADINGBGCOLOUR = "NAVY";
  21. const INSBGCOLOUR = "NAVY";
  22. const PLAYAREAWIDTH = 35 * BLOCKSIZE;
  23. const PLAYAREAHEIGHT = 25 * BLOCKSIZE;
  24. const PLAYAREACOLOUR = "OliveDrab";
  25. const PLAYAREAZINDEX = "-5";
  26. const ROADWIDTH = 11 * BLOCKSIZE; // Best as odd number of blocks
  27. const ROADCOLOUR = "RED";
  28. const ROADZINDEX = "-4";
  29. const ROADINITPOS = Math.floor((PLAYAREAWIDTH - ROADWIDTH) / 2); // halfway
  30. const NUMROADPIECES = Math.floor(PLAYAREAHEIGHT/ANIMDELTA);
  31. const ROADGOESLEFT = 0.4; // roadDiceThrow < this value, road curves left
  32. const ROADGOESRIGHT = 0.6; // roadDiceThrow > this value, road curves right
  33. const CARCOLOUR = "black";
  34. const CARZINDEX = "-3";
  35. const CARINITPOS = Math.floor((PLAYAREAWIDTH - BLOCKSIZE) / 2); // halfway
  36. const FLASHCOLOUR = "RED"; // Colour used in death animation
  37. const FLASHPERIOD = 50; // Controls rate of flashing in death animation
  38. const NUMFLASHES = 20; // Control how long the death animation goes for
  39. // Setup the page
  40. document.body.style.margin = PAGEMARGIN + "px";
  41. document.body.style.background = PAGECOLOUR;
  42. document.body.style.textAlign = "center";
  43. var pageTitle = document.createElement("title");
  44. pageTitle.innerHTML = TITLE;
  45. document.head.appendChild(pageTitle);
  46. var pageHeading = document.createElement("div");
  47. pageHeading.style.width = (PLAYAREAWIDTH - 20) + "px";
  48. pageHeading.style.color = MAINFONTCOLOUR;
  49. pageHeading.style.background = HEADINGBGCOLOUR;
  50. pageHeading.style.textAlign = "center";
  51. pageHeading.style.font = "bold 20px Verdana,Geneva,sans-serif";
  52. pageHeading.style.margin = "0px auto 0px auto";
  53. pageHeading.style.padding = "10px 10px 10px 10px";
  54. pageHeading.innerHTML = TITLE;
  55. document.body.appendChild(pageHeading);
  56. var scoreBox = document.createElement("div");
  57. scoreBox.style.width = (PLAYAREAWIDTH - 20) + "px";
  58. scoreBox.style.color = MAINFONTCOLOUR;
  59. scoreBox.style.background = HEADINGBGCOLOUR;
  60. scoreBox.style.textAlign = "center";
  61. scoreBox.style.font = "14px Verdana,Geneva,sans-serif";
  62. scoreBox.style.margin = "0px auto 0px auto";
  63. scoreBox.style.padding = "0px 10px 10px 10px";
  64. scoreBox.innerHTML = "Score: 0";
  65. document.body.appendChild(scoreBox);
  66. // Create the playing area
  67. var playArea = document.createElement("div");
  68. playArea.style.width = PLAYAREAWIDTH + "px";
  69. playArea.style.height = PLAYAREAHEIGHT + "px";
  70. playArea.style.margin = "0px auto 0px auto";
  71. playArea.style.background = PLAYAREACOLOUR;
  72. playArea.style.position = "relative";
  73. playArea.style.zIndex = PLAYAREAZINDEX;
  74. document.body.appendChild(playArea);
  75. // Display the instructions
  76. var instructions = document.createElement("div");
  77. instructions.style.width = (PLAYAREAWIDTH - 14) + "px";
  78. instructions.style.color = MAINFONTCOLOUR;
  79. instructions.style.background = INSBGCOLOUR;
  80. instructions.style.textAlign = "center";
  81. instructions.style.font = "14px Verdana,Geneva,sans-serif";
  82. instructions.style.margin = "0px auto 0px auto";
  83. instructions.style.padding = "7px";
  84. instructions.innerHTML = "<B>S</B> &nbsp;Start &nbsp; &nbsp; &#9664; &nbsp;Left &nbsp; &nbsp; &#9654; &nbsp;Right";
  85. document.body.appendChild(instructions);
  86. // Create the road
  87. var roadPieces = []; // an array of the rectangles that make up the road
  88. var roadPiece = 0; // will serve as the index of the roadPieces array
  89. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  90. {
  91. roadPieces[roadPiece] = document.createElement("div");
  92. roadPieces[roadPiece].style.width = ROADWIDTH + "px";
  93. roadPieces[roadPiece].style.height = ANIMDELTA + "px";
  94. roadPieces[roadPiece].style.background = ROADCOLOUR;
  95. roadPieces[roadPiece].style.position = "absolute";
  96. roadPieces[roadPiece].style.zIndex = ROADZINDEX;
  97. roadPieces[roadPiece].style.left = ROADINITPOS + "px";
  98. roadPieces[roadPiece].style.top = (roadPiece * ANIMDELTA) + "px";
  99. playArea.appendChild(roadPieces[roadPiece]);
  100. }
  101. var carOnPiece = Math.floor(3/4*NUMROADPIECES); // constant in this version
  102. // Create the car (the manipulation below results in a triangle)
  103. var car = document.createElement("div");
  104. car.style.width = BLOCKSIZE + "px";
  105. car.style.height = BLOCKSIZE + "px";
  106. car.style.background = CARCOLOUR;
  107. car.style.position = "absolute";
  108. car.style.zIndex = CARZINDEX;
  109. car.style.left = CARINITPOS + "px";
  110. car.style.top = roadPieces[carOnPiece].style.top;
  111. playArea.appendChild(car);
  112. // Listen for key presses
  113. var gameIsRunning = false;
  114. var changeInCarPos = 0; // used for steering
  115. document.onkeydown = checkDown;
  116. function checkDown(e)
  117. {
  118. if (e.keyCode === 37) changeInCarPos = -ANIMDELTA; // 37 left cursor
  119. else if (e.keyCode === 39) changeInCarPos = ANIMDELTA; // 39 right cursor
  120. else if (e.keyCode === 83 && !gameIsRunning) // 83 is S
  121. {
  122. gameIsRunning = true;
  123. runGame();
  124. }
  125. }
  126. document.onkeyup = checkLift;
  127. function checkLift(e)
  128. {
  129. if
  130. (
  131. (e.keyCode === 37 && changeInCarPos < 0) || // 37 left cursor
  132. (e.keyCode === 39 && changeInCarPos > 0) // 39 right cursor
  133. )
  134. changeInCarPos = 0;
  135. }
  136. // The game loop
  137. function runGame()
  138. {
  139. var score = 0; // The score
  140. var timeDelta = INITTIMEDELTA; // Initial delay between each frame
  141. var flashNumber = NUMFLASHES; // Flashes left on death animation
  142. var carLeftEdge = CARINITPOS;
  143. var roadLeftEdges = [];
  144. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  145. roadLeftEdges[roadPiece] = ROADINITPOS;
  146. var changedLeftRoadEdge = ROADINITPOS;
  147. var roadDiceThrow = 0;
  148. var changeInRoadPos = 0; // note: changeInCarPos is defined above
  149. // reset positions of the road and car
  150. render();
  151. // start difficulty timer
  152. var difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  153. function increaseDifficulty()
  154. {
  155. if (gameIsRunning)
  156. {
  157. if (timeDelta > MINTIMEDELTA) timeDelta--;
  158. difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  159. }
  160. }
  161. // start the game loop
  162. var frame = setTimeout(gameLoop, timeDelta);
  163. function gameLoop()
  164. {
  165. // calculate movement
  166. carLeftEdge += changeInCarPos;
  167. if (carLeftEdge < 0)
  168. carLeftEdge = 0;
  169. else if (carLeftEdge > (PLAYAREAWIDTH - BLOCKSIZE))
  170. carLeftEdge = PLAYAREAWIDTH - BLOCKSIZE;
  171. roadDiceThrow = Math.random();
  172. if (roadDiceThrow < ROADGOESLEFT) changeInRoadPos = -ANIMDELTA;
  173. else if (roadDiceThrow >= ROADGOESRIGHT) changeInRoadPos = ANIMDELTA;
  174. else changeInRoadPos = 0;
  175. changedLeftRoadEdge += changeInRoadPos;
  176. if (changedLeftRoadEdge < 0)
  177. changedLeftRoadEdge = 0;
  178. else if (changedLeftRoadEdge > (PLAYAREAWIDTH - ROADWIDTH))
  179. changedLeftRoadEdge = PLAYAREAWIDTH - ROADWIDTH;
  180. for (roadPiece = NUMROADPIECES - 1; roadPiece >= 1; roadPiece--)
  181. roadLeftEdges[roadPiece] = roadLeftEdges[roadPiece - 1];
  182. roadLeftEdges[0] = changedLeftRoadEdge;
  183. // render new physical state
  184. render();
  185. // collision detection
  186. if (
  187. (carLeftEdge < roadLeftEdges[carOnPiece]) ||
  188. (carLeftEdge + BLOCKSIZE > roadLeftEdges[carOnPiece] + ROADWIDTH)
  189. )
  190. {
  191. gameIsRunning = false;
  192. frame = setTimeout(deathAnimation, FLASHPERIOD);
  193. }
  194. // scoring
  195. score++;
  196. // loop again if the game is still going
  197. if (gameIsRunning) frame = setTimeout(gameLoop, timeDelta);
  198. }
  199. function render()
  200. {
  201. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  202. roadPieces[roadPiece].style.left = roadLeftEdges[roadPiece] + "px";
  203. car.style.left = carLeftEdge + "px";
  204. //scoreBox.innerHTML = "Score: " + score + " (" + timeDelta + ")";
  205. scoreBox.innerHTML = "Score: " + score;
  206. }
  207. function deathAnimation()
  208. {
  209. if (flashNumber % 2 == 0)
  210. car.style.background = FLASHCOLOUR;
  211. else
  212. car.style.background = CARCOLOUR;
  213. flashNumber--;
  214. if (flashNumber > 0 && !gameIsRunning)
  215. frame = setTimeout(deathAnimation, FLASHPERIOD);
  216. else
  217. car.style.background = CARCOLOUR;
  218. }
  219. }

comments powered by Disqus