Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const cat = document.getElementById('cat');
+ const mouse = document.getElementById('mouse');
+ const startBtn = document.getElementById('start-btn');
+ const resetBtn = document.getElementById('reset-btn');
+ const scoreElement = document.getElementById('score');
+ const container = document.getElementById('game-container');
+
+ let score = 0;
+ let gameActive = false;
+ let mouseInterval;
+
+ function getRandomPosition(element) {
+ const containerRect = container.getBoundingClientRect();
+ const elementRect = element.getBoundingClientRect();
+
+ return {
+ x: Math.random() * (containerRect.width - elementRect.width),
+ y: Math.random() * (containerRect.height - elementRect.height)
+ };
+ }
+
+ function moveMouse() {
+ const position = getRandomPosition(mouse);
+ mouse.style.transform = `translate(${position.x}px, ${position.y}px)`;
+ }
+
+ function updateCatPosition(e) {
+ if (!gameActive) return;
+
+ const containerRect = container.getBoundingClientRect();
+ const catRect = cat.getBoundingClientRect();
+ const mouseRect = mouse.getBoundingClientRect();
+
+ let x = e.clientX - containerRect.left - catRect.width / 2;
+ let y = e.clientY - containerRect.top - catRect.height / 2;
+
+ // Boundary checking
+ x = Math.max(0, Math.min(x, containerRect.width - catRect.width));
+ y = Math.max(0, Math.min(y, containerRect.height - catRect.height));
+
+ cat.style.transform = `translate(${x}px, ${y}px)`;
+
+ // Check collision
+ if (isCollision(catRect, mouseRect)) {
+ score++;
+ scoreElement.textContent = score;
+ moveMouse();
+ }
+ }
+
+ function isCollision(rect1, rect2) {
+ return !(rect1.right < rect2.left ||
+ rect1.left > rect2.right ||
+ rect1.bottom < rect2.top ||
+ rect1.top > rect2.bottom);
+ }
+
+ function startGame() {
+ gameActive = true;
+ score = 0;
+ scoreElement.textContent = score;
+ moveMouse();
+ mouseInterval = setInterval(moveMouse, 2000);
+ startBtn.disabled = true;
+ }
+
+ function resetGame() {
+ gameActive = false;
+ clearInterval(mouseInterval);
+ score = 0;
+ scoreElement.textContent = score;
+ cat.style.transform = 'translate(0, 0)';
+ mouse.style.transform = 'translate(0, 0)';
+ startBtn.disabled = false;
+ }
+
+ // Event listeners
+ container.addEventListener('mousemove', updateCatPosition);
+ container.addEventListener('touchmove', (e) => {
+ e.preventDefault();
+ updateCatPosition(e.touches[0]);
+ });
+ startBtn.addEventListener('click', startGame);
+ resetBtn.addEventListener('click', resetGame);
+
+ // Mobile touch events
+ container.addEventListener('touchstart', (e) => e.preventDefault());
+ container.addEventListener('touchend', (e) => e.preventDefault());
+ });