Advertisement
Guest User

Untitled

a guest
Sep 15th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.12 KB | Source Code | 0 0
  1. <script>
  2.  
  3. var gameContainer = document.getElementById("game-container");
  4.  
  5. var catcher = document.getElementById("catcher");
  6.  
  7. var endMessage = document.getElementById("end-message");
  8.  
  9. var scoreDisplay = document.getElementById("score");
  10.  
  11. var score = 0;
  12.  
  13. var missedCubes = 0;
  14.  
  15. var cubes = [];
  16.  
  17. var initialInterval = 1500;
  18.  
  19. var intervalDecreaseRate = 0.9;
  20.  
  21. var minInterval = 500;
  22.  
  23. var speedIncreaseRate = 0.1;
  24.  
  25. var cubeSpeed = 1.0;
  26.  
  27. var collectedCubes = 0;
  28.  
  29. var colorChangeInterval = 500;
  30.  
  31. var changingCubeColors = true;
  32.  
  33. var paddleShape = 'rectangle';
  34.  
  35. var paddleColor = 'blue';
  36.  
  37. var mainMenu = document.getElementById("main-menu");
  38.  
  39. var settingsMenu = document.getElementById("settings-menu");
  40.  
  41. var controlsMenu = document.getElementById("controls-menu");
  42.  
  43. var howToPlayMenu = document.getElementById("how-to-play-menu");
  44.  
  45. var objectCreationInterval;
  46.  
  47. function startGame() {
  48.  
  49. mainMenu.style.display = "none";
  50.  
  51. settingsMenu.style.display = "none";
  52.  
  53. controlsMenu.style.display = "none";
  54.  
  55. howToPlayMenu.style.display = "none";
  56.  
  57. gameContainer.style.display = "block";
  58.  
  59. catcher.style.display = "block";
  60.  
  61. score = -4;
  62.  
  63. scoreDisplay.textContent = score;
  64.  
  65. collectedCubes = 0;
  66.  
  67. cubeSpeed = 1.0;
  68.  
  69. colorChangeInterval = 500;
  70.  
  71. catcher.style.backgroundColor = paddleColor;
  72.  
  73. if (paddleShape === 'rounded') {
  74.  
  75. catcher.classList.add('rounded');
  76.  
  77. } else {
  78.  
  79. catcher.classList.remove('rounded');
  80.  
  81. }
  82.  
  83. initializeGame();
  84.  
  85. }
  86.  
  87. function showSettings() {
  88.  
  89. mainMenu.style.display = "none";
  90.  
  91. settingsMenu.style.display = "block";
  92.  
  93. }
  94.  
  95. function hideSettings() {
  96.  
  97. settingsMenu.style.display = "none";
  98.  
  99. mainMenu.style.display = "block";
  100.  
  101. }
  102.  
  103. function showControls() {
  104.  
  105. mainMenu.style.display = "none";
  106.  
  107. controlsMenu.style.display = "block";
  108.  
  109. }
  110.  
  111. function hideControls() {
  112.  
  113. controlsMenu.style.display = "none";
  114.  
  115. mainMenu.style.display = "block";
  116.  
  117. }
  118.  
  119. function showHowToPlay() {
  120.  
  121. mainMenu.style.display = "none";
  122.  
  123. howToPlayMenu.style.display = "block";
  124.  
  125. }
  126.  
  127. function hideHowToPlay() {
  128.  
  129. howToPlayMenu.style.display = "none";
  130.  
  131. mainMenu.style.display = "block";
  132.  
  133. }
  134.  
  135. function setPaddleColor(color) {
  136.  
  137. paddleColor = color;
  138.  
  139. catcher.style.backgroundColor = paddleColor;
  140.  
  141. hideColorPalette();
  142.  
  143. }
  144.  
  145. function toggleColorPalette() {
  146.  
  147. var colorPalette = document.querySelector(".color-palette");
  148.  
  149. colorPalette.style.display = colorPalette.style.display === "flex" ? "none" : "flex";
  150.  
  151. }
  152.  
  153. function hideColorPalette() {
  154.  
  155. var colorPalette = document.querySelector(".color-palette");
  156.  
  157. colorPalette.style.display = "none";
  158.  
  159. }
  160.  
  161. function togglePaddleShape() {
  162.  
  163. paddleShape = (paddleShape === 'rectangle') ? 'rounded' : 'rectangle';
  164.  
  165. catcher.classList.toggle('rounded', paddleShape === 'rounded');
  166.  
  167. highlightText('Zmień kształt paletki');
  168.  
  169. }
  170.  
  171. function highlightText(menuItemText) {
  172.  
  173. var menuItem = Array.from(document.querySelectorAll('.menu-item')).find(item => item.textContent.trim() === menuItemText);
  174.  
  175. if (menuItem) {
  176.  
  177. menuItem.classList.add('highlight-green');
  178.  
  179. setTimeout(function() {
  180.  
  181. menuItem.classList.remove('highlight-green');
  182.  
  183. }, 200);
  184.  
  185. }
  186.  
  187. }
  188.  
  189. function toggleCubeColorChange() {
  190.  
  191. changingCubeColors = !changingCubeColors;
  192.  
  193. document.getElementById("toggle-color-change").textContent = changingCubeColors ? "Przestań zmieniać kolory kwadracików" : "Zacznij zmieniać kolory kwadracików";
  194.  
  195. cubes.forEach(cube => {
  196.  
  197. if (changingCubeColors) {
  198.  
  199. startCubeColorChange(cube);
  200.  
  201. } else {
  202.  
  203. stopCubeColorChange(cube);
  204.  
  205. }
  206.  
  207. });
  208.  
  209. console.log('Toggled cube color change. New state:', changingCubeColors);
  210.  
  211. }
  212.  
  213. function startCubeColorChange(cube) {
  214.  
  215. const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
  216.  
  217. let currentColorIndex = 0;
  218.  
  219. // Clear any existing interval
  220.  
  221. if (cube.colorChangeIntervalId) {
  222.  
  223. clearInterval(cube.colorChangeIntervalId);
  224.  
  225. }
  226.  
  227. cube.colorChangeIntervalId = setInterval(() => {
  228.  
  229. currentColorIndex = (currentColorIndex + 1) % colors.length;
  230.  
  231. cube.style.backgroundColor = colors[currentColorIndex];
  232.  
  233. }, colorChangeInterval);
  234.  
  235. console.log('Started color change for cube:', cube, 'Interval ID:', cube.colorChangeIntervalId);
  236.  
  237. }
  238.  
  239. function stopCubeColorChange(cube) {
  240.  
  241. if (cube.colorChangeIntervalId) {
  242.  
  243. console.log('Clearing interval for cube:', cube, 'Interval ID:', cube.colorChangeIntervalId);
  244.  
  245. clearInterval(cube.colorChangeIntervalId);
  246.  
  247. cube.colorChangeIntervalId = undefined; // Clear the interval ID
  248.  
  249. cube.style.backgroundColor = 'red'; // Reset color to red
  250.  
  251. } else {
  252.  
  253. console.log('No interval to clear for cube:', cube);
  254.  
  255. }
  256.  
  257. }
  258.  
  259. function adjustColorChangeSpeed(factor) {
  260.  
  261. colorChangeInterval = Math.max(colorChangeInterval * factor, 100);
  262.  
  263. cubes.forEach(cube => {
  264.  
  265. if (changingCubeColors && cube.colorChangeIntervalId) {
  266.  
  267. stopCubeColorChange(cube);
  268.  
  269. startCubeColorChange(cube);
  270.  
  271. }
  272.  
  273. });
  274.  
  275. }
  276.  
  277. function adjustObjectCreationInterval() {
  278.  
  279. if (objectCreationInterval) {
  280.  
  281. }
  282.  
  283. var newInterval = initialInterval;
  284.  
  285. if (collectedCubes >= 1) {
  286.  
  287. newInterval *= 0.001; // More frequent
  288.  
  289. }
  290.  
  291. newInterval = Math.max(newInterval * intervalDecreaseRate, minInterval);
  292.  
  293. objectCreationInterval = setInterval(createObject, newInterval);
  294.  
  295. clearInterval(objectCreationInterval);
  296.  
  297. }
  298.  
  299. function createObject() {
  300.  
  301. var object = document.createElement("div");
  302.  
  303. object.className = "object";
  304.  
  305. var containerWidth = gameContainer.offsetWidth;
  306.  
  307. var objectWidth = object.offsetWidth;
  308.  
  309. var maxObjectX = containerWidth - objectWidth;
  310.  
  311. var objectX = Math.floor(Math.random() * maxObjectX);
  312.  
  313. object.style.left = objectX + "px";
  314.  
  315. object.style.top = "0px";
  316.  
  317. object.colorChangeIntervalId = undefined; // Initialize interval ID
  318.  
  319. cubes.push(object);
  320.  
  321. gameContainer.appendChild(object);
  322.  
  323. var objectCaught = false;
  324.  
  325. var animationInterval = setInterval(function() {
  326.  
  327. var objectY = object.offsetTop;
  328.  
  329. var containerHeight = gameContainer.offsetHeight;
  330.  
  331. if (!objectCaught && objectY + object.offsetHeight >= catcher.offsetTop &&
  332.  
  333. objectY <= catcher.offsetTop + catcher.offsetHeight &&
  334.  
  335. isColliding(catcher, object)) {
  336.  
  337. objectCaught = true;
  338.  
  339. clearInterval(animationInterval);
  340.  
  341. gameContainer.removeChild(object);
  342.  
  343. cubes.splice(cubes.indexOf(object), 1);
  344.  
  345. score++;
  346.  
  347. scoreDisplay.textContent = score;
  348.  
  349. cubeSpeed += speedIncreaseRate;
  350.  
  351. collectedCubes++;
  352.  
  353. if (collectedCubes % 5 === 0) {
  354.  
  355. adjustColorChangeSpeed(0.75);
  356.  
  357. }
  358.  
  359. if (collectedCubes % 10 === 0) {
  360.  
  361. adjustObjectCreationInterval();
  362.  
  363. }
  364.  
  365. } else if (objectY >= containerHeight) {
  366.  
  367. clearInterval(animationInterval);
  368.  
  369. gameContainer.removeChild(object);
  370.  
  371. cubes.splice(cubes.indexOf(object), 1);
  372.  
  373. missedCubes++;
  374.  
  375. if (missedCubes >= 1) {
  376.  
  377. endGame();
  378.  
  379. }
  380.  
  381. } else {
  382.  
  383. object.style.top = (objectY + cubeSpeed) + "px";
  384.  
  385. }
  386.  
  387. }, 10);
  388.  
  389. if (changingCubeColors) {
  390.  
  391. startCubeColorChange(object);
  392.  
  393. }
  394.  
  395. }
  396.  
  397. function isColliding(catcher, object) {
  398.  
  399. var catcherRect = catcher.getBoundingClientRect();
  400.  
  401. var objectRect = object.getBoundingClientRect();
  402.  
  403. return !(objectRect.right < catcherRect.left ||
  404.  
  405. objectRect.left > catcherRect.right ||
  406.  
  407. objectRect.bottom < catcherRect.top ||
  408.  
  409. objectRect.top > catcherRect.bottom);
  410.  
  411. }
  412.  
  413. function endGame() {
  414.  
  415. clearInterval(objectCreationInterval);
  416.  
  417. gameContainer.style.display = "none";
  418.  
  419. endMessage.style.display = "block";
  420.  
  421. scoreDisplay.textContent = score;
  422.  
  423. }
  424.  
  425. function restartGame() {
  426.  
  427. endMessage.style.display = "none";
  428.  
  429. clearInterval(objectCreationInterval);
  430.  
  431. startGame();
  432.  
  433. clearInterval(objectCreationInterval);
  434.  
  435. }
  436.  
  437. function goToMenu() {
  438.  
  439. clearInterval(objectCreationInterval);
  440.  
  441. endMessage.style.display = "none";
  442.  
  443. clearInterval(objectCreationInterval);
  444.  
  445. mainMenu.style.display = "block";
  446.  
  447. }
  448.  
  449. function initializeGame() {
  450.  
  451. objectCreationInterval = setInterval(createObject, initialInterval);
  452.  
  453. }
  454.  
  455. document.addEventListener('mousemove', function(event) {
  456.  
  457. var containerRect = gameContainer.getBoundingClientRect();
  458.  
  459. var mouseX = event.clientX - containerRect.left;
  460.  
  461. var catcherWidth = catcher.offsetWidth;
  462.  
  463. var newLeft = Math.max(0, Math.min(mouseX - catcherWidth / 2, gameContainer.offsetWidth - catcherWidth));
  464.  
  465. catcher.style.left = newLeft + 'px';
  466.  
  467. });
  468.  
  469. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement