Changed around line 1
+ const cat = document.querySelector('.cat');
+ const mouse = document.querySelector('.mouse');
+ const scoreDisplay = document.querySelector('.score span');
+ let score = 0;
+
+ function getRandomPosition() {
+ const x = Math.floor(Math.random() * (window.innerWidth - 50));
+ const y = Math.floor(Math.random() * (window.innerHeight - 50));
+ return { x, y };
+ }
+
+ function moveMouse() {
+ const { x, y } = getRandomPosition();
+ mouse.style.left = `${x}px`;
+ mouse.style.top = `${y}px`;
+ }
+
+ function checkCollision() {
+ const catRect = cat.getBoundingClientRect();
+ const mouseRect = mouse.getBoundingClientRect();
+
+ if (
+ catRect.left < mouseRect.right &&
+ catRect.right > mouseRect.left &&
+ catRect.top < mouseRect.bottom &&
+ catRect.bottom > mouseRect.top
+ ) {
+ score++;
+ scoreDisplay.textContent = score;
+ moveMouse();
+ }
+ }
+
+ document.addEventListener('keydown', (e) => {
+ const catRect = cat.getBoundingClientRect();
+ switch (e.key) {
+ case 'ArrowUp':
+ cat.style.top = `${Math.max(catRect.top - 10, 0)}px`;
+ break;
+ case 'ArrowDown':
+ cat.style.top = `${Math.min(catRect.top + 10, window.innerHeight - 50)}px`;
+ break;
+ case 'ArrowLeft':
+ cat.style.left = `${Math.max(catRect.left - 10, 0)}px`;
+ break;
+ case 'ArrowRight':
+ cat.style.left = `${Math.min(catRect.left + 10, window.innerWidth - 50)}px`;
+ break;
+ }
+ checkCollision();
+ });
+
+ moveMouse();