Skip to content

Commit

Permalink
Added time limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gergely Novak committed Dec 22, 2018
1 parent 5cba2bd commit dc02b84
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 95 deletions.
43 changes: 37 additions & 6 deletions src/main/java/quiz/QuizController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +83,8 @@ public class QuizController {
private Integer selectedAnswer = null;
private List<String> selectedAnswers = new ArrayList<>();

private Timer timeLeftTimer;

enum GameState {
QUESTION_SHOWN,
ANSWERS_SHOWN,
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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<String> mainStyles = mainPane.getStyleClass();
mainStyles.clear();
mainStyles.addAll("mainPane");
HashMap<String, Pane> panes = new HashMap<>();
panes.put("rootPane", rootPane);
panes.put("mainPane", mainPane);
for (Map.Entry<String, Pane> pane : panes.entrySet()) {
ObservableList<String> 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) {
Expand Down Expand Up @@ -274,6 +304,7 @@ private void selectAnswer(int answer) {
if (!styles.contains("selectedAnswerBox")) {
styles.add("selectedAnswerBox");
}
resetTimer();
}

private void deselectAnswer(int answer) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/quiz/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/quiz/TimeLeftCounter.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
5 changes: 4 additions & 1 deletion src/main/resources/quiz.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
183 changes: 96 additions & 87 deletions src/main/resources/quiz.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,96 +13,105 @@
<?import java.util.*?>
<?import javafx.scene.*?>

<GridPane fx:id="mainPane" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="592.0"
stylesheets="quiz.css" styleClass="mainPane" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="quiz.QuizController" onKeyPressed="#keyPressed"
focusTraversable="true">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" maxHeight="200.0" minHeight="200.0" percentHeight="20.0" prefHeight="200.0"/>
<RowConstraints minHeight="10.0" percentHeight="80.0" prefHeight="310.0" vgrow="SOMETIMES"/>
</rowConstraints>
<Label fx:id="questionLabel" text="Question" textAlignment="CENTER" wrapText="true" GridPane.halignment="CENTER"
GridPane.valignment="CENTER" onMouseClicked="#questionClicked"/>
<GridPane GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<AnchorPane xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="quiz.QuizController" onKeyPressed="#keyPressed"
fx:id="rootPane"
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
styleClass="rootPane"
minWidth="-Infinity" prefHeight="400.0" prefWidth="592.0"
stylesheets="quiz.css">
<GridPane fx:id="mainPane" alignment="CENTER" styleClass="mainPane"
focusTraversable="true" AnchorPane.topAnchor="0.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints fillHeight="false" maxHeight="200.0" minHeight="200.0" percentHeight="20.0" prefHeight="200.0"/>
<RowConstraints minHeight="10.0" percentHeight="80.0" prefHeight="310.0" vgrow="SOMETIMES"/>
</rowConstraints>
<VBox fx:id="answer0Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="0" GridPane.columnIndex="0">
<Label fx:id="answer0Label" text="Answer 0" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer1Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="1" GridPane.columnIndex="1">
<Label fx:id="answer1Label" text="Answer 1" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer2Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="2" GridPane.columnIndex="2">
<Label fx:id="answer2Label" text="Answer 2" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer3Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="3" GridPane.columnIndex="3">
<Label fx:id="answer3Label" text="Answer 3" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer4Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="4" GridPane.rowIndex="1">
<Label fx:id="answer4Label" text="Answer 4" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer5Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="5" GridPane.columnIndex="1" GridPane.rowIndex="1">
<Label fx:id="answer5Label" text="Answer 5" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer6Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="6" GridPane.columnIndex="2" GridPane.rowIndex="1">
<Label fx:id="answer6Label" text="Answer 6" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer7Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="7" GridPane.columnIndex="3" GridPane.rowIndex="1">
<Label fx:id="answer7Label" text="Answer 7" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer8Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="8" GridPane.rowIndex="2">
<Label fx:id="answer8Label" text="Answer 8" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer9Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="9" GridPane.columnIndex="1" GridPane.rowIndex="2">
<Label fx:id="answer9Label" text="Answer 9" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer10Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="10" GridPane.columnIndex="2" GridPane.rowIndex="2">
<Label fx:id="answer10Label" text="Answer 10" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer11Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="11" GridPane.columnIndex="3" GridPane.rowIndex="2">
<Label fx:id="answer11Label" text="Answer 11" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer12Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="12" GridPane.rowIndex="3">
<Label fx:id="answer12Label" text="Answer 12" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer13Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="13" GridPane.columnIndex="1" GridPane.rowIndex="3">
<Label fx:id="answer13Label" text="Answer 13" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer14Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="14" GridPane.columnIndex="2" GridPane.rowIndex="3">
<Label fx:id="answer14Label" text="Answer 14" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer15Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="15" GridPane.columnIndex="3" GridPane.rowIndex="3">
<Label fx:id="answer15Label" text="Answer 15" wrapText="true" textAlignment="CENTER"/>
</VBox>
<Label fx:id="questionLabel" text="Question" textAlignment="CENTER" wrapText="true" GridPane.halignment="CENTER"
GridPane.valignment="CENTER" onMouseClicked="#questionClicked"/>
<GridPane GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<VBox fx:id="answer0Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="0" GridPane.columnIndex="0">
<Label fx:id="answer0Label" text="Answer 0" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer1Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="1" GridPane.columnIndex="1">
<Label fx:id="answer1Label" text="Answer 1" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer2Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="2" GridPane.columnIndex="2">
<Label fx:id="answer2Label" text="Answer 2" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer3Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="3" GridPane.columnIndex="3">
<Label fx:id="answer3Label" text="Answer 3" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer4Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="4" GridPane.rowIndex="1">
<Label fx:id="answer4Label" text="Answer 4" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer5Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="5" GridPane.columnIndex="1" GridPane.rowIndex="1">
<Label fx:id="answer5Label" text="Answer 5" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer6Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="6" GridPane.columnIndex="2" GridPane.rowIndex="1">
<Label fx:id="answer6Label" text="Answer 6" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer7Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="7" GridPane.columnIndex="3" GridPane.rowIndex="1">
<Label fx:id="answer7Label" text="Answer 7" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer8Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="8" GridPane.rowIndex="2">
<Label fx:id="answer8Label" text="Answer 8" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer9Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="9" GridPane.columnIndex="1" GridPane.rowIndex="2">
<Label fx:id="answer9Label" text="Answer 9" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer10Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="10" GridPane.columnIndex="2" GridPane.rowIndex="2">
<Label fx:id="answer10Label" text="Answer 10" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer11Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="11" GridPane.columnIndex="3" GridPane.rowIndex="2">
<Label fx:id="answer11Label" text="Answer 11" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer12Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="12" GridPane.rowIndex="3">
<Label fx:id="answer12Label" text="Answer 12" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer13Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="13" GridPane.columnIndex="1" GridPane.rowIndex="3">
<Label fx:id="answer13Label" text="Answer 13" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer14Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="14" GridPane.columnIndex="2" GridPane.rowIndex="3">
<Label fx:id="answer14Label" text="Answer 14" wrapText="true" textAlignment="CENTER"/>
</VBox>
<VBox fx:id="answer15Box" alignment="CENTER" onMouseClicked="#answerClicked" styleClass="defaultAnswerBox"
userData="15" GridPane.columnIndex="3" GridPane.rowIndex="3">
<Label fx:id="answer15Label" text="Answer 15" wrapText="true" textAlignment="CENTER"/>
</VBox>
</GridPane>
</GridPane>
</GridPane>
</AnchorPane>

Binary file added src/main/resources/sounds/bomb.mp3
Binary file not shown.
Binary file added src/main/resources/sounds/tick.mp3
Binary file not shown.

0 comments on commit dc02b84

Please sign in to comment.