Microsoft WSH and VBScript Programming for the Absolute Beginner Part 17 ppt

10 401 0
Microsoft WSH and VBScript Programming for the Absolute Beginner Part 17 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

140 Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Creating 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”) 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”) 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 “ & _ 141 Chapter 4 • Constants, Variables, and Arrays 142 “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 Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘ 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 143 Chapter 4 • Constants, Variables, and Arrays 144 Now, 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 constants 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. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition C HALLENGES 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. Conditional Logic 5 CHAPTER E very 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. CHAPTER 146 Figures 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. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition 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. 147 Chapter 5 • Conditional Logic Examining 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 If statement 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 If statement. 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 Case statement 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. 148 Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Select 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 If and Select Case statements into greater detail and show you exactly how they work. The If Statement The VBScript If statement 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 If statement begins with the If keyword and ends with End If. condition represents the comparison being performed. For example, you might want to see whether the value of X is equal to 250, like this: If X = 250 Then The keyword Then identifies the beginning of a list of one or more statements. statements is 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: If X = 250 Then WScript.Echo “Go and by that TV!” End If You may add as many statements as you want between the Then and End If keywords. 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 Else keyword 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. 149 Chapter 5 • Conditional Logic Figure 5.5 A flowchart outlining the logic behind a typical If statement. . number of built-in VBScript functions. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Figure 5.1 The game’s splash screen invites the user to take the quiz. Figure. more efficient storage and processing, and to develop scripts that can process input passed to them at execution time. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition C. space (17) & “AAA” & vbCrLf & _ “CCCaptain A Adventure gets his super powers” & _ vbCrLf Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘ Welcome the

Ngày đăng: 03/07/2014, 18:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan