function App(){ const {useState, useEffect, useRef} = React; const [score, setScore] = useState(0); const [timeLeft, setTimeLeft] = useState(30); const [target, setTarget] = useState({x:50,y:50,size:80}); const [running, setRunning] = useState(false); const gameRef = useRef(); useEffect(()=>{ let timer; if(running && timeLeft>0){ timer = setInterval(()=>setTimeLeft(t=>t-1),1000); } else if(timeLeft===0){ setRunning(false); } return ()=>clearInterval(timer); },[running,timeLeft]); useEffect(()=>{ const handleResize = ()=>placeTarget(); window.addEventListener('resize',handleResize); return ()=>window.removeEventListener('resize',handleResize); },[]); function placeTarget(){ const el = gameRef.current; if(!el) return; const rect = el.getBoundingClientRect(); const padding = 40; const size = Math.max(48, Math.floor(48 + Math.random()*80)); const x = Math.floor(Math.random()*(rect.width - size - padding)) + padding/2; const y = Math.floor(Math.random()*(rect.height - size - padding)) + padding/2; setTarget({x,y,size}); } function startGame(){ setScore(0); setTimeLeft(30); placeTarget(); setRunning(true); } function handleTap(e){ if(!running) return; const rect = gameRef.current.getBoundingClientRect(); const x = (e.touches ? e.touches[0].clientX : e.clientX) - rect.left; const y = (e.touches ? e.touches[0].clientY : e.clientY) - rect.top; const dx = x - (target.x + target.size/2); const dy = y - (target.y + target.size/2); const dist = Math.sqrt(dx*dx + dy*dy); if(dist <= target.size/2){ setScore(s=>s+1); // speed up: reduce time slightly setTimeLeft(t=>Math.max(0,t-1)); placeTarget(); } } return (