Jump to content

Debug Pitch Guess Game


Recommended Posts

Hello all you sexy devoted autoit masters. I am having an issue with my code and would appreciate any advice.

The problem: the 'play again', 'donate', and 'exit' buttons only work the first time through the game. So, the game can only be played twice before something strange happens in the code. I'm guessing that part of the problem is that I don't really understand how the oneventmode works.

If you run the code, use a, b, c, d, e, f, or g to guess the notes.

Anyways, here's the code... in its entirety.

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

CreateGUI()

Func CreateGUI()


; Display Instructions
;MsgBox (1, "Instructions", "A tone will be played.  Press the corresponding note on the keyboard (Eg. A-G).  If you guess correctly the associated color will flash and a new tone will be played.")

; Determine screen resolution
global $ResY = @DesktopHeight*.94
global $ResX = @DesktopWidth

; Create a white window (GUI)
global $GUI = GUICreate("Game Start", $ResX, $ResY)

; Create Buttons
global $BtnExit = GUICtrlCreateButton("Exit", $ResX*.05, $ResY*.9, 120, 50)
global $BtnDonate = GUICtrlCreateButton("Donate", $ResX*.7, $ResY*.9, 200, 50)

Initialize()
EndFunc

Func Initialize()

GUISetBkColor("0xFFFFF0")
GUISetFont (40, 500)
global $GUIScore = GUICtrlCreateLabel ('Welcome to Guess Pitch', $ResX*.3, $ResY*.4, $ResX)
global $GUIFeedback = GUICtrlCreateLabel('Guess what note just played.', $ResX*.3, $ResY*.4+100, $ResX)
GUISetFont (15, 500)
global $GUIResponseTime = GUICtrlCreateLabel('...', $ResX*.3, $ResY*.3, $ResX)
global $GUIAvgResponseTime = GUICtrlCreateLabel('...', $ResX*.6, $ResY*.3, $ResX)

GUISetFont (20, 500)
global $GUIClock = GUICtrlCreateLabel('0:00', $ResX*.5, $ResY*.6, $ResX)

; Set Variables to 0
; Declare global variables
Global $user
Global $try = 0
Global $correct = 0
Global $Multiplier = 1
Global $ClockMax = 5 ; this is in seconds

Global $ResponseTimeSum = 0


; Display GUI
GUISetState()

; Clock Timer Initiate
Global $StartClock = 0
$StartClock = TimerInit()

Main()
EndFunc

Func Main()
        
    Local $NextTimems, $NextTime
    
    

While 1
    $NextTime = Random ( 1 , 20 , 1 )
    $NextTimems = $NextTime * $Multiplier
    Sleep ($NextTimems)
    ; play quick beep to catch my attention
    Beep ( 2489 , 20 )
    
    ; Create 'Random' Note
    $Note = Random ( 1 , 7 , 1 )

    Switch $Note
        ; Define color and tone associations
        Case 1
            Global $Name = "C"
            Global $Color = "0x0000ff"
            Global $Tone = 261.626
        Case 2
            Global $Name = "D"
            Global $Color = "0xffff00"
            Global $Tone = 293.67
        Case 3
            Global $Name = "E"
            Global $Color = "0x000000"
            Global $Tone = 329.63
        Case 4
            Global $Name = "F"
            Global $Color = "0xff0000"
            Global $Tone = 349.23
        Case 5
            Global $Name = "G"
            Global $Color = "0x008000"
            Global $Tone = 392
        Case 6
            Global $Name = "A"
            Global $Color = "0x00ffff"
            Global $Tone = 440
        Case 7
            Global $Name = "B"
            Global $Color = "0x800000"
            Global $Tone = 493.88

    EndSwitch
    
    ; Play sound, show color, and wait for input
    ColorWindow()           
WEnd
EndFunc ; ==> Main


Func ColorWindow()
    global $iOldOpt, $button
    $iOldOpt = Opt("GUIOnEventMode", 1)
    
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUICtrlSetOnEvent($BtnExit, "CLOSEClicked")
    GUICtrlSetOnEvent($BtnDonate, "DONATEClicked")
    HotKeySet("a","Check")
    HotKeySet("b","Check")
    HotKeySet("c","Check")
    HotKeySet("d","Check")
    HotKeySet("e","Check")
    HotKeySet("f","Check")
    HotKeySet("g","Check")
    
    global $answer = "tempvar"
    
    ; Reset background to white
    GUISetBkColor("0xFFFFF0")
    
    ; Start Response Timer
    Global $StartTime = TimerInit()
    
    ; Play tone to guess
    Beep ( $Tone , 1000 )
    
    ; Wait for response in infinite loop
    While 1
    $ClockTime = Round(TimerDiff($StartClock)/1000, 0)
    GUICtrlSetData($GUIClock, 'Time: ' & $ClockTime & ' seconds')
    Sleep ( 25 )
    WEnd
EndFunc   ; ==> ColorWindow

Func Check()
    $dll = DllOpen("user32.dll")
    While 1
        Sleep( 25 )
        
        $ClockTime = Round(TimerDiff($StartClock)/1000, 0)
        GUICtrlSetData($GUIClock, 'Time: ' & $ClockTime & ' seconds')
        
        If $Name = $answer Then
            
        ; Calculate Response Time
        $ResponseTime = Round(TimerDiff($StartTime), 0)
        $ResponseTimeSum = Round($ResponseTimeSum + $ResponseTime, 0)
        $AvgResponseTime = Round($ResponseTimeSum/$try, 0)
        ;msgbox(0,"test", $ResponseTime)
        ;msgbox(0,"test", $AvgResponseTime)
        
        ; Calculate Score
        $correct = $correct + 1
        global $score = round($correct/$try, 3)*100
        global $ScoreDisp = "Accuracy: " & $score & " %, "
        global $FinalScore = RounD($correct*(1/$AvgResponseTime)*10000, 0)
        GUISetBkColor($Color)  ; will change background color
        
        ; Give positive Feedback
        GUICtrlSetData($GUIFeedback, 'Good! That was ' & $answer)
        GUICtrlSetData($GUIScore, $ScoreDisp & $correct & " out of " & $try)
        GUICtrlSetData($GUIResponseTime, 'Last Response Time: ' & $ResponseTime)
        GUICtrlSetData($GUIAvgResponseTime, 'Average Response Time: ' & $AvgResponseTime)
                
        Sleep ( 1000 )
        
        ; Reset Feedback message to nothing
        GUICtrlSetData($GUIFeedback, '')
        ; Make sure font goes back to black
        GUICtrlSetColor ($GUIFeedback, 0x000000)
        GUICtrlSetColor ($GUIScore, 0x000000)
        GUICtrlSetColor ($GUIResponseTime, 0x000000)
        GUICtrlSetColor ($GUIAvgResponseTime, 0x000000)
        
            ExitLoop
    Elseif $ClockTime > $ClockMax Then
        Purgatory()
        
        ;MsgBox(1,"Time", 'Time"s Up!')
    Elseif _IsPressed("41", $dll) Then
        ;MsgBox(0,"_IsPressed", "A Key Pressed")
        $answer = "A"
        $try = $try + 1
        Sleep (200)
    Elseif _IsPressed("42", $dll) Then
        ;MsgBox(0,"_IsPressed", "B Key Pressed")
        $answer = "B"
        $try = $try + 1
        Sleep (200)
    Elseif _IsPressed("43", $dll) Then
        ;MsgBox(0,"_IsPressed", "C Key Pressed")
        $answer = "C"
        $try = $try + 1
        Sleep (200)
    Elseif _IsPressed("44", $dll) Then
        ;MsgBox(0,"_IsPressed", "D Key Pressed")
        $answer = "D"
        $try = $try + 1
        Sleep (200)
    Elseif _IsPressed("45", $dll) Then
        ;MsgBox(0,"_IsPressed", "E Key Pressed")
        $answer = "E"
        $try = $try + 1
        Sleep (200)
        If $Name = $Answer Then
            ;Set Font to white because background is black
            GUICtrlSetColor ($GUIFeedback, 0xF0FFFF)
            GUICtrlSetColor ($GUIScore, 0xF0FFFF)
            GUICtrlSetColor ($GUIResponseTime, 0xF0FFFF)
            GUICtrlSetColor ($GUIAvgResponseTime, 0xF0FFFF)
            Endif
            
    Elseif _IsPressed("46", $dll) Then
        ;MsgBox(0,"_IsPressed", "F Key Pressed")
        $answer = "F"
        $try = $try + 1
        Sleep (200)
    Elseif _IsPressed("47", $dll) Then
        ;MsgBox(0,"_IsPressed", "G Key Pressed")
        $answer = "G"
        $try = $try + 1
        Sleep (200)
    EndIf   
        ;MsgBox (0, "After EndIf", $try)
    WEnd
    DllClose($dll)
    ;MsgBox (0, "After WEnd", $try)
    
    Main()
EndFunc ; ==> Check

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
  ;and @GUI_WINHANDLE would equal $mainwindow 
  Exit
EndFunc

Func DONATEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
  ;and @GUI_WINHANDLE would equal $mainwindow 
  ShellExecute("www.google.com")
EndFunc

Func Purgatory()
        GUICtrlSetData($GUIFeedback, ' ')
        GUICtrlSetData($GUIScore, ' ')
        GUICtrlSetData($GUIResponseTime, ' ')
        GUICtrlSetData($GUIClock, "time's up!") ; change this back to time's up!
        GUISetFont (40, 500)
        global $GUIFinalScore = GUICtrlCreateLabel(' ', $ResX*.4, $ResY*.5, $ResX)
        GUICtrlSetData($GUIFinalScore, 'Final Score: ' & $FinalScore)
        global $BtnPlayAgain = GUICtrlCreateButton("Play Again?", $ResX*.4, $ResY*.8, 300, 50)
        GUICtrlSetOnEvent($BtnPlayAgain, "Initialize")
        
        GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
        GUICtrlSetOnEvent($BtnExit, "CLOSEClicked")
        GUICtrlSetOnEvent($BtnDonate, "DONATEClicked")
        While 1
            ;msgbox(0,'inside while','inside while')
            sleep(25)
            Wend
EndFunc
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...