From d0dce3fce7a5206b000307333d02ff261afc0df1 Mon Sep 17 00:00:00 2001 From: ppouchet <pierre.pouchet@etu.ec-lyon.fr> Date: Mon, 24 Mar 2025 19:16:32 +0100 Subject: [PATCH] Le timer marche finalement ! --- Motus/CMakeLists.txt | 2 ++ Motus/Main.qml | 7 +++++++ Motus/MotusTimer.qml | 32 +++++++++++++++++++++++++++++ Motus/motusgame.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ Motus/motusgame.h | 20 +++++++++++++++++- 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Motus/MotusTimer.qml diff --git a/Motus/CMakeLists.txt b/Motus/CMakeLists.txt index 0b64f40..f5a3c96 100644 --- a/Motus/CMakeLists.txt +++ b/Motus/CMakeLists.txt @@ -44,6 +44,8 @@ qt_add_qml_module(appMotus SOURCES motusgame.h motusgame.cpp RESOURCES mots_francais.txt QML_FILES LangageButton.qml + QML_FILES + QML_FILES MotusTimer.qml ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. diff --git a/Motus/Main.qml b/Motus/Main.qml index 6e20693..a913fc8 100644 --- a/Motus/Main.qml +++ b/Motus/Main.qml @@ -410,6 +410,7 @@ Window { onClicked: { let mot = motusGame.getRandomWord(); console.log("Mot choisi :", mot); + motusGame.startTimer(); // 🕒 démarre le chrono case3._textText = mot; onClicked: case3._textColor="#51c3e1"; } @@ -471,5 +472,11 @@ Window { } } + + MotusTimer { + id: motusTimer + x: 502 + y: 73 + } } } diff --git a/Motus/MotusTimer.qml b/Motus/MotusTimer.qml new file mode 100644 index 0000000..0f159b6 --- /dev/null +++ b/Motus/MotusTimer.qml @@ -0,0 +1,32 @@ +import QtQuick + +Item { + width: 150 + height: 75 + + Rectangle { + id: rectangle + width: 150 + height: 75 + color: { + if (!motusGame) return "#6ceacb"; + return motusGame.remainingTime <= 20 ? "#e86161" : "#6ceacb"; // rouge si <= 20s + } + + Text { + id: _text + width: 150 + height: 75 + text: { + if (!motusGame) return "—"; + let minutes = Math.floor(motusGame.remainingTime / 60); + let seconds = motusGame.remainingTime % 60; + return minutes + "m " + (seconds < 10 ? "0" + seconds : seconds) + "s"; + } + font.pixelSize: 40 + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + color: "black" + } + } +} diff --git a/Motus/motusgame.cpp b/Motus/motusgame.cpp index 18f7e03..babb849 100644 --- a/Motus/motusgame.cpp +++ b/Motus/motusgame.cpp @@ -9,8 +9,35 @@ MotusGame::MotusGame(QObject *parent) : QObject(parent) { loadWords(); + qDebug() << "🔧 Connexion timer établie"; + + + countdownTimer = new QTimer(this); // ✅ rattaché à this + countdownTimer->setInterval(1000); + + connect(countdownTimer, &QTimer::timeout, this, [this]() { + qDebug() << "🔁 Timeout déclenché"; + + if (remainingTime > 0) { + remainingTime--; + emit timerUpdated(); + qDebug() << "⏱️ Tick:" << remainingTime; + + if (remainingTime == 0) { + countdownTimer->stop(); + qDebug() << "⏱️ Temps écoulé !"; + } + } + }); + + + qDebug() << "🔧 Connexion timer établie"; + + } + + void MotusGame::loadWords() { words.clear(); QFile file(dictionnaryChoosed); // ✅ fonctionne avec le .qrc @@ -52,3 +79,24 @@ void MotusGame::setDictionnaryChoosed(const QString &value) { qDebug() << "📁 Nouveau dictionnaire sélectionné :" << dictionnaryChoosed; } } + +void MotusGame::startTimer() { + qDebug() << "🔁 startTimer() appelé"; + + remainingTime = 120; + emit timerUpdated(); + + countdownTimer->stop(); + countdownTimer->start(); + qDebug() << "▶️ Timer actif ?" << countdownTimer->isActive(); + + qDebug() << "▶️ Timer lancé"; + qDebug() << "⏳ remainingTime =" << remainingTime; + + +} + + +int MotusGame::getRemainingTime() const { + return remainingTime; +} diff --git a/Motus/motusgame.h b/Motus/motusgame.h index b075f53..890e811 100644 --- a/Motus/motusgame.h +++ b/Motus/motusgame.h @@ -4,6 +4,8 @@ #include <QObject> #include <QString> #include <QStringList> +#include <QTimer> + class MotusGame : public QObject { Q_OBJECT @@ -13,6 +15,8 @@ class MotusGame : public QObject { WRITE setDictionnaryChoosed NOTIFY dictionnaryChoosedChanged) + Q_PROPERTY(int remainingTime READ getRemainingTime NOTIFY timerUpdated) + public: explicit MotusGame(QObject *parent = nullptr); Q_INVOKABLE QString getRandomWord(); @@ -20,6 +24,11 @@ public: QString getDictionnaryChoosed() const; void setDictionnaryChoosed(const QString &value); + + Q_INVOKABLE void startTimer(); + int getRemainingTime() const; + + private: QStringList words; @@ -27,12 +36,21 @@ private: int letterNumber=7; // Nombre de lettres dans le mot, choisi par l'utilisateur int tryNumber=0; // Nombre d'essais effectués, si tryNumber = nombre max, c'est perdu QString dictionnaryChoosed="Motus\\words_alpha.txt"; // "Motus\\words_alpha.txt" ou "Motus\\mots_francais.txt" - int Timer; // Chronomètre, s'il tombe à 0, c'est perdu + + + + QTimer *countdownTimer; + + int remainingTime = 120; // chrono en secondes void loadWords(); signals: void dictionnaryChoosedChanged(); + void timerUpdated(); + + + }; #endif // MOTUSGAME_H -- GitLab