From 253bb9a302f2e5916b3e2654124cdb497ff94847 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Thu, 5 Apr 2012 12:59:25 -0400 Subject: [PATCH] Implemented the timer function Signed-off-by: Ricky Barrette --- .../RickBarrette/asteroids/AsteroidGame.java | 10 +++++- .../com/RickBarrette/asteroids/Status.java | 33 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java index bca4a52..232572c 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -32,6 +32,7 @@ public class AsteroidGame extends Thread { private final ArrayList mWorld; private final GameFrame mGameFrame; public boolean isStarted = false; + private long mLastTime; /** * Creates an new Asteroids game @@ -221,7 +222,13 @@ public class AsteroidGame extends Thread { Object[] world; while (true){ if(isStarted) { - + + /* + * increment time + */ + mGameFrame.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime); + mLastTime = System.currentTimeMillis(); + /* * update the display and stats */ @@ -317,6 +324,7 @@ public class AsteroidGame extends Thread { * @author ricky barrette */ public synchronized void startGame(){ + mLastTime = System.currentTimeMillis(); mGameFrame.setDisplayText(null); setMovingSpaceObjectsEnabled(true); isStarted = true; diff --git a/Asteroids/src/com/RickBarrette/asteroids/Status.java b/Asteroids/src/com/RickBarrette/asteroids/Status.java index 0d11b62..b00bd1a 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Status.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Status.java @@ -204,13 +204,25 @@ public class Status extends JPanel { this.mTime += mTime; } + /** + * convince method for formating the seconds string + * @param seconds + * @return formated string + * @author ricky barrette + */ + private String padTime(int time){ + if (time <= 9) + return "0"+ time; + return ""+ time; + } + /** * @param mAsteroidCount the mAsteroidCount to set */ public synchronized void setAsteroidCount(int mAsteroidCount) { this.mAsteroidCount = mAsteroidCount; } - + /** * @param level the level to set */ @@ -231,7 +243,7 @@ public class Status extends JPanel { public synchronized void setShipCount(int mShipCount) { this.mShipCount = mShipCount; } - + /** * @param mShotCount the mShotCount to set */ @@ -245,6 +257,21 @@ public class Status extends JPanel { public synchronized void setTime(long mTime) { this.mTime = mTime; } + + /** + * convince method for formating milliseconds into hour : minutes format + * @param mills + * @return human readable hour : minutes format + * @author ricky barrette + */ + private String stringTime(long mills){ + int hours = (int) (mills / 3600000); + mills = mills - (hours * 3600000); + int minutes = (int) ( mills / 60000); + int seconds = (int) (mills % 60000); + seconds = seconds / 1000; + return hours +" : "+ padTime(minutes) +" : "+ padTime(seconds); + } /** * Sets the status @@ -263,7 +290,7 @@ public class Status extends JPanel { mBuffer.append(" Score: "); mBuffer.append(getScore()); mBuffer.append(" Time: "); - mBuffer.append(getTime()); + mBuffer.append(stringTime(getTime())); status.setText(mBuffer.toString()); mBuffer = new StringBuffer(); }