Implemented the timer function
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -32,6 +32,7 @@ public class AsteroidGame extends Thread {
|
||||
private final ArrayList<Object> mWorld;
|
||||
private final GameFrame mGameFrame;
|
||||
public boolean isStarted = false;
|
||||
private long mLastTime;
|
||||
|
||||
/**
|
||||
* Creates an new Asteroids game
|
||||
@@ -222,6 +223,12 @@ public class AsteroidGame extends Thread {
|
||||
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;
|
||||
|
||||
@@ -204,6 +204,18 @@ 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
|
||||
*/
|
||||
@@ -246,6 +258,21 @@ public class Status extends JPanel {
|
||||
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
|
||||
* @param temp
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user