timer, interactive smiley, reinit
This commit is contained in:
parent
ba7dea1436
commit
d11c6e5b29
@ -3,6 +3,8 @@ package com.shr4pnel.minesweeper;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.image.Image;
|
||||
@ -17,12 +19,25 @@ public class Controller {
|
||||
@FXML
|
||||
ImageView smiley;
|
||||
|
||||
@FXML
|
||||
ImageView time_1;
|
||||
|
||||
@FXML
|
||||
ImageView time_2;
|
||||
|
||||
@FXML
|
||||
ImageView time_3;
|
||||
|
||||
|
||||
// i already said i'm sorry!
|
||||
Set<String> visitedTiles = new HashSet<>();
|
||||
Grid gridHandler;
|
||||
GridWrapper wrapper;
|
||||
boolean gameOver = false;
|
||||
boolean isFirstLoad = true;
|
||||
static Timer timer = new Timer();
|
||||
int time = 0;
|
||||
long startTime;
|
||||
|
||||
@FXML
|
||||
private void initialize() {
|
||||
@ -30,10 +45,64 @@ public class Controller {
|
||||
wrapper = gridHandler.grid;
|
||||
}
|
||||
|
||||
|
||||
private void reinitialize() {
|
||||
timer = new Timer();
|
||||
URL blank = getClass().getResource("img/blank.png");
|
||||
for (Node n : grid.getChildren()) {
|
||||
ImageView nodeAsImage = (ImageView) n;
|
||||
nodeAsImage.setImage(new Image(blank.toString()));
|
||||
}
|
||||
gridHandler = new Grid();
|
||||
wrapper = gridHandler.grid;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void gridClicked(MouseEvent event) {
|
||||
if (gameOver)
|
||||
if (gameOver) {
|
||||
return;
|
||||
}
|
||||
if (isFirstLoad) {
|
||||
startTime = System.currentTimeMillis();
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (time >= 999) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
long elapsedTimeMillis = currentTimeMillis - startTime;
|
||||
time = (int) (elapsedTimeMillis / 1000);
|
||||
String timeString = String.valueOf(time);
|
||||
char[] timeStringArray = timeString.toCharArray();
|
||||
int length = timeString.length();
|
||||
if (length == 3) {
|
||||
char hundred = timeStringArray[0];
|
||||
char tens = timeStringArray[1];
|
||||
char unit = timeStringArray[2];
|
||||
URL hundredURL = getClass().getResource("img/" + hundred + "_seconds.png");
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_1.setImage(new Image(String.valueOf(hundredURL)));
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 2) {
|
||||
char tens = timeStringArray[0];
|
||||
char unit = timeStringArray[1];
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 1) {
|
||||
char unit = timeStringArray[0];
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
}
|
||||
}
|
||||
};
|
||||
timer.schedule(task, 0, 1000);
|
||||
}
|
||||
Node tileClicked = event.getPickResult().getIntersectedNode();
|
||||
int column = GridPane.getColumnIndex(tileClicked);
|
||||
int row = GridPane.getRowIndex(tileClicked);
|
||||
@ -41,15 +110,13 @@ public class Controller {
|
||||
gameOver = true;
|
||||
System.out.println("DEAD!!!");
|
||||
URL smileyURL = getClass().getResource("img/face_dead.png");
|
||||
smiley.setImage(new Image(smileyURL.toString()));
|
||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
||||
ImageView tileClickedImage = (ImageView) tileClicked;
|
||||
URL deathBombURL = getClass().getResource("img/bomb_death.png");
|
||||
tileClickedImage.setImage(new Image(deathBombURL.toString()));
|
||||
tileClickedImage.setImage(new Image(String.valueOf(deathBombURL)));
|
||||
showAllBombs();
|
||||
return;
|
||||
// todo do stuff!!
|
||||
}
|
||||
|
||||
setAdjacentCount(tileClicked);
|
||||
expandGrid(column, row);
|
||||
}
|
||||
@ -62,19 +129,33 @@ public class Controller {
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||
ImageView currentBombTile = (ImageView) children[column + row * 30];
|
||||
URL bombRevealedURL = getClass().getResource("img/bomb_revealed.png");
|
||||
currentBombTile.setImage(new Image(bombRevealedURL.toString()));
|
||||
currentBombTile.setImage(new Image(String.valueOf(bombRevealedURL)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void smileyPressed() {
|
||||
URL smileyURL = getClass().getResource("img/face_smile_pressed.png");
|
||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
||||
}
|
||||
|
||||
@FXML
|
||||
void smileyReleased() {
|
||||
gameOver = false;
|
||||
URL smileyURL = getClass().getResource("img/face_smile.png");
|
||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
||||
reinitialize();
|
||||
}
|
||||
|
||||
void setAdjacentCount(Node tileClicked) {
|
||||
int column = GridPane.getColumnIndex(tileClicked);
|
||||
int row = GridPane.getRowIndex(tileClicked);
|
||||
ImageView image = (ImageView) tileClicked;
|
||||
int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
||||
URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png");
|
||||
image.setImage(new Image(imageURL.toString()));
|
||||
image.setImage(new Image(String.valueOf(imageURL)));
|
||||
}
|
||||
|
||||
void expandGrid(int baseColumn, int baseRow) {
|
||||
|
@ -2,6 +2,7 @@ package com.shr4pnel.minesweeper;
|
||||
|
||||
import java.io.IOException;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
@ -20,4 +21,10 @@ public class Main extends Application {
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
|
||||
@Override
|
||||
@FXML
|
||||
public void stop() {
|
||||
Controller.timer.cancel();
|
||||
}
|
||||
}
|
@ -80,22 +80,22 @@
|
||||
</ImageView>
|
||||
<Rectangle fill="GREY" height="23.0" layoutX="446.0" layoutY="15.0" smooth="false" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="39.0" />
|
||||
<Rectangle fill="WHITE" height="23.0" layoutX="448.0" layoutY="17.0" smooth="false" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="39.0" />
|
||||
<ImageView fitHeight="23.0" fitWidth="13.0" layoutX="447.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<ImageView fx:id="time_1" fitHeight="23.0" fitWidth="13.0" layoutX="447.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<image>
|
||||
<Image url="@img/0_seconds.png" />
|
||||
</image>
|
||||
</ImageView>
|
||||
<ImageView fitHeight="23.0" fitWidth="13.0" layoutX="460.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<ImageView fx:id="time_2" fitHeight="23.0" fitWidth="13.0" layoutX="460.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<image>
|
||||
<Image url="@img/0_seconds.png" />
|
||||
</image>
|
||||
</ImageView>
|
||||
<ImageView fitHeight="23.0" fitWidth="13.0" layoutX="473.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<ImageView fx:id="time_3" fitHeight="23.0" fitWidth="13.0" layoutX="473.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<image>
|
||||
<Image url="@img/0_seconds.png" />
|
||||
</image>
|
||||
</ImageView>
|
||||
<ImageView fx:id="smiley" fitHeight="26.0" fitWidth="26.0" layoutX="238.0" layoutY="15.0" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<ImageView fx:id="smiley" fitHeight="26.0" fitWidth="26.0" layoutX="238.0" layoutY="15.0" onMousePressed="#smileyPressed" onMouseReleased="#smileyReleased" pickOnBounds="true" preserveRatio="true" smooth="false">
|
||||
<image>
|
||||
<Image url="@img/face_smile.png" />
|
||||
</image>
|
||||
|
Loading…
x
Reference in New Issue
Block a user