Darb


SUBMITTED BY: Guest

DATE: Oct. 27, 2016, 2:02 a.m.

FORMAT: Text only

SIZE: 9.2 kB

HITS: 903

  1. 1./*
  2. "driving.js" is a simple driving simulator written to introduce students to
  3. the basics of game programming.
  4. Author: Bob Jones & Raven Lord
  5. Date: October 10th 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 = 1000; // 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 = "Driving Game";
  17. const PAGEMARGIN = 20;
  18. const PAGECOLOUR = "White";
  19. const MAINFONTCOLOUR = "Black";
  20. const HEADINGBGCOLOUR = "White";
  21. const INSBGCOLOUR = "White";
  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 = "Silver";
  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 = "Yellow";
  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; // left cursor
  119. else if(e.keyCode === 39) changeInCarPos = ANIMDELTA; // right cursor
  120. else if(e.keyCode === 83 && !gameIsRunning) // S key
  121. {
  122. gameIsRunning = true;
  123. runGame();
  124. }
  125. }
  126. document.onkeyup = checkLift;
  127. function checkLift(e)
  128. {
  129. if (
  130. (e.keyCode === 37 && changeInCarPos < 0) || // left
  131. (e.keyCode === 39 && changeInCarPos > 0) // right
  132. )
  133. changeInCarPos = 0;
  134. }
  135. // The game loop
  136. function runGame()
  137. {
  138. var score = 0; // The score
  139. var timeDelta = INITTIMEDELTA; // Initial delay between each frame
  140. var flashNumber = NUMFLASHES; // Flashes left on death animation
  141. var carLeftEdge = CARINITPOS;
  142. var roadLeftEdges = [];
  143. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  144. roadLeftEdges[roadPiece] = ROADINITPOS;
  145. var changedLeftRoadEdge = ROADINITPOS;
  146. var roadDiceThrow = 0;
  147. var changeInRoadPos = 0; // note: changeInCarPos is defined above
  148. // reset positions of the road and car
  149. render();
  150. // start difficulty timer
  151. var difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  152. function increaseDifficulty()
  153. {
  154. if (gameIsRunning)
  155. {
  156. if (timeDelta > MINTIMEDELTA) timeDelta--;
  157. difficultyTimer = setTimeout(increaseDifficulty, DIFFICULTYPERIOD);
  158. }
  159. }
  160. // start the game loop
  161. var frame = setTimeout(gameLoop, timeDelta);
  162. function gameLoop()
  163. {
  164. // calculate movement
  165. carLeftEdge += changeInCarPos;
  166. if (carLeftEdge < 0)
  167. carLeftEdge = 0;
  168. else if (carLeftEdge > (PLAYAREAWIDTH - BLOCKSIZE))
  169. carLeftEdge = PLAYAREAWIDTH - BLOCKSIZE;
  170. roadDiceThrow = Math.random();
  171. if (roadDiceThrow < ROADGOESLEFT) changeInRoadPos = -ANIMDELTA;
  172. else if (roadDiceThrow >= ROADGOESRIGHT) changeInRoadPos = ANIMDELTA;
  173. else changeInRoadPos = 0;
  174. changedLeftRoadEdge += changeInRoadPos;
  175. if (changedLeftRoadEdge < 0)
  176. changedLeftRoadEdge = 0;
  177. else if (changedLeftRoadEdge > (PLAYAREAWIDTH - ROADWIDTH))
  178. changedLeftRoadEdge = PLAYAREAWIDTH - ROADWIDTH;
  179. for (roadPiece = NUMROADPIECES - 1; roadPiece >= 1; roadPiece --)
  180. roadLeftEdges[roadPiece] = roadLeftEdges[roadPiece -1];
  181. roadLeftEdges[0] = changedLeftRoadEdge;
  182. // render new physical state
  183. render();
  184. // collision detection
  185. if(
  186. (carLeftEdge < roadLeftEdges [carOnPiece]) ||
  187. (carLeftEdge + BLOCKSIZE > roadLeftEdges[carOnPiece] + ROADWIDTH)
  188. )
  189. {
  190. gameIsRunning = false;
  191. frame = setTimeout(deathAnimation, FLASHPERIOD);
  192. }
  193. // scoring
  194. score++;
  195. // loop again if the game is still going
  196. if (gameIsRunning) frame = setTimeout (gameLoop, timeDelta);
  197. }
  198. function render()
  199. {
  200. for (roadPiece = 0; roadPiece < NUMROADPIECES ; roadPiece++)
  201. roadPieces[roadPiece].style.left = roadLeftEdges[roadPiece] + "px";
  202. car.style.left = carLeftEdge + "px";
  203. //scoreBox.innerHTML = "Score: " + score + " (" + timeDelta + ")";
  204. scoreBox.innerHTML = "Score: " + score;
  205. }
  206. function deathAnimation()
  207. {
  208. if (flashNumber % 2 == 0)
  209. car.style.background = FLASHCOLOUR;
  210. else
  211. car.style.background = CARCOLOUR;
  212. flashNumber--;
  213. if (flashNumber > 0 && !gameIsRunning)
  214. frame = setTimeout(deathAnimation, FLASHPERIOD);
  215. else
  216. car.style.background = CARCOLOUR;
  217. }
  218. }

comments powered by Disqus