0

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.

coder
  • 51
  • 1
  • 5

3 Answers3

1

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()

Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40
1

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?

Community
  • 1
  • 1
AurA
  • 12,135
  • 7
  • 46
  • 63
1

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

  • 1
    thanks 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