I am making a maze game in Java. I have made a maze board, A start point and a end point. When i reach the end point then it exit and show a winning message. But i can not add a time limitation. Suppose player have to reach the end point with 30 seconds otherwise he lose the game.
-
Why can't you add a time limitation? – Makoto Apr 04 '13 at 18:24
-
And your question is? "Give me code that counts down"? What have you tried? – WereWolfBoy Apr 04 '13 at 18:24
-
Yes i need the code. please no more question, if you know then give the answer. – coder Apr 07 '13 at 18:06
3 Answers
System.currentTimeMillis();
// returns current time in milliseconds...
//save time at beggining of game, compare saved time to current time... when x time has passed... do something...
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#currentTimeMillis()

- 1,621
- 2
- 24
- 40
You have to basically create a separate thread which will take care of the time counter. To see how to create a thread you can refer
http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
After this you need to run the game on a separate thread and counter on another thread..To make a time counter you can take help from this previous post.
How can I count the time it takes a function to complete in Java?
Get current system time when you start a game:
long startTime = System.currentTimeMillis();
Than every time your tick/update method occours check how much time elapsed:
long elapsedTime = System.currentTimeMillis() - startTime;
elapsedTime holds elapsed time in miliseconds to get elapsed time in seconds just divide it with 1000:
elapsedTime\= 1000;
Now you can check how much time passed since the game was started,
For example:
if(elapsedTime >= 30) running = false;
Have in mind that this is simplest implementation just to give you some idea of how it can be done in Java

- 191
- 2
- 9
-
1thanks it works properly. here is the total code i have done so far..http://emontec.blogspot.com/2013/04/maze-game-source-code-in-java.html – coder Apr 08 '13 at 20:24