This site requires JavaScript, please enable it in your browser!
Greenfoot back
rizerxgaming
rizerxgaming wrote ...

2017/3/29

timer

rizerxgaming rizerxgaming

2017/3/29

#
hi i am making a game and in that game you have an airplane and you try to avoid the circles on every level so you can go to the next level and finally win but i want to put a timer so the user can see how long the game took him and then try to beat that any ideas on how to make that
danpost danpost

2017/3/29

#
My Value Display Tutorial scenario shows how to create and display a timer.
Nosson1459 Nosson1459

2017/3/29

#
You can have a variable in you World which gets the time the game started.
private long startedTime;

// when the actual game starts
startedTime = System.currentTimeMillis();
Then when the game ends you can use that to show the duration of the game.
int gameDuration = (int) (System.currentTimeMillis() - startedTime);
// that will give you the amount of time the game took in milliseconds
// if you want it in 00:00:00.00 format then you can do
int hours = gameDuration / 3600000;
gameDuration -= hours * 3600000;
int minutes = gameDuration / 60000;
gameDuration -= minutes * 60000;
int seconds = gameDuration / 1000;
gameDuration -= seconds * 1000;
int hundredths = gameDuration / 10;
// then for a GreenfootImage you can do
import java.text.DecimalFormat;

// in the end of the game
GreenfootImage gameTime = new GreenfootImage("Time:\n" + new DecimalFormat("00").format(hours) + ":" + new DecimalFormat("00").format(minutes) + ":" + new DecimalFormat("00").format(seconds) + "." + new DecimalFormat("00").format(hundredths), /*a text size*/ 24, Color.BLACK, new Color(0, 0, 0, 0));
danpost danpost

2017/3/29

#
@Nosson1459, unless actual time (in seconds) is critical, using the system clock for gameplay is not recommended. Better is to count act cycles as play is more consistent (especially when system lag due to background operations being perform comes into play, which would rob precious time from the player).
Nosson1459 Nosson1459

2017/3/30

#
danpost wrote...
@Nosson1459, unless actual time (in seconds) is critical, using the system clock for gameplay is not recommended. Better is to count act cycles as play is more consistent (especially when system lag due to background operations being perform comes into play, which would rob precious time from the player).
If there is lag then there is lag by everybody (just maybe different amounts) and by act cycles it will say a certain amount of time when really you've spent more time on the game.
danpost danpost

2017/3/30

#
Nosson1459 wrote...
If there is lag then there is lag by everybody (just maybe different amounts) and by act cycles it will say a certain amount of time when really you've spent more time on the game.
One cannot tell when their system will run background programs or when they will hog their CPU and prevent act cycles from executing while running any scenario. That "more" time is time that is not usable during gameplay. So when counting act cycles, you are counting only usable gameplay time.
Nosson1459 Nosson1459

2017/3/30

#
OK, I might finally agree.
You need to login to post a reply.