Jump to content

Small, simple, "Guess the Number" game.


CyNDeR
 Share

Recommended Posts

I created this little game to help me learn about using a GUI and thought someone else might be able to learn a little from it. It's almost fully commented. Thanks go to smashly who helped me with it.

#include <GUIConstants.au3>
GUICreate("Guess the Number!", 300, 150, -1, -1) ;Create the GUI
GUISetState()
GUICtrlCreateLabel("Pick a number between 1 and 20.", 70, 10, 160, 20) ;Instructions
$Guess = GUICtrlCreateInput("", 130, 30, 20, 20) ;Input the guess
$SubmitGuess = GUICtrlCreateButton("Guess!", 90, 60, 110, 20) ;Button to guess number
$Result = GUICtrlCreateInput("Created by CyNDeR..." & @CRLF & "with help from smashly.", 50, 90, 190, 50, $ES_MULTILINE) ;Result of the guess. IE: Right or Wrong, etc.
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SubmitGuess
            $Number = Random(1, 20, 1) ;Create a random number between 1 and 20
            If GUICtrlRead($Guess) <> $Number Then ;If the number is guessed wrong, then goto next line
                GUICtrlSetData($Result, "Wrong Guess!" & @CRLF & "Number Was: " & $Number) ;Show the correct answer
                GUICtrlSetData($Guess, "")
            Else
                GUICtrlSetData($Result, "Right Guess!" & @CRLF & "Number Was: " & $Number) ;Answer was correct
                GUICtrlSetData($Guess, "")
            EndIf
    EndSwitch
WEnd
Edited by CyNDeR

My scripts: Random Painter

Link to comment
Share on other sites

I like it. Good example.

Minor improvement:

#include <GUIConstants.au3>
GUICreate("Guess the Number!", 300, 150, -1, -1) ;Create the GUI
GUISetState()
GUICtrlCreateLabel("Pick a number between 1 and 20.", 70, 10, 160, 20) ;Instructions
$Guess = GUICtrlCreateInput("", 130, 30, 20, 20,$ES_NUMBER) ;Input the guess
$SubmitGuess = GUICtrlCreateButton("Guess!", 90, 60, 110, 20) ;Button to guess number
$Result = GUICtrlCreateLabel("Created by CyNDeR..." & @CRLF & "with help from smashly and Manadar.", 50, 90, 190, 50, $ES_CENTER) ;Result of the guess. IE: Right or Wrong, etc.
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SubmitGuess
            $Number = Random(1, 20, 1) ;Create a random number between 1 and 20
            If GUICtrlRead($Guess) < 1 OR GUICtrlRead($Guess) > 20 Then ; number must be between 1 and 20
                GUICtrlSetData($Result, "Wrong Guess!" & @CRLF & "Number must be between 1 and 20") ;Show an error message
            Else
                If GUICtrlRead($Guess) <> $Number Then ;If the number is guessed wrong, then goto next line
                    GUICtrlSetData($Result, "Wrong Guess!" & @CRLF & "Number Was: " & $Number) ;Show the correct answer
                Else
                    GUICtrlSetData($Result, "Right Guess!" & @CRLF & "Number Was: " & $Number) ;Answer was correct
                EndIf
            EndIf
            GUICtrlSetData($Guess, "")
    EndSwitch
WEnd
Edited by Manadar
Link to comment
Share on other sites

Ah, i didnt think about having an error message if the number was over 20, good idea. I also didnt think that i could blank out the inputbox at the end of the loop instead of each case. Heh, this script is teaching me more than i thought it would.

Glad you also like it. I also added in $ES_NUMBER so only numbers could be typed in the input box.
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...