Implemented the timer function

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-05 12:59:25 -04:00
parent 2bc83e4802
commit 253bb9a302
2 changed files with 39 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ public class AsteroidGame extends Thread {
private final ArrayList<Object> mWorld; private final ArrayList<Object> mWorld;
private final GameFrame mGameFrame; private final GameFrame mGameFrame;
public boolean isStarted = false; public boolean isStarted = false;
private long mLastTime;
/** /**
* Creates an new Asteroids game * Creates an new Asteroids game
@@ -221,7 +222,13 @@ public class AsteroidGame extends Thread {
Object[] world; Object[] world;
while (true){ while (true){
if(isStarted) { if(isStarted) {
/*
* increment time
*/
mGameFrame.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime);
mLastTime = System.currentTimeMillis();
/* /*
* update the display and stats * update the display and stats
*/ */
@@ -317,6 +324,7 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public synchronized void startGame(){ public synchronized void startGame(){
mLastTime = System.currentTimeMillis();
mGameFrame.setDisplayText(null); mGameFrame.setDisplayText(null);
setMovingSpaceObjectsEnabled(true); setMovingSpaceObjectsEnabled(true);
isStarted = true; isStarted = true;

View File

@@ -204,13 +204,25 @@ public class Status extends JPanel {
this.mTime += mTime; 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 * @param mAsteroidCount the mAsteroidCount to set
*/ */
public synchronized void setAsteroidCount(int mAsteroidCount) { public synchronized void setAsteroidCount(int mAsteroidCount) {
this.mAsteroidCount = mAsteroidCount; this.mAsteroidCount = mAsteroidCount;
} }
/** /**
* @param level the level to set * @param level the level to set
*/ */
@@ -231,7 +243,7 @@ public class Status extends JPanel {
public synchronized void setShipCount(int mShipCount) { public synchronized void setShipCount(int mShipCount) {
this.mShipCount = mShipCount; this.mShipCount = mShipCount;
} }
/** /**
* @param mShotCount the mShotCount to set * @param mShotCount the mShotCount to set
*/ */
@@ -245,6 +257,21 @@ public class Status extends JPanel {
public synchronized void setTime(long mTime) { public synchronized void setTime(long mTime) {
this.mTime = 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 * Sets the status
@@ -263,7 +290,7 @@ public class Status extends JPanel {
mBuffer.append(" Score: "); mBuffer.append(" Score: ");
mBuffer.append(getScore()); mBuffer.append(getScore());
mBuffer.append(" Time: "); mBuffer.append(" Time: ");
mBuffer.append(getTime()); mBuffer.append(stringTime(getTime()));
status.setText(mBuffer.toString()); status.setText(mBuffer.toString());
mBuffer = new StringBuffer(); mBuffer = new StringBuffer();
} }