Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Basic Two-Player Timer
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
registration




PostPosted: Fri Jan 15, 2010 1:03 am   Post subject: Basic Two-Player Timer

Just as an example, let's just say I'm making a board game that requires two players to play.

The game will start off with some questions (I don't know.. let's just say it asks "how's you day", and after the person answers, the game will start)

Each player has 10 minutes to finish the game; if player one goes over 10 minutes, player two will win and vice versa.

Now if you note what I said on the second line, I said I start off with a question. So techincally, doing Time.Elapsed won't work because player one's turn doesn't start from the beginning of the program.. it starts after the question is answered. Doing this won't work either:
var timeBetween := Time.Elapsed
plyrOneTimer := Time.Elapsed - timeBetween
because it just won't function properly when you alternate turns..

Does anyone have any ideas for me as to how I can keep track of two different timers for each player? (please note player one's timer will pause after he makes a move (e.g., move a dice or car or something), then player two's timer will start counting down.. etc.)
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Jan 15, 2010 1:23 am   Post subject: RE:Basic Two-Player Timer

So you want to start counting when a player's turn starts, and stop counting when a player's turn ends, then keep a running sum of time used?

To start counting, use this:
code:
playerTurnStart = Time.Elapsed


To stop counting, determine how much time has passed:
code:

var turnTime : int := Time.Elapsed - playerTurnStart
% Add 'turnTime' to the running tally of time used, however you want to do that. You should keep seperate, per-player totals.
registration




PostPosted: Fri Jan 15, 2010 1:45 am   Post subject: Re: RE:Basic Two-Player Timer

DemonWasp @ Fri Jan 15, 2010 1:23 am wrote:
So you want to start counting when a player's turn starts, and stop counting when a player's turn ends, then keep a running sum of time used?

To start counting, use this:
code:
playerTurnStart = Time.Elapsed


To stop counting, determine how much time has passed:
code:

var turnTime : int := Time.Elapsed - playerTurnStart
% Add 'turnTime' to the running tally of time used, however you want to do that. You should keep seperate, per-player totals.


Time.Elapsed won't work (and I mentioned above why), would there be another way to do it?

Also, I need to actually DRAW the timer counting down.. so every second it's someone's turn, their timer is updated every second.. but I can't do that when you do turnTime := ~ AFTER everything happens.. even a for loop from the start to the end time won't work because you don't know the end time..
TheGuardian001




PostPosted: Fri Jan 15, 2010 8:41 am   Post subject: Re: Basic Two-Player Timer

Time.Elapsed will actually work here. When you use the Time.Elapsed method, you're comparing the current time to the time you started counting at, not to the start of the program.

Lets say you wake up at noon, and you start doing something at 6:15, and stop at 6:20, Have you been doing that thing for 5 minutes or 6 hours 20 minutes? Since you are comparing the two times to find the amount of time in between them, so long as you set the initial Time.Elapsed value when the player is starting (and not at the start of the program), you will be able to use it as a timer.

code:


%blah.

%useless other stuff
.
%taking up time....

%Oh! time for the player to start their quiz!
startTime = Time.Elapsed
%Since we started the timer when we wanted to use it (and not
%at the beginning of the program, it can still be used as a timer!

loop
    curTime = Time.Elapsed
    compare curTime and startTime
end loop
DemonWasp




PostPosted: Fri Jan 15, 2010 11:40 am   Post subject: RE:Basic Two-Player Timer

To draw the timer counting down, all you need to do at every frame of rendering in your program (you are rendering frame-by-frame using non-blocking input, right?) is find the value of Time.Elapsed - playerStartTime and output that somehow.

If you don't know what I mean by frame-by-frame output, that's the difference between an update-redraw-loop type of program and a wait-for-user-input type of program.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: