Jump to content

Add to a timer?


Recommended Posts

Well, I'm making a game that involves being able to add a little bit of time(or off in my case) to a timer, but how do I do this?

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Link to comment
Share on other sites

Old Edit destroys the AutoIt Tags

Example in the help file

; *** Demo to show a timer window
#include <GUIConstants.au3>
#include <Date.au3>
opt("TrayIconDebug",1)
Global $Secs, $Mins, $Hour, $Time
;Create GUI
GUICreate("Timer",120, 50)
GUICtrlCreateLabel("00:00:00", 10,10)
GUISetState()
;Start timer
$timer = TimerInit()
AdlibEnable("Timer", 50)
;
While 1
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> before")
  $msg = GUIGetMsg()
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> after")
  Select
     Case $msg = $GUI_EVENT_CLOSE
        Exit
  EndSelect
Wend
;
Func Timer()
  _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs )
  Local $sTime = $Time  ; save current time to be able to test and avoid flicker..
  $Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
  If $sTime <> $Time Then ControlSetText("Timer", "", "Static1", $Time)
EndFunc  ;==>Timer
Link to comment
Share on other sites

First, I need to know how long I want the timer to countdown.

$timeMinimum = 60000 // This would be one minute, or 60 seconds--60000 in milliseconds

To start the timer, I would need one more variable.

$timeStart = TimerInit()

Now you need to check the time. I'm assuming you have some sort of loop. If not just make a do-until loop. Here is the algorithm to check for time:

If TimerDiff($timeStart) <= $timeMinimum Then
     // do something
EndIf

You'd put whatever code you want to execute when the timer runs out in the "// do something" part of the algorithm.

Now, to add time to the timer, all you need to do is add it to $timeMinimum.

$timeMinimum = $timeMinimum + 10000 // This adds 10 seconds, which is 10000 miliseconds.

Finally, you'll probably want to have some way to know how much time is left, or to display the time.

$timeLeft = $timeMinimum - TimerDiff($timeStart)

I'm guessing you'll know how to display this number. It will be in milliseconds. You can use Round($timeLeft,-3) to convert it into seconds.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...