From 8ed14f44fda0ffdb9343b784faeb3ce234f7c609 Mon Sep 17 00:00:00 2001 From: instantfred Date: Tue, 20 Aug 2024 14:58:34 -0600 Subject: [PATCH] Add end game button --- src/SounddittoGame.js | 157 ++++++++++++++++++++++++++++-------------- 1 file changed, 107 insertions(+), 50 deletions(-) diff --git a/src/SounddittoGame.js b/src/SounddittoGame.js index 536c8cc..b12543f 100644 --- a/src/SounddittoGame.js +++ b/src/SounddittoGame.js @@ -1,34 +1,34 @@ -import React, { useState, useEffect } from 'react'; -import { Button } from "@/components/ui/button" -import { Card, CardContent, CardHeader } from "@/components/ui/card" -import { Switch } from "@/components/ui/switch" -import { Info } from "lucide-react" -import englishWords from './lib/englishWords'; -import spanishWords from './lib/spanishWords'; -import Footer from './components/ui/Footer'; -import InstructionsModal from './components/modals/InstructionsModal'; +import React, { useState, useEffect } from "react"; +import { Button } from "@/components/ui/button"; +import { Card, CardContent, CardHeader } from "@/components/ui/card"; +import { Switch } from "@/components/ui/switch"; +import { Info } from "lucide-react"; +import englishWords from "./lib/englishWords"; +import spanishWords from "./lib/spanishWords"; +import Footer from "./components/ui/Footer"; +import InstructionsModal from "./components/modals/InstructionsModal"; const GAME_DURATION = 60; // Game duration in seconds const SKIP_PENALTY = 5; // Time penalty for skipping in seconds const SounddittoGame = () => { - const [gameState, setGameState] = useState('waiting'); - const [currentWords, setCurrentWords] = useState({ easy: '', hard: '' }); + const [gameState, setGameState] = useState("waiting"); + const [currentWords, setCurrentWords] = useState({ easy: "", hard: "" }); const [score, setScore] = useState(0); const [timeLeft, setTimeLeft] = useState(GAME_DURATION); - const [language, setLanguage] = useState('english'); + const [language, setLanguage] = useState("english"); const [availableWords, setAvailableWords] = useState([]); const [isInstructionsOpen, setIsInstructionsOpen] = useState(false); const [isTimerEnabled, setIsTimerEnabled] = useState(true); useEffect(() => { let timer; - if (gameState === 'playing' && isTimerEnabled) { + if (gameState === "playing" && isTimerEnabled) { timer = setInterval(() => { setTimeLeft((prevTime) => { if (prevTime <= 1) { clearInterval(timer); - setGameState('finished'); + setGameState("finished"); return 0; } return prevTime - 1; @@ -39,9 +39,10 @@ const SounddittoGame = () => { }, [gameState, isTimerEnabled]); const startGame = () => { - const words = language === 'english' ? [...englishWords] : [...spanishWords]; + const words = + language === "english" ? [...englishWords] : [...spanishWords]; setAvailableWords(words); - setGameState('playing'); + setGameState("playing"); setScore(0); if (isTimerEnabled) { setTimeLeft(GAME_DURATION); @@ -49,9 +50,13 @@ const SounddittoGame = () => { nextWord(words); }; + const endGame = () => { + setGameState("finished"); + }; + const nextWord = (words = availableWords) => { if (words.length <= 1) { - setGameState('finished'); + setGameState("finished"); return; } const randomIndex = Math.floor(Math.random() * words.length); @@ -67,40 +72,48 @@ const SounddittoGame = () => { }; const handleGotIt = (difficulty) => { - setScore((prevScore) => prevScore + (difficulty === 'easy' ? 1 : 2)); + setScore((prevScore) => prevScore + (difficulty === "easy" ? 1 : 2)); nextWord(); }; const toggleLanguage = () => { - setLanguage(prevLang => prevLang === 'english' ? 'spanish' : 'english'); + setLanguage((prevLang) => (prevLang === "english" ? "spanish" : "english")); }; const toggleTimer = () => { - setIsTimerEnabled(prevState => !prevState); + setIsTimerEnabled((prevState) => !prevState); }; const renderLanguageToggle = () => (
- EN + + EN + - ES + + ES +
); const renderTimerToggle = () => (
- {language === 'english' ? 'Timer' : 'Temporizador'} + {language === "english" ? "Timer" : "Temporizador"}
@@ -126,7 +139,7 @@ const SounddittoGame = () => { - {gameState === 'waiting' && ( + {gameState === "waiting" && ( <> {renderLanguageToggle()} {renderTimerToggle()} @@ -134,59 +147,98 @@ const SounddittoGame = () => { )} - {gameState === 'waiting' && ( - )} - {gameState === 'playing' && ( + {gameState === "playing" && (

- {language === 'english' ? 'Choose a word:' : 'Elige una palabra:'} + {language === "english" + ? "Choose a word:" + : "Elige una palabra:"}

- -
-

- {language === 'english' ? 'Score:' : 'Puntos:'} + {language === "english" ? "Score:" : "Puntos:"} {score}

- {isTimerEnabled && ( + {isTimerEnabled ? (

- {language === 'english' ? 'Time:' : 'Tiempo:'} + {language === "english" ? "Time:" : "Tiempo:"} {timeLeft}s

+ ) : ( + )}
)} - {gameState === 'finished' && ( + {gameState === "finished" && (

- {language === 'english' ? 'Game Over!' : '¡Juego Terminado!'} + {language === "english" ? "Game Over!" : "¡Juego Terminado!"}

- {language === 'english' ? 'Final Score:' : 'Puntuación Final:'} {score} + {language === "english" + ? "Final Score:" + : "Puntuación Final:"}{" "} + {score}

-
)} @@ -194,9 +246,14 @@ const SounddittoGame = () => {