Skip to content

Commit

Permalink
Merge pull request #17 from jperedadnr/master
Browse files Browse the repository at this point in the history
* Added Best Score, Clock and Pause
* Added resizable capabilities
  • Loading branch information
brunoborges committed Jun 25, 2014
2 parents 67cb51e + 77adf4c commit 6c90746
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 58 deletions.
28 changes: 23 additions & 5 deletions src/game2048/Game2048.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Bounds;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
Expand All @@ -19,7 +21,8 @@ public class Game2048 extends Application {

private GameManager gameManager;
private Bounds gameBounds;

private final static int MARGIN = 36;

@Override
public void init(){
// Downloaded from https://01.org/clear-sans/blogs
Expand All @@ -33,15 +36,16 @@ public void start(Stage primaryStage) {
gameBounds = gameManager.getLayoutBounds();

StackPane root = new StackPane(gameManager);
root.setPrefSize(gameBounds.getWidth(), gameBounds.getHeight());
ChangeListener<Number> resize = (ov, v, v1) -> {
double scale=Math.min((root.getWidth()-MARGIN)/gameBounds.getWidth(),(root.getHeight()-MARGIN)/gameBounds.getHeight());
gameManager.setScale(scale);
gameManager.setLayoutX((root.getWidth() - gameBounds.getWidth()) / 2d);
gameManager.setLayoutY((root.getHeight() - gameBounds.getHeight()) / 2d);
};
root.widthProperty().addListener(resize);
root.heightProperty().addListener(resize);

Scene scene = new Scene(root, 600, 720);
Scene scene = new Scene(root);
scene.getStylesheets().add("game2048/game.css");
addKeyHandler(scene);
addSwipeHandlers(scene);
Expand All @@ -55,10 +59,15 @@ public void start(Stage primaryStage) {
scene.setCursor(Cursor.NONE);
}

Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
double factor=Math.min(visualBounds.getWidth()/(gameBounds.getWidth()+MARGIN),
visualBounds.getHeight()/(gameBounds.getHeight()+MARGIN));
primaryStage.setTitle("2048FX");
primaryStage.setScene(scene);
primaryStage.setMinWidth(gameBounds.getWidth());
primaryStage.setMinHeight(gameBounds.getHeight());
primaryStage.setMinWidth(gameBounds.getWidth()/2d);
primaryStage.setMinHeight(gameBounds.getHeight()/2d);
primaryStage.setWidth((gameBounds.getWidth()+MARGIN)*factor);
primaryStage.setHeight((gameBounds.getHeight()+MARGIN)*factor);
primaryStage.show();
}

Expand All @@ -77,6 +86,10 @@ private void addKeyHandler(Scene scene) {
gameManager.restoreSession();
return;
}
if (keyCode.equals(KeyCode.P)) {
gameManager.pauseGame();
return;
}
if (keyCode.isArrowKey() == false) {
return;
}
Expand All @@ -92,6 +105,11 @@ private void addSwipeHandlers(Scene scene) {
scene.setOnSwipeDown(e -> gameManager.move(Direction.DOWN));
}

@Override
public void stop(){
gameManager.saveRecord();
}

/**
* @param args the command line arguments
*/
Expand Down

0 comments on commit 6c90746

Please sign in to comment.