CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2001
    Posts
    65

    How can I add a Timer/Clock to my game?

    Hi,

    I have completed a game and it works well.

    There is one more part to my game, that is, to add a clock (Timer) to it, so that when the game starts I can activate the timer, and when it finished I can deactivate it, and record the time taken.

    The object is to complete the game/level in the fastest time, so I would also like to be able to record the timer.

    Can anyone give me any info on code for this task.

    Thanks

    Mark


  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: How can I add a Timer/Clock to my game?

    You can use the Date function. This returns a Variant/Date corresponding to the cuurent time and date. There also is a function named DateDiff. This computes the difference between two dates. You can customize the interval length to, say, seconds.

    Enjoy,

    Gabreil

    *******************************************
    Thank you in advance for rating my answer :-)))))
    *******************************************
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How can I add a Timer/Clock to my game?

    You can use the GetTickCount API to get something. I believe it gives the number of milliseconds since the system was started or so. Get the value at the beginning of the level, and at the end. Then just substract them and you have the time ellapsed in milliseconds.

    private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () as Long

    Dim StartTime as long
    Dim EndTime as long

    private Sub StartLevel()
    StartTime = getTickCount
    End Sub

    private Sub EndLevel()
    EndTime = GetTickCount
    Msgbox "It took you " & Round((EndTime - StartTime)/1000,2) & " seconds to complete the level"
    End Sub






    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  4. #4
    Join Date
    Jun 2001
    Posts
    65

    Re: How can I add a Timer/Clock to my game?

    How can I attach this code to a button?

    Thanks

    Mark


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured