How To Add A Timer While A Whole Procedure Takes Place

Hey I am developing a game in which I need a variable that is incremeting or decrementing ever 1 second. I need this variable to keep active while a whole procedure takes place. The procedure includes choice:keypressed. The thing is that I am having trouble inserting this sort of timer since I a procedure is running.I though that something like a while loop would be usefull but I don't know how to do it. Can anyone please give me any suggestions please?Thanks Alot.

Comments

  • : Hey I am developing a game in which I need a variable that is
    : incremeting or decrementing ever 1 second. I need this variable to
    : keep active while a whole procedure takes place. The procedure
    : includes choice:keypressed. The thing is that I am having trouble
    : inserting this sort of timer since I a procedure is running.I though
    : that something like a while loop would be usefull but I don't know
    : how to do it. Can anyone please give me any suggestions
    : please?Thanks Alot.
    :
    Here's a basic example of a game-loop:
    [code]
    repeat
    if KeyPressed then
    begin
    ExtendedKey := false;
    ch := ReadKey;
    if ch = #0 then
    begin
    ExtendedKey := true;
    ch := ReadKey;
    end;
    ProcessKeyPress(ch, ExtendedKey);
    end;
    ProcessOtherStuff;{Examples:
    - Increment counters
    - Move other pieces}
    if ch <> 'q' then
    Sleep(150); { wait 150 ms. before starting next iteration
    This is necessary for the timing of the game
    Change to your liking }
    until ch = 'q'; { Press q to exit the loop }
    [/code]
  • : : Hey I am developing a game in which I need a variable that is
    : : incremeting or decrementing ever 1 second. I need this variable to
    : : keep active while a whole procedure takes place. The procedure
    : : includes choice:keypressed. The thing is that I am having trouble
    : : inserting this sort of timer since I a procedure is running.I though
    : : that something like a while loop would be usefull but I don't know
    : : how to do it. Can anyone please give me any suggestions
    : : please?Thanks Alot.
    : :
    : Here's a basic example of a game-loop:
    : [code]:
    : repeat
    : if KeyPressed then
    : begin
    : ExtendedKey := false;
    : ch := ReadKey;
    : if ch = #0 then
    : begin
    : ExtendedKey := true;
    : ch := ReadKey;
    : end;
    : ProcessKeyPress(ch, ExtendedKey);
    : end;
    : ProcessOtherStuff;{Examples:
    : - Increment counters
    : - Move other pieces}
    : if ch <> 'q' then
    : Sleep(150); { wait 150 ms. before starting next iteration
    : This is necessary for the timing of the game
    : Change to your liking }
    : until ch = 'q'; { Press q to exit the loop }
    : [/code]:


    Hey, thanks for you reply however if for example I have this code;

    for i:=1 to 30 do begin
    writeln ('Enter Name');
    readln (names[i]);
    end;

    And I would like to attach a timer to it to check how fast is the user in typing. Can you please give me an example on this ocde please. I know that yours was helpful however I am just in the 2nd year of programming and I am kind of noob. Thanks Again
  • : : : Hey I am developing a game in which I need a variable that is
    : : : incremeting or decrementing ever 1 second. I need this variable to
    : : : keep active while a whole procedure takes place. The procedure
    : : : includes choice:keypressed. The thing is that I am having trouble
    : : : inserting this sort of timer since I a procedure is running.I though
    : : : that something like a while loop would be usefull but I don't know
    : : : how to do it. Can anyone please give me any suggestions
    : : : please?Thanks Alot.
    : : :
    : : Here's a basic example of a game-loop:
    : : [code]: :
    : : repeat
    : : if KeyPressed then
    : : begin
    : : ExtendedKey := false;
    : : ch := ReadKey;
    : : if ch = #0 then
    : : begin
    : : ExtendedKey := true;
    : : ch := ReadKey;
    : : end;
    : : ProcessKeyPress(ch, ExtendedKey);
    : : end;
    : : ProcessOtherStuff;{Examples:
    : : - Increment counters
    : : - Move other pieces}
    : : if ch <> 'q' then
    : : Sleep(150); { wait 150 ms. before starting next iteration
    : : This is necessary for the timing of the game
    : : Change to your liking }
    : : until ch = 'q'; { Press q to exit the loop }
    : : [/code]: :
    :
    :
    : Hey, thanks for you reply however if for example I have this code;
    :
    : for i:=1 to 30 do begin
    : writeln ('Enter Name');
    : readln (names[i]);
    : end;
    :
    : And I would like to attach a timer to it to check how fast is the
    : user in typing. Can you please give me an example on this ocde
    : please. I know that yours was helpful however I am just in the 2nd
    : year of programming and I am kind of noob. Thanks Again
    :
    Readln() blocks the code until the user presses enter. You cannot easily interrupt it to update a clock, while the user is typing. It is however possible to calculate the length of time the readln() statement took:
    [code]
    var
    Start, Finish: TDateTime;
    begin
    Start := Now();
    readln(SomeString);
    Finish := Now();
    Writeln('Time to enter: '+Finish-Start);
    end;
    [/code]
    The code above might be compiler dependent, check the help files for the correct date/time functions if you get compiler errors.
  • : : : : Hey I am developing a game in which I need a variable that is
    : : : : incremeting or decrementing ever 1 second. I need this variable to
    : : : : keep active while a whole procedure takes place. The procedure
    : : : : includes choice:keypressed. The thing is that I am having trouble
    : : : : inserting this sort of timer since I a procedure is running.I though
    : : : : that something like a while loop would be usefull but I don't know
    : : : : how to do it. Can anyone please give me any suggestions
    : : : : please?Thanks Alot.
    : : : :
    : : : Here's a basic example of a game-loop:
    : : : [code]: : :
    : : : repeat
    : : : if KeyPressed then
    : : : begin
    : : : ExtendedKey := false;
    : : : ch := ReadKey;
    : : : if ch = #0 then
    : : : begin
    : : : ExtendedKey := true;
    : : : ch := ReadKey;
    : : : end;
    : : : ProcessKeyPress(ch, ExtendedKey);
    : : : end;
    : : : ProcessOtherStuff;{Examples:
    : : : - Increment counters
    : : : - Move other pieces}
    : : : if ch <> 'q' then
    : : : Sleep(150); { wait 150 ms. before starting next iteration
    : : : This is necessary for the timing of the game
    : : : Change to your liking }
    : : : until ch = 'q'; { Press q to exit the loop }
    : : : [/code]: : :
    : :
    : :
    : : Hey, thanks for you reply however if for example I have this code;
    : :
    : : for i:=1 to 30 do begin
    : : writeln ('Enter Name');
    : : readln (names[i]);
    : : end;
    : :
    : : And I would like to attach a timer to it to check how fast is the
    : : user in typing. Can you please give me an example on this ocde
    : : please. I know that yours was helpful however I am just in the 2nd
    : : year of programming and I am kind of noob. Thanks Again
    : :
    : Readln() blocks the code until the user presses enter. You cannot
    : easily interrupt it to update a clock, while the user is typing. It
    : is however possible to calculate the length of time the readln()
    : statement took:
    : [code]:
    : var
    : Start, Finish: TDateTime;
    : begin
    : Start := Now();
    : readln(SomeString);
    : Finish := Now();
    : Writeln('Time to enter: '+Finish-Start);
    : end;
    : [/code]:
    : The code above might be compiler dependent, check the help files for
    : the correct date/time functions if you get compiler errors.

    Oh thanks alot, I never knew about this function, thanks alot man yuo were really helpful.Thanks Again.
  • : : : : : Hey I am developing a game in which I need a variable that is
    : : : : : incremeting or decrementing ever 1 second. I need this variable to
    : : : : : keep active while a whole procedure takes place. The procedure
    : : : : : includes choice:keypressed. The thing is that I am having trouble
    : : : : : inserting this sort of timer since I a procedure is running.I though
    : : : : : that something like a while loop would be usefull but I don't know
    : : : : : how to do it. Can anyone please give me any suggestions
    : : : : : please?Thanks Alot.
    : : : : :
    : : : : Here's a basic example of a game-loop:
    : : : : [code]: : : :
    : : : : repeat
    : : : : if KeyPressed then
    : : : : begin
    : : : : ExtendedKey := false;
    : : : : ch := ReadKey;
    : : : : if ch = #0 then
    : : : : begin
    : : : : ExtendedKey := true;
    : : : : ch := ReadKey;
    : : : : end;
    : : : : ProcessKeyPress(ch, ExtendedKey);
    : : : : end;
    : : : : ProcessOtherStuff;{Examples:
    : : : : - Increment counters
    : : : : - Move other pieces}
    : : : : if ch <> 'q' then
    : : : : Sleep(150); { wait 150 ms. before starting next iteration
    : : : : This is necessary for the timing of the game
    : : : : Change to your liking }
    : : : : until ch = 'q'; { Press q to exit the loop }
    : : : : [/code]: : : :
    : : :
    : : :
    : : : Hey, thanks for you reply however if for example I have this code;
    : : :
    : : : for i:=1 to 30 do begin
    : : : writeln ('Enter Name');
    : : : readln (names[i]);
    : : : end;
    : : :
    : : : And I would like to attach a timer to it to check how fast is the
    : : : user in typing. Can you please give me an example on this ocde
    : : : please. I know that yours was helpful however I am just in the 2nd
    : : : year of programming and I am kind of noob. Thanks Again
    : : :
    : : Readln() blocks the code until the user presses enter. You cannot
    : : easily interrupt it to update a clock, while the user is typing. It
    : : is however possible to calculate the length of time the readln()
    : : statement took:
    : : [code]: :
    : : var
    : : Start, Finish: TDateTime;
    : : begin
    : : Start := Now();
    : : readln(SomeString);
    : : Finish := Now();
    : : Writeln('Time to enter: '+Finish-Start);
    : : end;
    : : [/code]: :
    : : The code above might be compiler dependent, check the help files for
    : : the correct date/time functions if you get compiler errors.
    :
    : Oh thanks alot, I never knew about this function, thanks alot man
    : yuo were really helpful.Thanks Again.
    :

    Hey I am really sorry but I just can't get it right with my compiler, FPC accepts the variable tdatetime but not the now identifier, while TP doesn't accept the tdatetime variable type. Can I install your compiler or can you tell me the code of the turbopascal compiler please?Thanks alot and sorry for annoying.
  • : : : : : : Hey I am developing a game in which I need a variable that is
    : : : : : : incremeting or decrementing ever 1 second. I need this variable to
    : : : : : : keep active while a whole procedure takes place. The procedure
    : : : : : : includes choice:keypressed. The thing is that I am having trouble
    : : : : : : inserting this sort of timer since I a procedure is running.I though
    : : : : : : that something like a while loop would be usefull but I don't know
    : : : : : : how to do it. Can anyone please give me any suggestions
    : : : : : : please?Thanks Alot.
    : : : : : :
    : : : : : Here's a basic example of a game-loop:
    : : : : : [code]: : : : :
    : : : : : repeat
    : : : : : if KeyPressed then
    : : : : : begin
    : : : : : ExtendedKey := false;
    : : : : : ch := ReadKey;
    : : : : : if ch = #0 then
    : : : : : begin
    : : : : : ExtendedKey := true;
    : : : : : ch := ReadKey;
    : : : : : end;
    : : : : : ProcessKeyPress(ch, ExtendedKey);
    : : : : : end;
    : : : : : ProcessOtherStuff;{Examples:
    : : : : : - Increment counters
    : : : : : - Move other pieces}
    : : : : : if ch <> 'q' then
    : : : : : Sleep(150); { wait 150 ms. before starting next iteration
    : : : : : This is necessary for the timing of the game
    : : : : : Change to your liking }
    : : : : : until ch = 'q'; { Press q to exit the loop }
    : : : : : [/code]: : : : :
    : : : :
    : : : :
    : : : : Hey, thanks for you reply however if for example I have this code;
    : : : :
    : : : : for i:=1 to 30 do begin
    : : : : writeln ('Enter Name');
    : : : : readln (names[i]);
    : : : : end;
    : : : :
    : : : : And I would like to attach a timer to it to check how fast is the
    : : : : user in typing. Can you please give me an example on this ocde
    : : : : please. I know that yours was helpful however I am just in the 2nd
    : : : : year of programming and I am kind of noob. Thanks Again
    : : : :
    : : : Readln() blocks the code until the user presses enter. You cannot
    : : : easily interrupt it to update a clock, while the user is typing. It
    : : : is however possible to calculate the length of time the readln()
    : : : statement took:
    : : : [code]: : :
    : : : var
    : : : Start, Finish: TDateTime;
    : : : begin
    : : : Start := Now();
    : : : readln(SomeString);
    : : : Finish := Now();
    : : : Writeln('Time to enter: '+Finish-Start);
    : : : end;
    : : : [/code]: : :
    : : : The code above might be compiler dependent, check the help files for
    : : : the correct date/time functions if you get compiler errors.
    : :
    : : Oh thanks alot, I never knew about this function, thanks alot man
    : : yuo were really helpful.Thanks Again.
    : :
    :
    : Hey I am really sorry but I just can't get it right with my
    : compiler, FPC accepts the variable tdatetime but not the now
    : identifier, while TP doesn't accept the tdatetime variable type. Can
    : I install your compiler or can you tell me the code of the
    : turbopascal compiler please?Thanks alot and sorry for annoying.
    :
    In TP, use the GetTime() procedure (see help files for details).
    I don't know which function to use in FPC, but you could also try GetTime(). If that fails: [b]check the help files for the correct date/time functions[/b]. I've checked the online FPC and found Now() listed among the date/time routines.
  • : : : : : : : Hey I am developing a game in which I need a variable that is
    : : : : : : : incremeting or decrementing ever 1 second. I need this variable to
    : : : : : : : keep active while a whole procedure takes place. The procedure
    : : : : : : : includes choice:keypressed. The thing is that I am having trouble
    : : : : : : : inserting this sort of timer since I a procedure is running.I though
    : : : : : : : that something like a while loop would be usefull but I don't know
    : : : : : : : how to do it. Can anyone please give me any suggestions
    : : : : : : : please?Thanks Alot.
    : : : : : : :
    : : : : : : Here's a basic example of a game-loop:
    : : : : : : [code]: : : : : :
    : : : : : : repeat
    : : : : : : if KeyPressed then
    : : : : : : begin
    : : : : : : ExtendedKey := false;
    : : : : : : ch := ReadKey;
    : : : : : : if ch = #0 then
    : : : : : : begin
    : : : : : : ExtendedKey := true;
    : : : : : : ch := ReadKey;
    : : : : : : end;
    : : : : : : ProcessKeyPress(ch, ExtendedKey);
    : : : : : : end;
    : : : : : : ProcessOtherStuff;{Examples:
    : : : : : : - Increment counters
    : : : : : : - Move other pieces}
    : : : : : : if ch <> 'q' then
    : : : : : : Sleep(150); { wait 150 ms. before starting next iteration
    : : : : : : This is necessary for the timing of the game
    : : : : : : Change to your liking }
    : : : : : : until ch = 'q'; { Press q to exit the loop }
    : : : : : : [/code]: : : : : :
    : : : : :
    : : : : :
    : : : : : Hey, thanks for you reply however if for example I have this code;
    : : : : :
    : : : : : for i:=1 to 30 do begin
    : : : : : writeln ('Enter Name');
    : : : : : readln (names[i]);
    : : : : : end;
    : : : : :
    : : : : : And I would like to attach a timer to it to check how fast is the
    : : : : : user in typing. Can you please give me an example on this ocde
    : : : : : please. I know that yours was helpful however I am just in the 2nd
    : : : : : year of programming and I am kind of noob. Thanks Again
    : : : : :
    : : : : Readln() blocks the code until the user presses enter. You cannot
    : : : : easily interrupt it to update a clock, while the user is typing. It
    : : : : is however possible to calculate the length of time the readln()
    : : : : statement took:
    : : : : [code]: : : :
    : : : : var
    : : : : Start, Finish: TDateTime;
    : : : : begin
    : : : : Start := Now();
    : : : : readln(SomeString);
    : : : : Finish := Now();
    : : : : Writeln('Time to enter: '+Finish-Start);
    : : : : end;
    : : : : [/code]: : : :
    : : : : The code above might be compiler dependent, check the help files for
    : : : : the correct date/time functions if you get compiler errors.
    : : :
    : : : Oh thanks alot, I never knew about this function, thanks alot man
    : : : yuo were really helpful.Thanks Again.
    : : :
    : :
    : : Hey I am really sorry but I just can't get it right with my
    : : compiler, FPC accepts the variable tdatetime but not the now
    : : identifier, while TP doesn't accept the tdatetime variable type. Can
    : : I install your compiler or can you tell me the code of the
    : : turbopascal compiler please?Thanks alot and sorry for annoying.
    : :
    : In TP, use the GetTime() procedure (see help files for details).
    : I don't know which function to use in FPC, but you could also try
    : GetTime(). If that fails: [b]check the help files for the correct
    : date/time functions[/b]. I've checked the online FPC and found Now()
    : listed among the date/time routines.

    Hey I just looked for help files and tried many different things over the last hour but I still can't get it right. Thanks anyway perhaps I will have more luck next time.
  • Hey I have managed to get it right finally, however I am encountering another problem. The problem is if when finding the time difference. if the game is executed at 14:41:55 and it is stopped and 14:42:30 the difference in seconds will be difficul to calculate.
  • : Hey I have managed to get it right finally, however I am
    : encountering another problem. The problem is if when finding the
    : time difference. if the game is executed at 14:41:55 and it is
    : stopped and 14:42:30 the difference in seconds will be difficul to
    : calculate.
    :
    If you use the TDateTime, then calculating the difference in time is simple: Subtract them.
    If your times consist of several variables, then I would suggest that you first calculate the start-time and end-time into seconds. Then you can subtract them. Here's the code to get the seconds form a time:
    [code]
    secs = (Hour*60+minutes)*60+seconds;
    [/code]
  • : : Hey I have managed to get it right finally, however I am
    : : encountering another problem. The problem is if when finding the
    : : time difference. if the game is executed at 14:41:55 and it is
    : : stopped and 14:42:30 the difference in seconds will be difficul to
    : : calculate.
    : :
    : If you use the TDateTime, then calculating the difference in time is
    : simple: Subtract them.
    : If your times consist of several variables, then I would suggest
    : that you first calculate the start-time and end-time into seconds.
    : Then you can subtract them. Here's the code to get the seconds form
    : a time:
    : [code]:
    : secs = (Hour*60+minutes)*60+seconds;
    : [/code]:

    I have managed to do it in a different way but still thanks alot for all your help, I really appreciate it.
  • : I have managed to do it in a different way but still thanks alot for
    : all your help, I really appreciate it.
    :

    You can also use the internal counter. TP example:
    [code]
    USES Crt, Dos;
    VAR
    StartTime,
    EndTime : LongInt;
    Begin
    ClrScr;
    StartTime := MemL[$40:$6C];
    Repeat
    GotoXY(1,1);
    WriteLn((MemL[$40:$6C])/18.2:2:2);
    Delay(2);
    Until Keypressed;
    EndTime := MemL[$40:$6C];
    WriteLn('TOTAL TIME = ',Round((EndTime-StartTime)/18.2));
    End.
    [/code]

    This counter (located at memory address [$0040:$006C]) is updated exactly 18.2 times a second. So you can just record it when it starts and ends and subtract the two. Divide by 18.2 and round off if you want the full second or don't round to get a more precise timing.

    Phat Nat
  • : : I have managed to do it in a different way but still thanks alot for
    : : all your help, I really appreciate it.
    : :
    :
    : You can also use the internal counter. TP example:
    : [code]:
    : USES Crt, Dos;
    : VAR
    : StartTime,
    : EndTime : LongInt;
    : Begin
    : ClrScr;
    : StartTime := MemL[$40:$6C];
    : Repeat
    : GotoXY(1,1);
    : WriteLn((MemL[$40:$6C])/18.2:2:2);
    : Delay(2);
    : Until Keypressed;
    : EndTime := MemL[$40:$6C];
    : WriteLn('TOTAL TIME = ',Round((EndTime-StartTime)/18.2));
    : End.
    : [/code]:
    :
    : This counter (located at memory address [$0040:$006C]) is updated
    : exactly 18.2 times a second. So you can just record it when it
    : starts and ends and subtract the two. Divide by 18.2 and round off
    : if you want the full second or don't round to get a more precise
    : timing.
    :
    : Phat Nat

    Hey thanks also for the idea it's good to get to know many functions.10x
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories