Tutorial hangman

From AutoIt Wiki
Revision as of 16:43, 17 November 2012 by IEvKI3gv9Wrkd41u (talk | contribs) (+Category:Tutorials)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is still a work in progress.

Lesson 1: Starting an application

  • Starts MSPaint, displays a welcome message.
  • Illustrates Run, RunWait, WinSetTitle, MsgBox, SplashTextOn, SplashOff

We're going to automate MSPaint to play Hangman. So lets start by launching mspaint.exe. According to the AU3 helpfile, we can use either Run or RunWait. The difference lies in when we get control back from the command.

Lets see this in action. Create a file called Hangman.au3 and copy in the following lines:

 ;
 ; Hangman with MSPaint
 ;
    ;we don't need to specify the path if the exe is in the system directory..
     RunWait ("MSPAINT.EXE") 
    
     MsgBox (0,"","Script continues")

When you run this, notice that the MsgBox pops up only after we shutdown mspaint. This won't do, because the MsgBox is standing in for the rest of the code that we still need to write. Lets see what happens if we use Run instead, so that the MsgBox comes up while mspaint is still running. Also, notice that the title of the mspaint window says "untitled - Paint": we'll change this to show that we're playing Hangman, and display a splash page for the same reason. We'll also maximise the mspaint window (see the change to the Run command).

We'll be using the title in some further commands, so lets create a variable for it instead of typing it out each time (and possibly misspelling it!):

 ;
 ; Hangman with MSPaint
 ;
 Dim $sTitle = "Hangman by AutoIt"
     
     ;Get things going
     SplashTextOn("Hangman", "This is a tutorial on how to use AutoIt v3",-1,-1,-1,-1,16)
     Run ("MSPAINT.EXE", "", @SW_MAXIMIZE) 
     WinSetTitle ("untitled - Paint", "", $sTitle)
     Sleep (2000)
     SplashOff ()
     
     MsgBox (0,"","Waiting")

But when we run this, we see that something is wrong: the title hasn't changed! And according to the helpfile, our syntax is correct. The problem is that mspaint takes a little longer to create the window that says "untitled - Paint" than the script takes to get to the point where it wants to change it. So lets just make sure with WinWait that the window exists before we change it with WinSetTitle.

At the same time, we'll change the dimensions of the splash page from the defaults, by altering the parameters to the SplashTextOn statement.

One last step for this lesson - if you've been running the scripts at each step as we've tested our changes (which is the best way to learn), you'll have several instances of mspaint hanging around unless you've closed them manually. So lets also add a feature that closes mspaint when the script terminates, using ProcessClose and capturing the Process ID for it from the Run command. Here's the final script for this stage:

 ;
 ; Hangman with MSPaint
 ;
 Dim $sTitle = "Hangman by AutoIt"
     
     ;Get things going
     SplashTextOn ("Hangman", "This is a tutorial on how to use AutoIt v3",400,100,-1,-1,16)
     $nPID = Run ("MSPAINT.EXE", "", @SW_MAXIMIZE)
     ;If you become stuck on the splash screen, capitalize the "u" in "untitled - Paint" on the following two lines to correct it
     WinWait ("untitled - Paint")
     WinSetTitle ("untitled - Paint", "", $sTitle)
     Sleep (2000)
     SplashOff ()
     
     MsgBox (0,"","Waiting")
     ProcessClose ($nPID)