Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Opadające wyrazy
AQuatro
post 25.01.2024, 09:44:49
Post #1





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 29.09.2004

Ostrzeżenie: (0%)
-----


Witam.
Próbuję robić program, gdzie spadają mi wyrazy a ja mam wpisać odpowiedź. Na razie "a".
Niestety po dwóch wpisanych odpowiedziach program się blokuje.
Gdybym zwiększył ilość haseł, to jak zostaną 2 ostatnie, to program się zablokuje.
Jeżeli pozwoliłbym, żeby na ekranie było widoczne więcej haseł niż jedno, to wtedy program się nie zablokuje.
Co to może być?
Zamieszczam kod:
  1.  
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Falling</title>
  7. body {
  8. overflow: hidden;
  9. margin: 0;
  10. font-family: Arial, sans-serif;
  11. }
  12.  
  13. #container {
  14. position: relative;
  15. height: 100vh;
  16. background-color: #f0f0f0;
  17.  
  18.  
  19. }
  20.  
  21. #formContainer {
  22. position: fixed;
  23. bottom: 0;
  24. left: 50%;
  25. transform: translateX(-50%);
  26. text-align: center;
  27. padding: 10px;
  28. background-color: white;
  29. border: 1px solid #ccc;
  30. }
  31.  
  32. #wordInput {
  33. padding: 5px;
  34. font-size: 16px;
  35. }
  36.  
  37. #score {
  38. font-size: 20px;
  39. font-weight: bold;
  40. }
  41.  
  42. .word {
  43. position: absolute;
  44. font-size: 24px;
  45. font-weight: bold;
  46. }
  47.  
  48. #line {
  49. position: fixed;
  50. bottom: 80px;
  51. left: 0;
  52. width: 100%;
  53. height: 2px;
  54. background-color: #000;
  55. }
  56.  
  57. #congratulations {
  58. position: fixed;
  59. top: 50%;
  60. left: 50%;
  61. transform: translate(-50%, -50%);
  62. font-size: 36px;
  63. font-weight: bold;
  64. display: none;
  65. }
  66.  
  67. #remainingWords {
  68. font-size: 16px;
  69. font-weight: bold;
  70. margin-left: 10px;
  71. margin-right: 20px;
  72. }
  73. </style>
  74. </head>
  75.  
  76.  
  77.  
  78.  
  79. <body onload="document.getElementById('wordInput').focus()" style="background-color: white; color: black;">
  80. <div id="container"></div>
  81. <div id="line"></div>
  82. <div id="formContainer">
  83. <label for="wordInput">Jajko:</label>
  84. <span id="score">0</span>
  85. <span id="remainingWords">0</span>
  86. <input type="text" id="wordInput" onfocus="this.focus(); this.select()">
  87. </div>
  88. <div id="congratulations">Gratulacje!</div>
  89.  
  90.  
  91.  
  92.  
  93. let words = [
  94. { word: 'wyr1', color: 'a' },
  95. { word: 'wyr2', color: 'a' },
  96. { word: 'wyr3', color: 'a' },
  97. { word: 'wyr4', color: 'a' }
  98. ];
  99.  
  100.  
  101. const container = document.getElementById('container');
  102. const formContainer = document.getElementById('formContainer');
  103. const wordInput = document.getElementById('wordInput');
  104. const scoreElement = document.getElementById('score');
  105. const remainingWordsElement = document.getElementById('remainingWords');
  106. const line = document.getElementById('line');
  107. const congratulations = document.getElementById('congratulations');
  108. let score = 0;
  109.  
  110.  
  111. function createWordElement(wordObject) {
  112. const wordElement = document.createElement('div');
  113. wordElement.className = 'word';
  114. wordElement.textContent = wordObject.word;
  115. wordElement.style.left = `${Math.random() * (container.offsetWidth - 100)}px`; //30
  116.  
  117. container.appendChild(wordElement);
  118.  
  119. const fallInterval = setInterval(() => {
  120. const rect = wordElement.getBoundingClientRect();
  121. if (rect.bottom < container.offsetHeight) {
  122. wordElement.style.top = `${rect.top + 0.2}px`;
  123. } else {
  124. clearInterval(fallInterval);
  125. wordElement.remove();
  126. updateScore(-1);
  127. updateRemainingWords();
  128. }
  129.  
  130.  
  131. const lineRect = line.getBoundingClientRect();
  132. if (rect.bottom >= lineRect.top && rect.top <= lineRect.bottom && rect.left >= lineRect.left && rect.right <= lineRect.right) {
  133. clearInterval(fallInterval);
  134. wordElement.remove();
  135. updateScore(-1);
  136. removeWordFromList(wordObject);
  137.  
  138.  
  139. if (words.length === 0) {
  140. showCongratulations();
  141. }
  142.  
  143. }
  144. }, 5);
  145.  
  146.  
  147. wordInput.addEventListener('input', function handleInput() {
  148. if (wordInput.value.toLowerCase() === wordObject.color.toLowerCase()) {
  149. clearInterval(fallInterval);
  150. wordElement.remove();
  151. updateScore(1);
  152. wordInput.value = '';
  153. wordInput.removeEventListener('input', handleInput);
  154. removeWordFromList(wordObject);
  155. updateRemainingWords();
  156.  
  157.  
  158. if (words.length === 0) {
  159. showCongratulations();
  160. }
  161. }
  162. });
  163. }
  164.  
  165.  
  166.  
  167.  
  168. function removeWordFromList(wordObject) {
  169. if (wordObject) {
  170. words = words.filter(word => word !== wordObject);
  171. }
  172. //console.log(words);
  173. }
  174.  
  175. function updateScore(delta) {
  176. score += delta;
  177. scoreElement.textContent = score;
  178. }
  179.  
  180. function updateRemainingWords() {
  181. remainingWordsElement.textContent = words.length;
  182. //console.log(words.length);
  183. }
  184.  
  185. function showCongratulations() {
  186. congratulations.style.display = 'block';
  187. }
  188.  
  189.  
  190. function startGame() {
  191. let wordIndex = 0;
  192.  
  193. setInterval(() => {
  194. // console.log(words.length);
  195. // console.log(wordIndex);
  196. if (words.length > 0) {
  197. const wordObject = words[wordIndex];
  198. createWordElement(wordObject);
  199. wordIndex = (wordIndex + 1) % words.length;
  200.  
  201.  
  202. }
  203. }, 5000);
  204. }
  205.  
  206. startGame();
  207.  
  208. </script>
  209.  
  210.  
  211.  
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
23 Użytkowników czyta ten temat (23 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 07:19