From 548e8beb4680fc6b4733b9a6db120777975b4555 Mon Sep 17 00:00:00 2001 From: Jean Luo <luoj3@mcmaster.ca> Date: Wed, 16 Nov 2016 17:45:59 -0500 Subject: [PATCH] time variable added --- Game_Code/src/startGame/GameController.java | 49 +++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Game_Code/src/startGame/GameController.java b/Game_Code/src/startGame/GameController.java index bc9c112..d36c0a9 100644 --- a/Game_Code/src/startGame/GameController.java +++ b/Game_Code/src/startGame/GameController.java @@ -78,8 +78,9 @@ public class GameController{ private Player ai; private Timer t; - - private Timer record; + private long startTime; + private long endTime; + private double timeElapsed; public GameController(GameView v, GameModel m){ this.v = v; @@ -164,6 +165,13 @@ public class GameController{ gameDisplay.setFocusable(true); gameDisplay.setFocusTraversalKeysEnabled(false); + /** + * Initialize the start time and end time for a player + */ + startTime = 0; + endTime = 0; + timeElapsed = 0; + } /** @@ -265,7 +273,11 @@ public class GameController{ mode.setVisible(false); // Start the game with single mode gameFrame.setVisible(true); t.start(); + startTime = System.currentTimeMillis(); } + //else if(source==mode.getAdvance()){ + //TODO: the obstacle mode + //} } } @@ -554,19 +566,48 @@ public class GameController{ * @brief checks whether the game ends * @details check the number of life for both the player and the ai is 0. */ - public void checkGameOver(){ + private void checkGameOver(){ /** * - If the number of life for the ai is 0, the player wins * - If the number of life for the player is 0, the ai wins. + * - Calculate the time a player has played, if breaks the record, save the record. */ if(scoreBottom==0){ + getElapsedTime(); v.gameOver(0); - } else if(scoreTop==0){ + resetGame(); + + + } else if(scoreTop==0){ + getElapsedTime(); v.gameOver(1); + resetGame(); + + } //TODO: SAVE RECORD } + + private void getElapsedTime(){ + endTime = System.currentTimeMillis(); + timeElapsed = (endTime-startTime)/1000.0; + + System.out.println(timeElapsed); + } + + private void resetGame(){ + scoreTop = 0; + scoreBottom = 0; + + + + + } + + + + } -- GitLab