repository to manage all files for 1_2_oder_3 interaction game for Inf2/2 Interaktionen SoSe23 from Engert, Caliskan and Bachiri
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { useEffect, useState} from "react";
  2. import './App.css';
  3. import Header from "./components/Header";
  4. import Body from "./components/Body";
  5. import question from "./components/Question";
  6. function App() {
  7. const [timeLeft, setTimeLeft] = useState(10*1000);
  8. const [timerRunning, setTimerRunning] = useState(false);
  9. const [questionCount, setQuestionCount] = useState(0);
  10. const [teamsize, setTeamSize] = useState(0);
  11. useEffect(() => {
  12. let timer;
  13. if (timerRunning && timeLeft > 0) {
  14. timer = setTimeout(() => {
  15. setTimeLeft(timeLeft-10);
  16. }, 10);}
  17. else if (timeLeft === 0) {
  18. alert("Zeit ist um !");
  19. setTimerRunning(false);
  20. setTimeLeft(10);
  21. }
  22. return () => clearTimeout(timer);
  23. }, [timeLeft, timerRunning]);
  24. function formatTime(time) {
  25. const seconds = Math.floor((time / 1000) % 60).toString().padStart(2, '0');
  26. return `${seconds} s`;
  27. }
  28. const startQuestion = () => {
  29. if (questionCount <= 5){
  30. setQuestionCount(questionCount+1)
  31. setTimeLeft(10*1000);
  32. setTimerRunning(true);
  33. }
  34. else {
  35. alert("Spiel vorbei !");
  36. }
  37. }
  38. const stopTimer = () => {
  39. setTimerRunning(false)
  40. setTimeLeft(10*1000);
  41. }
  42. return (<>
  43. <Header timerRunning={timerRunning} timerContent={formatTime(timeLeft)} questionCount={questionCount}/>
  44. <Body startQuestion={startQuestion} timerRunning={timerRunning} stopTimer={stopTimer} setQuestionCount={setQuestionCount} setTeamSize={setTeamSize} teamsize={teamsize}/>
  45. </>
  46. );
  47. }
  48. export default App;