Jump to content
Sign in to follow this  
nobrainer612

Stop the timer when a stop button is pressed

Recommended Posts

nobrainer612

Hello, I am trying to build a timer for a mini game. This time will count down from a sepcific time to 0. I wrote the code below to perform that task. But I am stuck in the part that when I press a button to stop the timer and resume when I press another button. I don't know how to pause the count down and save the current number $i and pass back to resume function. I hope somebody can give me some ideas, thank you so much!

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
        Exit
          Case $Start_Button
            StartTimer(90)
          Case $Stop_Button
            StopTimer()
          Case $Resume_Button
                ResumeTimer()

    EndSwitch
WEnd

Func StartTimer($Timer)   
   global $i = $Timer
   Do 
   GuiCtrlSetData($Label3, $i & " second")
   $i = $i - 1   
   Sleep(1000)
Until $i == 0
EndFunc

Func StopTimer() 
   
   
EndFunc 

Func ResumeTimer() 
   
EndFunc

Share this post


Link to post
Share on other sites
nobrainer612

Thanks for the tips. I will try to finish the code using TimerDiff and _TicksToTime

Edited by nobrainer612

Share this post


Link to post
Share on other sites
nobrainer612

Hello, I wrote the code below that able to count down from given time to 0. When I pressed the stop button, it will put the current number put into a variable. But my problem is, how do I stop the counter from counting after I press the stop button?

Thank you so much.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

$Form1_1 = GUICreate("Timer", 615, 393, 192, 124)
$Label2 = GUICtrlCreateLabel("Timer: ", 40, 120, 49, 25)

GUICtrlSetColor(-1, 0x008000)
$Label3 = GUICtrlCreateLabel("0 seconds", 136, 120, 80, 25)


$Start_Button = GUICtrlCreateButton("Start Timer", 360, 120, 120, 33)

$Stop_Button = GUICtrlCreateButton("Stop Timer", 488, 120, 120, 33)

$Reset_Button = GUICtrlCreateButton("Reset Timer", 360, 160, 120, 33)

$Resume_Button = GUICtrlCreateButton("Resume Timer", 488, 160, 120, 33)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $timer, $Secs, $Mins, $Hour, $Time, $current_left
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
        Exit
         
      Case $Start_Button
         $finished = 5 
         $timer = TimerInit()
         AdlibRegister("Timer", 250)
      Case $Stop_Button
         StopTimer()
      Case $Reset_Button
         ResetTimer()
      Case $Resume_Button
         ResumeTimer()
    EndSwitch
WEnd

Func Timer()
     _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)

    Local $sTime = $Time 
    if $finished - $Secs >= 0 Then
      GuiCtrlSetData($Label3, $finished - $Secs & " seconds")
    Else
      
    EndIf
EndFunc 


Func StopTimer() 
   
      $current_left = $finished - $Secs 
      GuiCtrlSetData($Label3, $finished - $Secs & " seconds")
      MsgBox(0,"Stop",$current_left)
EndFunc

Share this post


Link to post
Share on other sites
Kidney

make your ResumeTimer function start a new timer with the left over time. as for the timer in the GUI, just make the label disappear.

Share this post


Link to post
Share on other sites
nobrainer612

Thank you for your reply. Right now I am able to stop the timer using TimerDiff, but I have no idea how to resume. It will just restart the whole counting process. Below is my code:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

$Form1_1 = GUICreate("Timer", 615, 393, 192, 124)
$Label2 = GUICtrlCreateLabel("Timer: ", 40, 120, 49, 25)

GUICtrlSetColor(-1, 0x008000)
$Label3 = GUICtrlCreateLabel("0 seconds", 136, 120, 80, 25)


$Start_Button = GUICtrlCreateButton("Start Timer", 360, 120, 120, 33)

$Stop_Button = GUICtrlCreateButton("Stop Timer", 488, 120, 120, 33)

$Reset_Button = GUICtrlCreateButton("Reset Timer", 360, 160, 120, 33)

$Resume_Button = GUICtrlCreateButton("Resume Timer", 488, 160, 120, 33)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $timer, $Secs, $Mins, $Hour, $Time, $current_left
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
        Exit
         
      Case $Start_Button
         $finished = 5 
         $timer = TimerInit()
         AdlibRegister("Timer", 250)
      Case $Stop_Button
         StopTimer()
      Case $Reset_Button
         ResetTimer()
      Case $Resume_Button
         ResumeTimer()
    EndSwitch
WEnd
Func Timer()
     _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
      
    Local $sTime = $Time ; save current time to be able to test and avoid flicker..
    if $finished - $Secs >= 0 Then
       ;MsgBox(0,"Stop",int(TimerDiff($timer)))
      GuiCtrlSetData($Label3, $finished - $Secs & " seconds")
    Else
      
    EndIf
EndFunc 


Func StopTimer() 
      $timer= TimerDiff($timer)
      Global $dif = TimerDiff($timer)
      global $current_left = $finished - $Secs 
      GuiCtrlSetData($Label3, $current_left & " seconds")
      ;MsgBox(0,"Stop",$dif)
      ;MsgBox(0,"Stop",$current_left)
EndFunc

Func ResetTimer() 
   
EndFunc

Func ResumeTimer() 
      $timer = $timer - $dif
      $timer  = TimerInit()
      AdlibRegister("Timer", 250)
     _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
      GuiCtrlSetData($Label3, $current_left & " seconds")
  
    if $current_left - $Secs >= 0 Then
       ;MsgBox(0,"Stop",int(TimerDiff($timer)))
      GuiCtrlSetData($Label3, $current_left - $Secs & " seconds")
    Else
      
    EndIf
EndFunc

Share this post


Link to post
Share on other sites
Chimp

Hi nobrainer 612

if you allow a bit of simplifications ....  :)

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

Global $Paused = True ; True or False
Global $StartFrom = 5 ; how many seconds you want to countdown
Global $Counter = $StartFrom ; Actual timer position

AdlibRegister("Timer", 1000)

$Form1_1 = GUICreate("Timer", 615, 393, 192, 124)
$Label2 = GUICtrlCreateLabel("Timer: ", 40, 120, 49, 25)

GUICtrlSetColor(-1, 0x008000)
$Label3 = GUICtrlCreateLabel($StartFrom & " seconds", 136, 120, 80, 25)


$Start_Button = GUICtrlCreateButton("Start Timer", 360, 120, 120, 33)

$Stop_Button = GUICtrlCreateButton("Stop Timer", 488, 120, 120, 33)

$Reset_Button = GUICtrlCreateButton("Reset Timer", 360, 160, 120, 33)

$Resume_Button = GUICtrlCreateButton("Resume Timer", 488, 160, 120, 33)

GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Global $timer, $Secs, $Mins, $Hour, $Time, $current_left
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Start_Button
            StartTimer()
            #cs
                $finished = 5
                $timer = TimerInit()
                AdlibRegister("Timer", 250)
            #ce
        Case $Stop_Button
            StopTimer()
        Case $Reset_Button
            ResetTimer()
        Case $Resume_Button
            ResumeTimer()
    EndSwitch
WEnd
Func Timer()
    If Not $Paused And $Counter > 0 Then
        $Counter -= 1
        GUICtrlSetData($Label3, $Counter & " seconds")
    EndIf

    #cs
        _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
        
        Local $sTime = $Time ; save current time to be able to test and avoid flicker..
        if $finished - $Secs >= 0 Then
        ;MsgBox(0,"Stop",int(TimerDiff($timer)))
        GuiCtrlSetData($Label3, $finished - $Secs & " seconds")
        Else
        
        EndIf
    #ce
EndFunc   ;==>Timer


Func StopTimer()

    $Paused = True

    #cs
        $timer= TimerDiff($timer)
        Global $dif = TimerDiff($timer)
        global $current_left = $finished - $Secs
        GuiCtrlSetData($Label3, $current_left & " seconds")
        ;MsgBox(0,"Stop",$dif)
        ;MsgBox(0,"Stop",$current_left)
    #ce
EndFunc   ;==>StopTimer

Func ResetTimer()
    $Paused = True
    $Counter = $StartFrom
    GUICtrlSetData($Label3, $Counter & " seconds")

EndFunc   ;==>ResetTimer

Func ResumeTimer()

    $Paused = False

    #cs
        $timer = $timer - $dif
        $timer  = TimerInit()
        AdlibRegister("Timer", 250)
        _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
        GuiCtrlSetData($Label3, $current_left & " seconds")
        
        if $current_left - $Secs >= 0 Then
        ;MsgBox(0,"Stop",int(TimerDiff($timer)))
        GuiCtrlSetData($Label3, $current_left - $Secs & " seconds")
        Else
        
        EndIf
    #ce
EndFunc   ;==>ResumeTimer

Func StartTimer()
    $Paused = False
    $Counter = $StartFrom
    GUICtrlSetData($Label3, $Counter & " seconds")
EndFunc   ;==>StartTimer

bye

  • Like 1

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
nobrainer612

Hi nobrainer 612

if you allow a bit of simplifications ....  :)

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

Global $Paused = True ; True or False
Global $StartFrom = 5 ; how many seconds you want to countdown
Global $Counter = $StartFrom ; Actual timer position

AdlibRegister("Timer", 1000)

$Form1_1 = GUICreate("Timer", 615, 393, 192, 124)
$Label2 = GUICtrlCreateLabel("Timer: ", 40, 120, 49, 25)

GUICtrlSetColor(-1, 0x008000)
$Label3 = GUICtrlCreateLabel($StartFrom & " seconds", 136, 120, 80, 25)


$Start_Button = GUICtrlCreateButton("Start Timer", 360, 120, 120, 33)

$Stop_Button = GUICtrlCreateButton("Stop Timer", 488, 120, 120, 33)

$Reset_Button = GUICtrlCreateButton("Reset Timer", 360, 160, 120, 33)

$Resume_Button = GUICtrlCreateButton("Resume Timer", 488, 160, 120, 33)

GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Global $timer, $Secs, $Mins, $Hour, $Time, $current_left
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Start_Button
            StartTimer()
            #cs
                $finished = 5
                $timer = TimerInit()
                AdlibRegister("Timer", 250)
            #ce
        Case $Stop_Button
            StopTimer()
        Case $Reset_Button
            ResetTimer()
        Case $Resume_Button
            ResumeTimer()
    EndSwitch
WEnd
Func Timer()
    If Not $Paused And $Counter > 0 Then
        $Counter -= 1
        GUICtrlSetData($Label3, $Counter & " seconds")
    EndIf

    #cs
        _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
        
        Local $sTime = $Time ; save current time to be able to test and avoid flicker..
        if $finished - $Secs >= 0 Then
        ;MsgBox(0,"Stop",int(TimerDiff($timer)))
        GuiCtrlSetData($Label3, $finished - $Secs & " seconds")
        Else
        
        EndIf
    #ce
EndFunc   ;==>Timer


Func StopTimer()

    $Paused = True

    #cs
        $timer= TimerDiff($timer)
        Global $dif = TimerDiff($timer)
        global $current_left = $finished - $Secs
        GuiCtrlSetData($Label3, $current_left & " seconds")
        ;MsgBox(0,"Stop",$dif)
        ;MsgBox(0,"Stop",$current_left)
    #ce
EndFunc   ;==>StopTimer

Func ResetTimer()
    $Paused = True
    $Counter = $StartFrom
    GUICtrlSetData($Label3, $Counter & " seconds")

EndFunc   ;==>ResetTimer

Func ResumeTimer()

    $Paused = False

    #cs
        $timer = $timer - $dif
        $timer  = TimerInit()
        AdlibRegister("Timer", 250)
        _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
        GuiCtrlSetData($Label3, $current_left & " seconds")
        
        if $current_left - $Secs >= 0 Then
        ;MsgBox(0,"Stop",int(TimerDiff($timer)))
        GuiCtrlSetData($Label3, $current_left - $Secs & " seconds")
        Else
        
        EndIf
    #ce
EndFunc   ;==>ResumeTimer

Func StartTimer()
    $Paused = False
    $Counter = $StartFrom
    GUICtrlSetData($Label3, $Counter & " seconds")
EndFunc   ;==>StartTimer

bye

 

Oh wow. That is amazing. Thanks for the code. I think my understanding of Timer is not good enough, that is why I am stuck. But I learned a lot from this. Thanks again.

Share this post


Link to post
Share on other sites
Chimp

Hi nobrainer612

glad to help ;)
in this code,  there is no use of timers, but nearly all the work is done by  the AdlibRegister() function that keeps track of time and call automatically the Timer() function every 1 second. the rest is just an "if then"  statement and 2 variables tracking flags.  it is easier than it may seem.

Bye


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
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
Sign in to follow this  

×