From dc02b84b33cb0619e23e1adf7b916d73783ff887 Mon Sep 17 00:00:00 2001 From: Gergely Novak Date: Sat, 22 Dec 2018 12:33:14 +0100 Subject: [PATCH] Added time limit --- src/main/java/quiz/QuizController.java | 43 +++++- src/main/java/quiz/Resource.java | 4 +- src/main/java/quiz/TimeLeftCounter.java | 69 +++++++++ src/main/resources/quiz.css | 5 +- src/main/resources/quiz.fxml | 183 +++++++++++++----------- src/main/resources/sounds/bomb.mp3 | Bin 0 -> 56129 bytes src/main/resources/sounds/tick.mp3 | Bin 0 -> 2398 bytes 7 files changed, 209 insertions(+), 95 deletions(-) create mode 100644 src/main/java/quiz/TimeLeftCounter.java create mode 100644 src/main/resources/sounds/bomb.mp3 create mode 100644 src/main/resources/sounds/tick.mp3 diff --git a/src/main/java/quiz/QuizController.java b/src/main/java/quiz/QuizController.java index b6fa54f..3f8d16f 100755 --- a/src/main/java/quiz/QuizController.java +++ b/src/main/java/quiz/QuizController.java @@ -20,12 +20,17 @@ import javafx.util.Duration; import java.io.*; +import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Timer; public class QuizController { + public Pane rootPane; public GridPane mainPane; public Label questionLabel; @@ -78,6 +83,8 @@ public class QuizController { private Integer selectedAnswer = null; private List selectedAnswers = new ArrayList<>(); + private Timer timeLeftTimer; + enum GameState { QUESTION_SHOWN, ANSWERS_SHOWN, @@ -188,12 +195,12 @@ public void answerClicked(MouseEvent mouseEvent) { selectedAnswer = null; if (currentQuestion.areAllWrongAnswersFound(selectedAnswers)) { - mainPane.getStyleClass().add("allWrong"); + rootPane.getStyleClass().add("allWrong"); gameState = GameState.ROUND_OVER; } else if (currentQuestion.oneCorrectAnswerRemaining(selectedAnswers)) { - mainPane.getStyleClass().add("oneRemains"); + rootPane.getStyleClass().add("oneRemains"); } else if (currentQuestion.areAllCorrectAnswersFound(selectedAnswers)) { - mainPane.getStyleClass().add("allCorrect"); + rootPane.getStyleClass().add("allCorrect"); gameState = GameState.ROUND_OVER; } } @@ -234,18 +241,41 @@ public void keyPressed(KeyEvent keyEvent) throws URISyntaxException, IOException hintThreeAnswers(); } break; + case T: + if (gameState == GameState.ANSWERS_SHOWN && transitions.isEmpty() && timeLeftTimer == null) { + TimeLeftCounter timeLeftCounter = new TimeLeftCounter(10, mainPane); + timeLeftTimer = new Timer(); + timeLeftTimer.schedule(timeLeftCounter, 0, 1000); + } + break; + case R: + resetTimer(); + break; } } private void removeStyles() { - ObservableList mainStyles = mainPane.getStyleClass(); - mainStyles.clear(); - mainStyles.addAll("mainPane"); + HashMap panes = new HashMap<>(); + panes.put("rootPane", rootPane); + panes.put("mainPane", mainPane); + for (Map.Entry pane : panes.entrySet()) { + ObservableList styles = pane.getValue().getStyleClass(); + styles.clear(); + styles.addAll(pane.getKey()); + } for (Transition transition : transitions) { transition.stop(); } } + private void resetTimer() { + mainPane.setStyle("-fx-background-color: transparent"); + if (timeLeftTimer != null) { + timeLeftTimer.cancel(); + timeLeftTimer = null; + } + } + private void previousQuestion() { removeStyles(); if (currentQuestionIndex > 0) { @@ -274,6 +304,7 @@ private void selectAnswer(int answer) { if (!styles.contains("selectedAnswerBox")) { styles.add("selectedAnswerBox"); } + resetTimer(); } private void deselectAnswer(int answer) { diff --git a/src/main/java/quiz/Resource.java b/src/main/java/quiz/Resource.java index 6c8550c..301d84d 100755 --- a/src/main/java/quiz/Resource.java +++ b/src/main/java/quiz/Resource.java @@ -2,11 +2,13 @@ import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; public class Resource { public static URI getUri(String resourceName) throws URISyntaxException { - return Resource.class.getResource("/" + resourceName).toURI(); + URL resource = Resource.class.getResource("/" + resourceName); + return resource.toURI(); } public static String getPath(String resourceName) throws URISyntaxException { diff --git a/src/main/java/quiz/TimeLeftCounter.java b/src/main/java/quiz/TimeLeftCounter.java new file mode 100644 index 0000000..26fbda1 --- /dev/null +++ b/src/main/java/quiz/TimeLeftCounter.java @@ -0,0 +1,69 @@ +package quiz; + +import javafx.application.Platform; +import javafx.scene.control.Alert; +import javafx.scene.layout.Pane; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; +import javafx.util.Duration; + +import java.net.URISyntaxException; +import java.net.URL; +import java.util.TimerTask; + +public class TimeLeftCounter extends TimerTask { + + private int timeSpent; + private int timeout; + private Pane pane; + private MediaPlayer tickSound; + private MediaPlayer bombSound; + + private MediaPlayer getMediaPlayer(String sound) { + URL resource = getClass().getResource("/sounds/" + sound); + try { + if (resource != null) { + String tickSoundString = resource.toURI().toString(); + Media tickSound = new Media(tickSoundString); + return new MediaPlayer(tickSound); + } + + } catch (URISyntaxException e) { + e.printStackTrace(); + } + return null; + } + + TimeLeftCounter(int timeout, Pane pane) { + this.timeSpent = 0; + this.timeout = timeout; + this.pane = pane; + this.tickSound = getMediaPlayer("tick.mp3"); + this.bombSound = getMediaPlayer("bomb.mp3"); + } + + public void run() { + if (timeSpent >= timeout) { + Platform.runLater(() -> pane.setStyle("-fx-background-color: rgb(178, 34, 34, 0.9)")); + this.cancel(); + if (bombSound != null) { + bombSound.play(); + } + return; + } else { + timeSpent++; + } + + if (tickSound != null) { + tickSound.seek(Duration.ZERO); + tickSound.play(); + } + + int timeSpentPercentage = 100 - (int)((float)timeSpent / timeout * 100); + String backgroundColor = "-fx-background-color: " + + "linear-gradient(from 50% " + timeSpentPercentage + "% to 50% 100%, " + + "rgb(6, 22, 35, 0.0), " + + "rgb(178, 34, 34, 0.9))"; + Platform.runLater(() -> pane.setStyle(backgroundColor)); + } +} diff --git a/src/main/resources/quiz.css b/src/main/resources/quiz.css index 5033b29..64e1ebf 100755 --- a/src/main/resources/quiz.css +++ b/src/main/resources/quiz.css @@ -30,10 +30,13 @@ -fx-text-fill: white; } +.rootPane { + -fx-background-image: url("images/space.jpg"); +} + .mainPane { -fx-font-size: 36; -fx-padding: 10px; - -fx-background-image: url("images/space.jpg"); } .oneRemains { diff --git a/src/main/resources/quiz.fxml b/src/main/resources/quiz.fxml index 021274f..6c09eb0 100755 --- a/src/main/resources/quiz.fxml +++ b/src/main/resources/quiz.fxml @@ -13,96 +13,105 @@ - - - - - - - - -