Jump to content

Clock

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
for a project for school we have to make a game

but there is a timelimit while playing the game, the player can only play for 3 minutes

I have allreayd googled for quite a while, but I still can't find how to

so basicly my problem is:

1 how can I run a clock/timer?
2 how can I make it run in the background of a program so it will only influence the gameplay when the 3 minutes barriere is reached

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
To start with, you want to get the system time when the game starts. Then, for every action, you can check the time and compare it to the time when the game started.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Cander

Cander

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
To check system time I guess this method in the Calendar class works good:

int time = Calendar.getInstance( ).getTimeInMillis( );


#4
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Then, to get it to run in the background you need to instantiate a new thread.

Something like:
public class Timer implements runnable{
  public void run(){
    Calendar finish = Calendar.getInstance();
    finish.add(Calendar.MINUTE, 3);
    while(true){		
      if(finish.before(Calendar.getInstance())){
	System.out.println("You've been playing for three minutes now");
	break;
      }
    }
  }
}


Thread t = new Thread(new Timer()); t.start();

HTH