The message text to be displayed is formatted using VBScript string constants to make it more attractive.. Finally, the completed story is displayed using the VBScript MsgBoxfunction.. I
Trang 1Creating a Splash Screen
As I said, adding a splash screen to your script gives you an opportunity to display your Web site, game instructions, or other information you think will be useful to the user
The following statements show one way of building a splash screen The StrWelcomeMsg vari-able is used to define the text that will be displayed in the splash screen The message text
to be displayed is formatted using VBScript string constants to make it more attractive
‘Specify the message to be displayed in the initial splash screen
strWelcomeMsg = “Welcome to the story of ” & vbCrLf & _
vbCrLf & “CCC” & space(14) & “A” & vbCrLf & _
“C” & space(17) & “AAA” & vbCrLf & _
“CCCaptain A Adventure gets his super powers” & _ vbCrLf
‘ Welcome the user to the story
MsgBox strWelcomeMsg, vbOkOnly + vbExclamation, cGameTitle
Finally, the VBScript MsgBox()function is used to display the splash screen In this case, the vbOkOnly + vbExclamation MsgBox()constants instruct VBScript to display only the OK but-ton and the exclamation mark graphic on the pop-up dialog In addition, the cGameTitle constant has been added to display the script’s custom title bar message
Collecting User Input
The next five lines of code, shown next, use the VBScript InputBox()function to collect data provided by the user This code contains the following five questions/instructions:
• What is your name?
• Name a place you would like to visit
• Name a strange object
• Type the name of a close friend
• Type the name of your favorite dessert
‘Collect story information from the user
strName = InputBox(“What is your name?”, cGameTitle,”Joe Blow”)
strVacation = InputBox(“Name a place you would like to visit.”, _
cGameTitle,”Nevada”)
Trang 2strObject = InputBox(“Name a strange object.”, cGameTitle,”Soda Can”)
strFriend = InputBox(“Type the name of a close friend.”, _
cGameTitle,”Barney”)
strFood = InputBox(“Type the name of your favorite dessert.”, _
cGameTitle,”Pickle”)
Notice that the user is only given a little bit of information about the type of information the script is looking for This is intentional, and is meant to provide a certain amount of unpredictability to the story line
You may have also noticed the final argument on each of the InputBox()statements I have added the argument so that each dialog that is displayed by the script will automatically display a default answer Providing a default answer in this way helps the user, by giving an idea of the kind of information you’re trying to collect
Assembling and Displaying the Story
The last step in putting together the Captain Adventure script is to assemble the story This
is done by typing out the story’s text while inserting references to the script’s variables at the appropriate locations in the story In addition, the vbCrLf string constant is used to improve the display of the story
The entire story is assembled as a single string, which is stored in a variable called Story Finally, the completed story is displayed using the VBScript MsgBox()function
‘ Assemble the Captain Adventure story
strStory = “Once upon a time ” & vbCrLf & vbCrLf & _
strName & “ went on vacation in the far away land of “ & strVacation & _
“ A local tour guide suggested cave exploration While in the cave “ & _
strName & “ accidentally became separated from the rest of the tour “ & _
“group and stumbled into a part of the cave never visited before “ & _
“It was completely dark Suddenly a powerful light began to glow “ & _
strName & “ saw that it came from a mysterious “ & strObject & “ “ & _
“located in the far corner of the cave room “ & strName & “ picked “ & _
“it up and a flash of light occurred and “ &strName & “ was “ & _
“instantly transported to a far away world There in front of him “ & _
“was “ & strFriend & “, the ancient God of the legendary cave “ & _
“people “ & strFriend & “ explained to “ & strName & “ that “ & _
“destiny had selected him to become Captain Adventure! He was “ & _
“then returned to Earth and told to purchase a Winnebago and travel “ & _
Trang 3“the countryside looking for people in need of help To activate “ & _
“the superpowers bestowed by “ & strFriend & “ all that “ & strName & _
“had to do was pick up the “ & strObject & “ and say “ & strFood & _
“three times in a row.” & vbCrLf & vbCrLf & _
“The End”
‘Display the story
MsgBox strStory, vbOkOnly + vbExclamation, cGameTitle
The Final Result
Okay, now that you’ve written all the various parts of the programs, put them all together into a single script, as follows:
‘*************************************************************************
‘Script Name: Captain Adventure.vbs
‘Author: Jerry Ford
‘Created: 02/28/02
‘Description: This script prompts the user to answer a number of questions
‘and then uses the answers to create a comical action adventure story.
‘*************************************************************************
‘Perform script initialization activities
Option Explicit
‘Specify the message to appear in each pop-up dialog title bar
Const cGameTitle = “Captain Adventure”
‘Specify variables used by the script
Dim strWelcomeMsg, strName, strVacation, strObject, strFriend
Dim strFood, strStory
‘Specify the message to be displayed in the initial splash screen
strWelcomeMsg = “Welcome to the story of ” & vbCrLf & _
vbCrLf & “CCC” & space(14) & “A” & vbCrLf & _
“C” & space(17) & “AAA” & vbCrLf & _
“CCCaptain A Adventure gets his super powers” & _ vbCrLf
Trang 4‘ Welcome the user to the story
MsgBox strWelcomeMsg, vbOkOnly + vbExclamation, cGameTitle
‘Collect story information from the user
strName = InputBox(“What is your name?”, cGameTitle,”Joe Blow”)
strVacation = InputBox(“Name a place you would like to visit.”, _
cGameTitle,”Nevada”)
strObject = InputBox(“Name a strange object.”, cGameTitle,”Soda Can”)
strFriend = InputBox(“Type the name of a close friend.”, _
cGameTitle,”Barney”)
strFood = InputBox(“Type the name of your favorite dessert.”, _
cGameTitle,”Pickle”)
‘ Assemble the Captain Adventure story
strStory = “Once upon a time ” & vbCrLf & vbCrLf & _
strName & “ went on vacation in the far away land of “ & strVacation & _
“ A local tour guide suggested cave exploration While in the cave “ & _
strName & “ accidentally became separated from the rest of the tour “ & _
“group and stumbled into a part of the cave never visited before “ & _
“It was completely dark Suddenly a powerful light began to glow “ & _
strName & “ saw that it came from a mysterious “ & strObject & “ “ & _
“located in the far corner of the cave room “ & strName & “ picked “ & _
“it up and a flash of light occurred and “ &strName & “ was “ & _
“instantly transported to a far away world There in front of him “ & _
“was “ & strFriend & “, the ancient God of the legendary cave “ & _
“people “ & strFriend & “ explained to “ & strName & “ that “ & _
“destiny had selected him to become Captain Adventure! He was “ & _
“then returned to Earth and told to purchase a Winnebago and travel “ & _
“the countryside looking for people in need of help To activate “ & _
“the superpowers bestowed by “ & strFriend & “ all that “ & strName & _
“had to do was pick up the “ & strObject & “ and say “ & strFood & _
“three times in a row.” & vbCrLf & vbCrLf & _
“The End”
‘Display the story
MsgBox strStory, vbOkOnly + vbExclamation, cGameTitle
Trang 5Now, run the script and test it to make sure that everything works as expected Be aware that this script pushes the string length allowed by VBScript to the limit If the information that you supply to the script is too long, some of the story may end up truncated
Summary
You covered a lot of ground in this chapter You now know how to define and work with con-stants and variables, including VBScript’s built-in concon-stants and Windows environment variables In addition, you learned how to apply VBScript string constants to script output
to control the manner in which output is displayed You also learned about the VBScript variant and how to use built-in VBScript functions to convert data from one variant subtype
to another Finally, you learned how to store related collections of data in arrays for more efficient storage and processing, and to develop scripts that can process input passed to them at execution time
CH A L L E N G E S
1 Modify the Captain Adventure story by collecting additional user input and adding more text to the story line.
2 Try using an array to store the user input collected in the Captain Adventure story instead of storing data in individual variables.
3 Develop your own story for someone you know, and e-mail it to your friend as a sort of living greeting card.
4 Experiment with the VBScript string constants when developing your own story
to improve the format and presentation of your story’s output.
Trang 6Logic
5
Every programming language allows you to perform tests between two or
more conditions This capability is one of the cornerstones of programming logic It lets you develop scripts that collect input from the user or the user’s computer and compare it to one or more conditions Using the results of the tests, you can alter the execution of your scripts and create dynamic scripts that can adjust their execution according to the data with which they’re presented
By the time you have completed this chapter, you’ll know how to:
• Write scripts that test two conditions
• Write scripts that can test two or more conditions against a single value
• Write scripts that can test for a variety of different types of conditions
• Write scripts that work with a variety of built-in VBScript functions
Project Preview: The Star Trek Quiz Game
In this chapter, you’ll create a game that administers and scores a quiz based on
various Star Trek TV shows and movies The game asks the player a series of
ques-tions and then assigns a rank of Ensign, Lieutenant, Lieutenant Commander, Commander, Captain, or Admiral, based on the player’s final score Besides dis-playing the final results, the game also creates a report that shows every question that was asked, the player’s answer, the correct answer for any incorrectly answered question, the total number of questions answered correctly, and the player’s assigned rank
Trang 7Figures 5.1 through 5.4 show some of the interaction between the player and the game
During the development of this game, you will learn how to apply sophisticated conditional logic In addition, you’ll also learn how to work with a number of built-in VBScript functions
Figure 5.1
The game’s splash
screen invites
the user to take
the quiz.
Figure 5.2
This dialog
appears if the
user decides
not to play.
Figure 5.3
This figure shows
an example of
the types of
questions asked
by the game.
Figure 5.4
When the player
finishes the
questions, his
or her score is
tallied, and a rank
is assigned based
on the number
of questions
correctly
answered.
Trang 8Examining Program Data
In any programming language, you need to be able to test whether a condition is true or false to develop complex logical processes VBScript provides two different statements that perform this function These statements are
• If A statement that allows or skips the execution of a portion of a program based on
the results of a logical expression or condition
• Select Case A formal programming construct that allows a programmer to visually
organize program flow when dealing with the results of a single expression
You’ve already seen short demonstrations of the Ifstatement in earlier chapters of this book This is because even the simplest scripts require some form of conditional logic
The power and importance of these two statements cannot be overstated For example, let’s say you took a job from someone without knowing exactly what you’d be paid, and now you’re finished with the job and are waiting to be paid As you’re waiting, you think about what you want to do with your newfound fortune After a few moments, you decide that if you’re paid $250, then you’ll by a TV If you’re paid less, you think, you’ll buy a radio instead This kind of test lends itself well to an Ifstatement Let’s rewrite this scenario into a more program-like format
If your pay is equal to $250
Then Buy a TV
Else
Buy a Radio
EndIf
As you can see, the logic is very straightforward and translates well from English into pseudo code I used bold text to identify portions of the example to point out the key VBScript language programming components that are
involved I’ll go into greater detail about what each of
these keywords means a little later in the chapter
Back to our scenario: Perhaps after thinking about it a
few more minutes, you decide that there are a number
of things that you might do with your pay, depending
on how much money you receive In this case, you can
use the VBScript Select Casestatement to outline the
logic of your decisions in pseudo code format
Definition
Pseudo code is a rough, English-like
outline or sketch of a script By writing out the steps you think will
be required to write a script in pseudo code, you provide yourself with an initial first-level script design that will serve as a basis for building the final product
Trang 9Select Case Your Pay
Case If you get $250 you’ll buy a TV
Case If you get $200 you’ll buy a VCR
Case If you get $100 you’ll buy a radio
Case Else You’ll just buy lunch
End Select
Again, I have used bold text to identify portions of the example to point out key VBScript language programming components involved In the next two sections of this chapter, I’ll break down the components of the Ifand Select Case statements into greater detail and show you exactly how they work
The If Statement
The VBScript Ifstatement lets you test two values or conditions and alter the execution of the script based on the results of the test The syntax of this statement is as follows:
If condition Then
statements
ElseIf condition-n Then
statements
.
.
.
Else
statements
End If
Working with the If Statement
The Ifstatement begins with the Ifkeyword and ends with End If condition represents the
comparison being performed For example, you might want to see whether the value of Xis equal to 250, like this:
If X = 250 Then
The keyword Thenidentifies the beginning of a list of one or more statements statementsis
a placeholder representing the location where you would supply whatever script statements you want executed For example, the following example displays a complete If statement that tests to see whether a variable has a value of 250, and if it does (that is, the test provides equal to true), a message is displayed:
Trang 10If X = 250 Then
WScript.Echo “Go and by that TV!”
End If
You may add as many statements as you want between the Thenand End Ifkeywords
If X = 250 Then
WScript.Echo “Go and buy that TV!”
WScript.Echo “Buy a TV Guide while you are at it.”
WScript.Echo “And do not forget to say thank you.”
End If
But what happens if the tested condition proves false? Well, in the previous test, nothing However, by adding the Elsekeyword and one or more additional statements, the script is provided with an additional execution path
If X = 250 Then
WScript.Echo “Go and buy that TV!”
WScript.Echo “Buy a TV Guide while you are at it.”
WScript.Echo “And do not forget to say thank you.”
Else
WScript.Echo “OK Just purchase the radio for today.”
End If
Figure 5.5 provides a flowchart view of the logic used in this example
Figure 5.5
A flowchart
outlining the
logic behind a
typical If
statement.