Jump to content

Placement of a continuous If-check in While


nf67
 Share

Recommended Posts

Hi,

I have a little script in which constant checks need to be performed (Continously, not after a certain button is pressed ).

I set the thing up but when I remove the ;'s, the other elements don't really work anymore ($Score keeps resetting to 0 ).

Perhaps I put the code somewhere wrong, please have a look, the parts that aren't working are marked with ; and <----------:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Global $LAST = 1
Global $Points = 0
Global $Streak = 0
;Global $TIMER = TimerInit()  <--------------------------------------------------------------------------------------
;Global Const $TIMEOUT = 60000 <--------------------------------------------------------------------------------
$Slothor = GUICreate("Slothor", 173, 213, 192, 114)
$Commander = GUICtrlCreateEdit("1", 5, 10, 160, 120, BitOR($ES_CENTER,$ES_AUTOVSCROLL,$ES_WANTRETURN,$ES_READONLY,$ES_AUTOHSCROLL))
GUICtrlSetFont(-1, 37, 800, 0, "Arial")
$Button1 = GUICtrlCreateButton("1", 5, 128, 40, 60)
$Button2 = GUICtrlCreateButton("2", 45, 128, 40, 60)
$Button3 = GUICtrlCreateButton("3", 85, 128, 40, 60)
$Button4 = GUICtrlCreateButton("4", 125, 128, 40, 60)
$Score = GUICtrlCreateEdit("", 5, 184, 161, 25, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_WANTRETURN,$ES_READONLY))
GUICtrlSetData(-1, "Score")
GUICtrlSetFont(-1, 12, 800, 2, "Arial")
GUISetState(@SW_SHOW)



While 1
;If TimerDiff($TIMER) > $TIMEOUT Then                             <---------------------------------
;MsgBox(0,"Game Over","STOP! You scored"&$Points)        <---------------------------------
;EndIf                                                                            <---------------------------------
;if 1 then                                                                         <---------------------------------
;WinKill ( "Game Over" )                                                <---------------------------------
;$Points = $Points-$Points                                           <---------------------------------
;GuiCtrlSetData ($Score, $Points)                             <---------------------------------
;endif                                                                <---------------------------------
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $LAST = 1 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)        
            Beep ( 600, 500)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points) 
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
                        
        Case $Button2
            If $LAST = 2 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep ( 500, 500)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
        
        Case $Button3
            If $LAST = 3 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep ( 400, 500)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
        
        Case $Button4
            If $LAST = 4 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep (300, 500)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
;If $Streak=15                 <---------------------------------
;$Points=$Points+200             <---------------------------------
;GUICtrlSetData ($Score, $Points)   <---------------------------------
;GUICtrlSetData($Commander, "+200"&@crlf, 1)      <---------------------------------
;Beep (900, 1500)                 <---------------------------------
;$LAST = Random(1,4,1)       <---------------------------------
;GUICtrlSetData($Commander, $LAST&@crlf, 1)  <---------------------------------
    EndSwitch
WEnd
Edited by nf67
Link to comment
Share on other sites

Thank you Zedna, works now :P

One little thing though, I have this:

If TimerDiff($TIMER) > $TIMEOUT Then
    MsgBox(0,"Game Over","STOP! You scored"&$Points)
    EndIf
    if 1 then; <----------------------------------
    WinKill ( "Game Over" )
    $Points = $Points-$Points
    GuiCtrlSetData ($Score, $Points)

And I thought that if 1 then would mean "If OK is pressed on MsgBox"

but it seems this is true all the time, not just after I pressed OK ( Because it keeps resetting the score, see $Points = $Points-$Points ).

What did I do wrong? Is

if 1 Then
not the right code to check if OK was pressed?
Link to comment
Share on other sites

If TimerDiff($TIMER) > $TIMEOUT Then
     MsgBox(0,"Game Over","STOP! You scored"&$Points)
     WinKill ( "Game Over" )
     $Points = 0
     GuiCtrlSetData ($Score, $Points)
 EndIf
Thanks, useable for a different situation in my script, but here the MsgBox just pops up again because If TimerDiff($TIMER) > $TIMEOUT Then is still true...

The timer should be reset but I don't know how, I checked KillTimers, but couldn't get it to work.

SOLVED:

_Timer_KillTimer($Slothor, $TIMER)
            Global $TIMER = TimerInit()

=

Reset

Edit:

My second problem is that I can't get the hotkeys to work... I was told that I should create a function which did the same as the buttons and then bind that function to a key.

#include <ButtonConstants.au3>
 #include <EditConstants.au3>
 #include <GUIConstantsEx.au3>
 #include <GUIConstants.au3>
 #include <WindowsConstants.au3>
 #Include <Timers.au3>
 
 Global $LAST = 1
 Global $Points = 0
 Global $Streak = 0
 Global $TIMER = TimerInit()
 Global Const $TIMEOUT = 60000
 $Slothor = GUICreate("Slothor", 173, 213, 192, 114)
 $Commander = GUICtrlCreateEdit("1", 5, 10, 160, 120, BitOR($ES_CENTER,$ES_AUTOVSCROLL,$ES_WANTRETURN,$ES_READONLY,$ES_AUTOHSCROLL))
 GUICtrlSetFont(-1, 37, 800, 0, "Arial")
 $Button1 = GUICtrlCreateButton("1", 5, 128, 40, 60)
 $Button2 = GUICtrlCreateButton("2", 45, 128, 40, 60)
 $Button3 = GUICtrlCreateButton("3", 85, 128, 40, 60)
 $Button4 = GUICtrlCreateButton("4", 125, 128, 40, 60)
 $Score = GUICtrlCreateEdit("", 5, 184, 161, 25, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_WANTRETURN,$ES_READONLY))
 GUICtrlSetData(-1, "Score")
 GUICtrlSetFont(-1, 12, 800, 2, "Arial")
 GUISetState(@SW_SHOW)
 
 
 HotKeySet ("1", Press1());<------------------Hotkey part: From here
 Func Press1()
             If $LAST = 1 Then
             $Points = $Points+25
             $Streak = $Streak+1
             GUICtrlSetData ($Score, $Points)        
             Beep ( 600, 500)
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             else 
             $Points = $Points-50
             $Streak = $Streak-$Streak
             GUICtrlSetData ($Score, $Points) 
             GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
             Beep ( 100, 1500 )
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             EndIf
 EndFunc    ;<------------------Untill here ( I just copied the code form Button1 to get them do to the same thing)
 
 
 While 1
 
     $nMsg = GUIGetMsg()
     Switch $nMsg
         Case $GUI_EVENT_CLOSE
             Exit
         Case $Button1
             If $LAST = 1 Then
             $Points = $Points+25
             $Streak = $Streak+1
             GUICtrlSetData ($Score, $Points)        
             Beep ( 600, 500)
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             else 
             $Points = $Points-50
             $Streak = $Streak-$Streak
             GUICtrlSetData ($Score, $Points) 
             GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
             Beep ( 100, 1500 )
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             EndIf
                         
         Case $Button2
             If $LAST = 2 Then
             $Points = $Points+25
             $Streak = $Streak+1
             GUICtrlSetData ($Score, $Points)
             Beep ( 500, 500)
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             else 
             $Points = $Points-50
             $Streak = $Streak-$Streak
             GUICtrlSetData ($Score, $Points)
             GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
             Beep ( 100, 1500 )
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             EndIf
         
         Case $Button3
             If $LAST = 3 Then
             $Points = $Points+25
             $Streak = $Streak+1
             GUICtrlSetData ($Score, $Points)
             Beep ( 400, 500)
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             else 
             $Points = $Points-50
             $Streak = $Streak-$Streak
             GUICtrlSetData ($Score, $Points)
             GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
             Beep ( 100, 1500 )
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             EndIf
         
         Case $Button4
             If $LAST = 4 Then
             $Points = $Points+25
             $Streak = $Streak+1
             GUICtrlSetData ($Score, $Points)
             Beep (300, 500)
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             else 
             $Points = $Points-50
             $Streak = $Streak-$Streak
             GUICtrlSetData ($Score, $Points)
             GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
             Beep ( 100, 1500 )
             $LAST = Random(1,4,1)
             GUICtrlSetData($Commander, ""&@crlf, 1)
             Sleep (300)
             GUICtrlSetData($Commander, $LAST&@crlf, 1)
             EndIf
 
     EndSwitch
     
     If $Streak=15 Then
     $Points=$Points+200
     GUICtrlSetData ($Score, $Points)
     GUICtrlSetData($Commander, "+200"&@crlf, 1)
     Beep (900, 1500)
     $Streak = $Streak+1
     $LAST = Random(1,4,1)
     Sleep (300)
     GUICtrlSetData($Commander, $LAST&@crlf, 1)
     EndIf  
             
     If TimerDiff($TIMER) > $TIMEOUT Then
     $RetryAnswer = MsgBox(36, "Game Over", "You scored "&$Points &" points. Retry?", 5000)
         Select
             Case $RetryAnswer = 6
             WinKill ( "Game Over" ) 
             FileWrite(@ScriptDir & "\Scores.txt", $Points&@CRLF)
             $Points = 0
             GuiCtrlSetData ($Score, $Points)
       ;???? Reset $TIMER so the game can be played again.
             Case $RetryAnswer = 7 
             WinKill ( "Game Over" )
             GUISetState(@SW_HIDE, $Slothor)
             FileWrite(@ScriptDir & "\Scores.txt", $Points&@CRLF)
                 $Scores = FileRead(@ScriptDir & "\Scores.txt")
                 $Highscores = GUICreate("Highscores", 163, 58, 193, 115)
                 $ScoreView = GUICtrlCreateEdit("Scores:"&@CRLF & $Scores, 0, 0, 161, 57, BitOR($ES_WANTRETURN,$WS_VSCROLL,$ES_READONLY))
                 GUISetState(@SW_SHOW)
                         While 3
                             $msg3 = GUIGetMsg()
                             Select
                                 Case $msg3 = $GUI_EVENT_CLOSE
                                 WinKill ("Slothor")
                                 ExitLoop
                             EndSelect
                         WEnd
         EndSelect
     EndIf
 WEnd
Edited by nf67
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...