import React, { useState, useEffect } from 'react'; const KidsLearningGame = () => { const [gameType, setGameType] = useState('math'); const [score, setScore] = useState(0); const [currentQuestion, setCurrentQuestion] = useState(null); const [userAnswer, setUserAnswer] = useState(''); const [feedback, setFeedback] = useState(''); const [showCelebration, setShowCelebration] = useState(false); const [isSpeaking, setIsSpeaking] = useState(false); // Simple words for spelling practice const words = ['cat', 'dog', 'sun', 'car', 'bat', 'hat', 'cup', 'bee', 'egg', 'pig']; // Speech function const speak = (text, rate = 0.8) => { if ('speechSynthesis' in window) { // Cancel any ongoing speech window.speechSynthesis.cancel(); const utterance = new SpeechSynthesisUtterance(text); utterance.rate = rate; // Slower for kids utterance.pitch = 1.1; // Slightly higher pitch utterance.volume = 1; // Try to use a child-friendly voice if available const voices = window.speechSynthesis.getVoices(); const preferredVoice = voices.find(voice => voice.name.includes('Google') || voice.name.includes('Microsoft') || voice.lang.startsWith('en') ); if (preferredVoice) { utterance.voice = preferredVoice; } utterance.onstart = () => setIsSpeaking(true); utterance.onend = () => setIsSpeaking(false); window.speechSynthesis.speak(utterance); } }; // Speak encouragement phrases const encouragementPhrases = [ "Great job!", "Wonderful!", "Amazing!", "You're so smart!", "Fantastic!", "Super!", "Excellent!", "Way to go!" ]; const getRandomEncouragement = () => { return encouragementPhrases[Math.floor(Math.random() * encouragementPhrases.length)]; }; // Generate math questions const generateMathQuestion = () => { const num1 = Math.floor(Math.random() * 10) + 1; const num2 = Math.floor(Math.random() * 10) + 1; const operation = Math.random() > 0.5 ? '+' : '-'; if (operation === '+') { const question = { question: `${num1} + ${num2}`, answer: num1 + num2, type: 'math', spokenText: `Let's add! ${num1} plus ${num2} equals what?` }; return question; } else { // Make sure subtraction doesn't result in negative numbers const larger = Math.max(num1, num2); const smaller = Math.min(num1, num2); const question = { question: `${larger} - ${smaller}`, answer: larger - smaller, type: 'math', spokenText: `Let's subtract! ${larger} minus ${smaller} equals what?` }; return question; } }; // Generate spelling questions const generateSpellingQuestion = () => { const word = words[Math.floor(Math.random() * words.length)]; return { question: `How do you spell: ${word.toUpperCase()}?`, answer: word.toLowerCase(), type: 'spelling', word: word, spokenText: `Let's spell! How do you spell ${word}? ${word.split('').join(', ')}` }; }; // Initialize or generate new question const newQuestion = () => { setUserAnswer(''); setFeedback(''); let question; if (gameType === 'math') { question = generateMathQuestion(); } else { question = generateSpellingQuestion(); } setCurrentQuestion(question); // Speak the question after a short delay setTimeout(() => { speak(question.spokenText); }, 500); }; // Repeat the current question const repeatQuestion = () => { if (currentQuestion && currentQuestion.spokenText) { speak(currentQuestion.spokenText); } }; // Check answer const checkAnswer = () => { if (!userAnswer.trim()) return; const isCorrect = userAnswer.toLowerCase().trim() === currentQuestion.answer.toString().toLowerCase(); if (isCorrect) { setScore(score + 1); const encouragement = getRandomEncouragement(); setFeedback(`🎉 ${encouragement} That's correct!`); setShowCelebration(true); // Speak positive feedback speak(`${encouragement}! That's correct! The answer is ${currentQuestion.answer}!`); setTimeout(() => { setShowCelebration(false); speak("Let's try another one!"); setTimeout(() => { newQuestion(); }, 1000); }, 2500); } else { setFeedback(`Good try! The answer is ${currentQuestion.answer}. Let's try another one!`); // Speak corrective feedback speak(`Good try! The correct answer is ${currentQuestion.answer}. Let's try another one!`); setTimeout(() => { newQuestion(); }, 4000); } }; // Switch game type const switchGameType = (type) => { setGameType(type); setUserAnswer(''); setFeedback(''); // Announce the game mode if (type === 'math') { speak("Let's practice math! We can add and subtract numbers!"); } else { speak("Let's practice spelling! We'll spell fun words together!"); } }; // Load voices and initialize first question useEffect(() => { // Load voices if ('speechSynthesis' in window) { const loadVoices = () => { window.speechSynthesis.getVoices(); }; if (window.speechSynthesis.onvoiceschanged !== undefined) { window.speechSynthesis.onvoiceschanged = loadVoices; } loadVoices(); } // Welcome message setTimeout(() => { speak("Welcome to Learning Fun! Let's practice math together!"); }, 1000); }, []); // Generate new question when game type changes useEffect(() => { if (currentQuestion === null) { newQuestion(); } else { newQuestion(); } }, [gameType]); // Handle enter key const handleKeyPress = (e) => { if (e.key === 'Enter') { checkAnswer(); } }; return (
{/* Header */}

🌟 Learning Fun! 🌟

Score: {score} ⭐
{isSpeaking && (
🔊 Speaking...
)}
{/* Game Type Switcher */}
{/* Game Area */}
{currentQuestion && ( <>

{currentQuestion.question}

{gameType === 'spelling' && currentQuestion.word && (
{currentQuestion.word === 'cat' && '🐱'} {currentQuestion.word === 'dog' && '🐶'} {currentQuestion.word === 'sun' && '☀️'} {currentQuestion.word === 'car' && '🚗'} {currentQuestion.word === 'bat' && '🦇'} {currentQuestion.word === 'hat' && '👒'} {currentQuestion.word === 'cup' && '☕'} {currentQuestion.word === 'bee' && '🐝'} {currentQuestion.word === 'egg' && '🥚'} {currentQuestion.word === 'pig' && '🐷'}
)}
setUserAnswer(e.target.value)} onKeyPress={handleKeyPress} className="text-2xl text-center p-4 border-4 border-blue-300 rounded-2xl w-48 focus:border-blue-500 focus:outline-none" placeholder="Your answer" autoFocus />
)} {/* Feedback */} {feedback && (
{feedback}
)} {/* Celebration Animation */} {showCelebration && (
🎉
Awesome!
You're doing great!
)}
{/* Instructions */}

🔊 Listen to the questions and practice speaking along!
Switch between Math and Spelling to practice both skills!
Click "Say it Again!" if you want to hear the question repeated.
You're doing amazing! Keep learning and having fun! 🌈

); }; export default KidsLearningGame;