From af679f88551205753885068666ff4e9cad845b8c Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Sun, 9 Mar 2025 21:59:57 +0100 Subject: add emed start button --- src/components/MorseGame.tsx | 171 ++++++++++++++++++++++----------------- src/components/OptionButtons.css | 33 ++++++++ 2 files changed, 132 insertions(+), 72 deletions(-) diff --git a/src/components/MorseGame.tsx b/src/components/MorseGame.tsx index 0a3b902..c140e92 100644 --- a/src/components/MorseGame.tsx +++ b/src/components/MorseGame.tsx @@ -24,6 +24,7 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { const [wpm, setWpm] = useState(20); const [showHints, setShowHints] = useState(false); const [selectedOption, setSelectedOption] = useState(null); + const [gameStarted, setGameStarted] = useState(!embed); // Auto-start if not in embed mode const actxRef = useRef(null); const optionButtonsRef = useRef>([null, null, null, null]); @@ -43,10 +44,12 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { actxRef.current = cw.initAudioContext({ tone: 600 }); isInitializedRef.current = true; - // Start the first round after audio context is initialized - setTimeout(() => { - newRound(); - }, 100); + // Start the first round after audio context is initialized if not in embed mode or if game already started + if (gameStarted) { + setTimeout(() => { + newRound(); + }, 100); + } } catch (error) { console.error('Failed to initialize audio context:', error); } @@ -56,14 +59,17 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { return () => { if (actxRef.current) { try { - actxRef.current.close(); + // Check if close method exists before calling it + if (typeof actxRef.current.close === 'function') { + actxRef.current.close(); + } actxRef.current = null; } catch (error) { - console.error('Error closing audio context:', error); + console.error('Error cleaning up audio context:', error); } } }; - }, []); + }, [gameStarted]); // Generate a new round const newRound = () => { @@ -222,80 +228,101 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { } }; + // Start the game (for embed mode) + const startGame = () => { + setGameStarted(true); + setTimeout(() => { + newRound(); + }, 100); + }; + return (
-
-
-
-
Score:
-
- {score} / {totalPlayed} + {embed && !gameStarted ? ( +
+ +
+ ) : ( + <> +
+
+
+
Score:
+
+ {score} / {totalPlayed} +
+
+
-
-
-
-
-
-

Listen to the Morse code and select the correct character

-
+
+
+

Listen to the Morse code and select the correct character

+
-
- {options.map((option, index) => ( - - ))} -
+
+ {options.map((option, index) => ( + + ))} +
-
-
- - -
-
- setShowHints(e.target.checked)} - /> - -
-
+
+
+ + +
+
+ setShowHints(e.target.checked)} + /> + +
+
- {!embed && ( -
-

Pro Tip: Use your keyboard to select options faster!

+ {!embed && ( +
+

Pro Tip: Use your keyboard to select options faster!

+
+ )}
- )} -
+ + )}
); }; diff --git a/src/components/OptionButtons.css b/src/components/OptionButtons.css index a513337..e01af4b 100644 --- a/src/components/OptionButtons.css +++ b/src/components/OptionButtons.css @@ -100,3 +100,36 @@ transform: rotate(360deg); } } + +/* Start game button for embed mode */ +.start-game-container { + display: flex; + justify-content: center; + align-items: center; + min-height: 200px; + padding: 20px; +} + +.start-game-button { + background-color: #4a6fa5; + color: white; + border: none; + border-radius: 8px; + padding: 15px 30px; + font-size: 18px; + font-weight: bold; + cursor: pointer; + transition: background-color 0.3s, transform 0.2s; + box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); +} + +.start-game-button:hover { + background-color: #3a5a8c; + transform: translateY(-2px); + box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15); +} + +.start-game-button:active { + transform: translateY(1px); + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); +} -- cgit v1.3.1