script.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const score = document.querySelector('.score'),
  2. start = document.querySelector('.start'),
  3. gameArea = document.querySelector('.gameArea'),
  4. selectDif = document.getElementsByName('rdo')
  5. car = document.createElement('div');
  6. const audio = new Audio();
  7. selectDif.forEach(el => {
  8. el.addEventListener('click', startGame);
  9. })
  10. document.addEventListener('keydown', startRun);
  11. document.addEventListener('keyup', stopRun);
  12. car.classList.add('car');
  13. const keys = {
  14. ArrowUp: false,
  15. ArrowDown: false,
  16. ArrowRight: false,
  17. ArrowLeft: false
  18. }
  19. const settings = {
  20. start: false,
  21. score: 0,
  22. speed: 4,
  23. traffic: 3
  24. }
  25. const playBack = {
  26. conposition: 'mp3/Need For Speed.mp3',
  27. paused: false
  28. }
  29. function getQuantityElements(elementHeight) {
  30. return document.documentElement.clientHeight / elementHeight + 1;
  31. }
  32. function startGame() {
  33. gameArea.innerHTML = '';
  34. selectDif.forEach(el => {
  35. if (el.checked) {
  36. const value = +el.value
  37. switch (value) {
  38. case 1:
  39. settings.speed = 3;
  40. settings.traffic = 3;
  41. break;
  42. case 2:
  43. settings.speed = 4;
  44. settings.traffic = 2;
  45. break;
  46. case 3:
  47. settings.speed = 5;
  48. settings.traffic = 2;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. })
  55. soundPlay();
  56. start.classList.add('hide');
  57. settings.score = 0;
  58. settings.start = true;
  59. addLines(getQuantityElements(100));
  60. gameArea.appendChild(car);
  61. car.style.left = '125px';
  62. car.style.top = 'auto';
  63. car.style.bottom = '10px';
  64. settings.x = car.offsetLeft;
  65. settings.y = car.offsetTop;
  66. requestAnimationFrame(playGame);
  67. let counter = 0;
  68. const startTitle = document.createElement('div');
  69. startTitle.classList.add('startTitle');
  70. startTitle.innerText = 'Поехали!';
  71. const starting = setInterval(() => {
  72. car.style.display = 'block';
  73. setTimeout(() => {
  74. if (counter > 4) {
  75. car.style.display = 'block';
  76. gameArea.appendChild(startTitle);
  77. clearInterval(starting);
  78. setTimeout(() => {
  79. startTitle.style.display = 'none'
  80. addEnemies(getQuantityElements(100 * settings.traffic));
  81. }, 2000)
  82. } else {
  83. car.style.display = 'none';
  84. }
  85. }, 250);
  86. counter++;
  87. }, 500)
  88. }
  89. function getRandomIntInclusive(min, max) {
  90. min = Math.ceil(min);
  91. max = Math.floor(max);
  92. return Math.floor(Math.random() * (max - min + 1)) + min; //Максимум и минимум включаются
  93. };
  94. function addLines(num) {
  95. for (let i = 0; i < num; i++) {
  96. const line = document.createElement('div');
  97. line.classList.add('line');
  98. line.style.top = i * 100 + 'px';
  99. line.y = i * 100;
  100. gameArea.appendChild(line);
  101. }
  102. }
  103. function addEnemies(num) {
  104. for (let i = 0; i < num; i++) {
  105. const enemy = document.createElement('div');
  106. enemy.classList.add('enemy');
  107. enemy.y = -100 * settings.traffic * (i + 1);
  108. enemy.style.top = enemy.y + 'px';
  109. enemy.style.left = getRandomIntInclusive(50, gameArea.offsetWidth - 50) + 'px';
  110. gameArea.appendChild(enemy);
  111. const enemyModel = getRandomIntInclusive(1, 9);
  112. enemy.style.background = `transparent url('./image/enemy${enemyModel}.png') center / cover no-repeat`
  113. }
  114. }
  115. function moveRoad() {
  116. let lines = document.querySelectorAll('.line');
  117. lines.forEach(line => {
  118. line.y += settings.speed;
  119. line.style.top = line.y + 'px';
  120. if (line.y >= document.documentElement.clientHeight) {
  121. line.y = -100;
  122. }
  123. })
  124. }
  125. function soundPlay() {
  126. audio.src = playBack.conposition;
  127. audio.autoplay = true;
  128. audio.loop = true;
  129. }
  130. function crash() {
  131. start.classList.remove('hide');
  132. settings.start = false;
  133. start.style.top = score.offsetHeight;
  134. audio.pause();
  135. if (localStorage.NFJS) {
  136. if (localStorage.NFJS < settings.score) {
  137. alert('Поздравляем! Вы побили рекорд.');
  138. localStorage.NFJS = Math.floor(settings.score);
  139. }
  140. } else {
  141. localStorage.NFJS = Math.floor(settings.score);
  142. }
  143. }
  144. function moveEnemy() {
  145. let enemies = document.querySelectorAll('.enemy');
  146. enemies.forEach(item => {
  147. let carRect = car.getBoundingClientRect();
  148. let enemyRect = item.getBoundingClientRect();
  149. if (carRect.top <= enemyRect.bottom && carRect.right >= enemyRect.left &&
  150. carRect.left <= enemyRect.right &&
  151. carRect.bottom >= enemyRect.top) {
  152. crash();
  153. }
  154. item.y += settings.speed / 2;
  155. item.style.top = item.y + 'px';
  156. if (item.y >= document.documentElement.clientHeight) {
  157. const enemyModel = getRandomIntInclusive(1, 9);
  158. item.style.background = `transparent url('./image/enemy${enemyModel}.png') center / cover no-repeat`
  159. item.style.left = getRandomIntInclusive(50, gameArea.offsetWidth - 50) + 'px';
  160. item.y = -100 * settings.traffic - 150;
  161. }
  162. })
  163. }
  164. function playGame() {
  165. if (settings.start) {
  166. score.innerHTML = 'SCORE: 0';
  167. moveRoad();
  168. if (document.querySelector('.enemy')) {
  169. moveEnemy()
  170. settings.score += settings.speed * (1.5 - settings.traffic / 10);
  171. score.innerHTML = 'SCORE: ' + Math.floor(settings.score)
  172. }
  173. if (keys.ArrowLeft && settings.x > 0) {
  174. settings.x -= settings.speed;
  175. }
  176. if (keys.ArrowRight && settings.x < (gameArea.offsetWidth - car.offsetWidth)) {
  177. settings.x += settings.speed;
  178. }
  179. if (keys.ArrowDown && settings.y < (gameArea.offsetHeight - car.offsetHeight)) {
  180. settings.y += settings.speed;
  181. }
  182. if (keys.ArrowUp && settings.y > 0) {
  183. settings.y -= settings.speed;
  184. }
  185. car.style.left = settings.x + 'px';
  186. car.style.top = settings.y + 'px';
  187. requestAnimationFrame(playGame);
  188. }
  189. }
  190. function startRun(event) {
  191. if (keys.hasOwnProperty(event.key)) {
  192. event.preventDefault();
  193. keys[event.key] = true;
  194. }
  195. }
  196. function stopRun(event) {
  197. if (keys.hasOwnProperty(event.key)) {
  198. event.preventDefault();
  199. keys[event.key] = false;
  200. }
  201. }