BACK TO THE STORY OF MIGHTY MOLLY


Now it is time to turn your attention back to the development of this chapter's game project, the Story of Mighty Molly. To create this game, you will follow the same five development steps that you've used to create previous chapter projects.

Designing the Game

The Story of Mighty Molly will be played on a single window, although the InputBox() function and MessageBox.Show method will be used to collect and display stored input and output. The game will therefore be made up of one form and the 11 controls listed in Table 5.5.

Table 5.5: Form Controls for the Story of Mighty Molly Game

Control Type

Control Name

Description

Label

lblWelcomeMsg

Displays the game's welcome message

Label

lblIntroText

Displays a brief prologue to the Story of Mighty Molly

Label

lblInstructions

Displays instructions for playing the game

Button

btnquestion1

Controls access to the game's first question

Button

btnquestion2

Controls access to the game's second question=

Button

btnquestion3

Controls access to the game's third question

Button

btnquestion4

Controls access to the game's fourth question

Button

btnquestion5

Controls access to the game's fifth question

Button

btnPlayGame

Displays the game story

StatusBar

stbControl

Displays status information as the game progresses

ProgressBar

prbControl

Provides a graphical indication of the game's progress

Step 1: Creating a New Visual Basic Project

The first step in creating the Story of Mighty Molly is to start Visual Basic and open a new project.

  1. If you have not already done so, start up Visual Basic 2005 Express and then click on File and select New Project. The New Project dialog will appear.

  2. Select Windows Application template.

  3. Type The Story of Mighty Molly as the name of your new application in the Name field located at the bottom of the New Project window.

  4. Click on OK to close the New Project dialog.

Visual Basic will now create a new project for you, including an empty form, which you'll use to create the game's user interface.

Step 2: Creating the User Interface

Let's continue by adding the controls required to set up the game's user interface. The overall design of the game's interface is shown in Figure 5.11.

image from book
Figure 5.11: Completing the interface design for the Story of Mighty Molly game.

  1. Begin by adding three Label controls to the form. By default, Visual Basic assigns the names Label1 through Label3 to these controls.

  2. Add six Button controls to the form and line them up horizontally beneath the last Label control. By default, Visual Basic names these controls Button1 through Button6.

  3. Add a StatusBar control to the form. By default, Visual Basic assigns this control the name StatusBar1.

  4. Add a ToolTip control to the form. By default Visual Basic assigns this control the name of ToolTip1.

  5. Finally, add a ProgressBar control to the form and move it on top of the StatusBar control, as shown in Figure 5.11.

The overall layout for the application's form is now complete.

Step 3: Customizing Form and Control Properties

Now it is time to customize the form and its controls. By this point in the book, you should be comfortable with the steps involved in modifying form and control properties. So, instead of walking you through each step that is involved in modifying every control, I am going provide you with tables for each control that identify the property changes that need to be made and leave it up to you to go ahead and make the changes.

The property modifications that need to be made to form1 are listed in Table 5.6.

Table 5.6: Property Changes for Form 1

Property

Value

Name

frmMain

BackColor

White

Cursor

Hand

FormBorderStyle

Fixed3D

MaximizeBox

False

MinimizeBox

False

StartPosition

CenterScreen

Text

The Story of Mighty Mo11y

The property changes for each of the form's three Label controls are listed in Table 5.7.

Table 5.7: Property Changes for the Label Controls

Control

Property

Value

Label1

Name

lb1WelcomeMsg

ForeColor

DarkBlue

Font.Bold

True

Font.Italic

True

Font.Size

16

Text

Welcome to The Story of Mighty Molly.

Label2

Name

lbllntroText

Text

Mighty Molly was the bravest of all the Mollys. She was fearless in battle and relentless in everything else. No one who ever met her was left untouched, for the mighty one had a certain mystical way about her that almost magically seemed to rub off on those around her.

 

Certainly, there never was before and may never be again anyone as mighty as Mighty Molly. For those of you who have not heard the tales of the mighty one, you are in luck, because today you get the chance to participate in the telling of the mighty one's last great adventure!

Label3

Name

lblInstructions

Font.Bold

True

Text

Instructions: To play the game and participate in the telling of "The Story of Mighty Molly," you must click on each of the following 5 buttons and supply the required information.

The property changes for each of the six Button controls are listed in Table 5.8.

Table 5.8: Property Changes for the Button Controls

Control

Property

Value

Button1

Name

btnQuestion1

BackColor

LightYellow

Font.Bold

True

Text

Question # 1

ToolTip

Click on button to answer the first question.

Button2

Name

btnQuestion2

BackColor

LightGray

Enabled

False

Font.Bold

True

Text

Question # 2

ToolTip

Click on button to answer the second question.

Button3

Name

btnQuestion3

BackColor

LightGray

Enabled

False

Font.Bold

True

Text

Question # 3

ToolTip

Click on button to answer the third question.

Button4

Name

btnQuestion4

BackColor

LightGray

Enabled

False

Font.Bold

True

Text

Question # 4

ToolTip

Click on button to answer the fourth question.

Button5

Name

btnQuestion5

BackColor

LightGray

Enabled

False

Font.Bold

True

Text

Question # 5

ToolTip

Click on button to answer the fifth question.

Button6

Name

btnPlayGame

BackColor

LightGray

Enabled

False

Font.Bold

True

Text

Tell me the story!

ToolTip

Click on button to see the story.

The property changes for the StatusBar control are listed in Table 5.9.

Table 5.9: Property Changes for the Statusbar Control

Property

Value

Name

stbControl

Panels

StatusBarPanel1 (Text = Game Ready!)

 

StatusBarPanel2 (Text = Progress:)

ShowPanels

True

Size.Height

24

SizingGrip

False

The property changes for the ProgressBar control are listed in Table 5.10.

Table 5.10: Property Changes for the Progressbar Control

Property

Value

Name

prbControl

Size.Height

22

Step 4: Adding a Little Programming Logic

Now that the game's user interface has been created and you have modified the appropriate form and control properties, it is time to begin coding. Start by double-clicking on form1. This will open the code editor and display code for the form's Load event. However, you don't need the code for the load event, so you can delete it, leaving just the opening and closing Class statements, as shown below.

 Public Class frmMain End Class 

Now add the following statements in between these two statements.

 'Declare constant used to specify titlebar message in pop-up Windows Const cTitleBarMsg As String = "The Story of Mighty Molly" 'Declare variables used throughout the application Private strCreature As String Private strRoom As String Private strColor As String Private strWeapon As String Private strFood As String 

The first statement defines a constant that will be used to supply a title bar message for all the pop-up windows displayed by the game. The next five statements declare variables. Each variable will be used to store a piece of information supplied by the player. Notice that the Friend keyword is used to limit the scope of each variable to the Class in which the variables were defined (such as form1). I could have just as easily used the Dim keyword and the application would have performed just the same. However, as a general rule, it is best to try and limit scope whenever possible.

Next, switch back to the form designer and double-click on the first button control. Then modify the button's Click event procedure, as shown below.

 'This Sub procedure prompts the player to answer the 1st question Private Sub btnQuestion1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnQuestion1.Click       'Post message in the first panel on the statusbar       StatusBarPanel1.Text = "Be Brave!"       'Prompt player to answer the first question       strCreature = InputBox("What scary creature scares you most?", _         cTitleBarMsg)       'Make sure the player entered something       If strCreature = "" Then         MessageBox.Show("You must answer all questions to " & _         "continue.", cTitleBarMsg)       Else         strCreature = strCreature.ToLower 'Convert input to lower- case         StatusBarPanel1.Text = "" 'Clear statusbar         btnQuestion1.Enabled = False 'Disable the 1st button con rol         btnQuestion2.Enabled = True 'Enable the 2nd Button control         btnQuestion1.BackColor = Color.LightPink 'Turn button pink         btnQuestion2.BackColor = Color.LightYellow 'Turn button yellow         prbControl.Value = 20  'Update the ProgressBar control       End If End Sub 

The first statement inside the procedure sets the Text property of the StatusBarPanel1 to "Be Brave!" The next statement uses the InputBox() function to prompt the user to provide the name of a scary monster. The player's input is stored in a variable named strCreature. The next statement checks to make sure that the player did not click on the Cancel button or that the player did not click on the OK button without supplying any information. The MessageBox.Show method is used to display an error message if the player fails to provide input. Otherwise the following actions are performed:

  • The text string supplied by the player is converted to all lowercase using the ToLower method.

  • The text displayed in the first Status Bar panel is cleared out.

  • The btnQuestion1 button is disabled, preventing the player from clicking on it again.

  • The btnQuestion2 button is enabled, allowing the player to click on it.

  • The background color of the btnQuestion1 button is set to LightPink to indicate that the input collected from the player for the first question was accepted.

  • The background color of the btnQuestion2 button is set to LightYellow to indicate that it is the currently active game button.

  • The ProgressBar control is updated to show that the application has collected 20 percent of the input that is required to tell the story.

Now, access the procedure of the btnQuestion2 control and modify it as shown below.

 'This Sub procedure prompts the player to answer the 2nd question Private Sub btnQuestion2_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnQuestion2.Click      'Post message in the first panel on the statusbar      StatusBarPanel1.Text = "Any Room Will Do!"      'Prompt player to answer the second question      strRoom = InputBox("What's the worst room in a castle?",_        cTitleBarMsg)      'Make sure the player entered something      If strRoom  = "" Then        MessageBox.Show("You must answer all questions to " & _        "continue.", cTitleBarMsg)      Else        strRoom = strRoom.ToLower   'Convert input to lowercase        StatusBarPanel1.Text = ""   'Clear statusbar        btnQuestion2.Enabled = False  'Disable the 2nd button control       btnQuestion3.Enabled = True 'Enable the 3rd Button control       btnQuestion2.BackColor = Color.LightPink  'Turn button pink       btnQuestion3.BackColor = Color.LightYellow 'Turn button yellow       prbControl.Value = 40  'Update the ProgressBar control     End If End Sub 

As you can see, the code for the btnQuestion2 control is almost identical to the code that you added to the btnQuestion1 control, the only differences being:

  • A different status bar message is displayed.

  • A different question is asked.

  • The player's response is stored in a different variable.

  • This time, btnQuestion2 is disabled and btnQuestion3 is enabled.

  • This time, the background color of btnQuestion2 to set to LightPink and the background of btnQuestion3 is set to LightYellow.

Now, access the procedure of the btnQuestion3 control and modify it as shown below.

 'This Sub procedure prompts the player to answer the 3rd question Private Sub btnQuestion3_Click(ByVal sender As System.Object,_   ByVal e As System.EventArgs) Handles btnQuestion3.Click      'Post message in the first panel on the statusbar      StatusBarPanel1.Text = "Any Color Will Do!"      'Prompt player to answer the third question      strColor = InputBox("What is your favorite color?",_        cTitleBarMsg)      'Make sure the player entered something      If strColor = "" Then        MessageBox.Show("You must answer all questions to " & _          "continue.", cTitleBarMsg)      Else        strColor = strColor.ToLower  'Convert input to lowercase        StatusBarPanel1.Text = ""  'Clear statusbar        btnQuestion3.Enabled = False 'Disable the 3rd button control        btnQuestion4.Enabled = True  'Enable the 4th Buttoncontrol        btnQuestion3.BackColor = Color.LightPink  'Turn button pink        btnQuestion4.BackColor = Color.LightYellow 'Turn button yellow        prbControl.Value = 60  'Update the ProgressBar control      End If End Sub 

Next, access the procedure of the btnQuestion4 control and modify it as shown below.

 'This Sub procedure prompts the player to answer the 4th question Private Sub btnQuestion4_Click(ByVal sender As System.Object,_   ByVal e As System.EventArgs) Handles btnQuestion4.Click      'Post message in the first panel on the statusbar      StatusBarPanel1.Text = "Better be enough to kill a " & _        strCreature & "!"      'Prompt player to answer the fourth question      strWeapon = InputBox("Name something that can be used as " & _        "weapon that you might find on the ground.", cTitleBarMsg)      'Make sure the player entered something      If strWeapon = "" Then        MessageBox.Show("You must answer all questions to " & _          "continue.", cTitleBarMsg)      Else        strWeapon = strWeapon.ToLower 'Convert input to lowercase        StatusBarPanel1.Text = ""  'Clear statusbar        btnQuestion4.Enabled = False 'Disable the 4th button control        btnQuestion5.Enabled = True  'Enable the 5th Button control        btnQuestion4.BackColor = Color.LightPink 'Turn button pink        btnQuestion5.BackColor = Color.LightYellow 'Turn button yellow        prbControl.Value = 80   'Update the ProgressBar control      End If End Sub 

Access the procedure of the btnQuestion5 control and modify it as shown below.

 'This Sub procedure prompts the player to answer the 5th question Private Sub btnQuestion5_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnQuestion5.Click      'Post message in the first panel on the statusbar      StatusBarPanel1.Text = "What's Your Preference?"      'Prompt player to answer the fifth question      strFood = InputBox("What is your favorite thing to eat?", _        cTitleBarMsg)      'Make sure the player entered something      If strFood = "" Then        MessageBox.Show("You must answer all questions to " & _          "continue.", cTitleBarMsg)      Else        strFood = strFood.ToLower 'Convert input to lowercase        StatusBarPanel1.Text = ""  'Clear statusbar        btnQuestion5.Enabled = False 'Disable the 5th button control        btnPlayGame.Enabled = True   'Enable the last Button control        btnQuestion5.BackColor = Color.LightPink 'Turn button pink        btnPlayGame.BackColor = Color.LightGreen 'Turn button green        prbControl.Value = 100  'Update the ProgressBar control      End If End Sub 

Finally, modify the code for the btnPlayGame control as shown below.

 'This Sub procedure is responsible for displaying the game's story Private Sub btnPlayGame_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnPlayGame.Click      'Declare local variable used to store the game's story      Dim strText As String = ""      'Post message in the first panel on the statusbar      StatusBarPanel1.Text = "Let's Rock!"      'Assemble the game's story      strText &= "A long time ago in a land far away, there "      strText &= "was a castle where the great Prince William lived."      strText &= "Prince William was a kindly boy who cared more for "      strText &= "his people than he did for himself. One day a storm "      strText &= "from out of nowhere swept upon the land where "      strText &= "Prince William lived. A mysterious " & strColor      strText &= " mist soon followed the storm. Out of this mist "      strText &= "appeared an evil " & strCreature & "."      strText &= ControlChars.NewLine & ControlChars.NewLine      strText &= "The " & strCreature & "'s heart was dark and cold. "      strText &= "The " & strCreature & " killed William's father, the      strText &= "good King Stefford. So William became the new king. "      strText &= "Summoning up all his bravery, King William rode out "      strText &- "ahead of his armies to do battle with the fearsome "      strText &= strCreature & ". However, King William's army was      strText &= "quickly crushed and King William was captured and "      strText &= "locked away in the castle's " & strRoom & "      strText &= ControlChars.NewLine & ControlChars.NewLine      strText &= "A call went out from far and wide for a great hero "      strText &= "to rescue King William. But no one dared answer "      strText &= "the call except for Mighty Molly, mightiest of all      strText &= "the Mollys. Within a fortnight the mighty one "      strText &= "arrived in the land where King William once ruled. "      strText &= "Upon hearing of the mighty oneâ_s arrival, the      strText &= strCreature & " quickly rushed out to meet her."      strText &= ControlChars.NewLine & ControlChars.NewLine      strText &= "Mighty Molly and the dreaded " & strCreature      strText &= " fought for 4 days and 4 nights. As they did battle "      strText &= "a cloud of " & strColor & " dust gathered around "      strText &= "them, making them invisible to all who tried to "      strText &= "watch. Finally, at the end of the 4th day, the "      strText &= strCreature & " fell dead at the mighty oneâ_ s feet.      strText &= "With her strength all but gone, Mighty Molly had "      strText &= "slain the " & strCreature & " with her final "      strText &= "blow, using a large " & strWeapon & " that she had "      strText &= "fallen on during the fight." & ControlChars.NewLine      strText &= ControlChars.NewLine & "When the fight was finally "      strText &= "over and the " & strColor & "mist finally cleared, "      strText &= "a great roar arose from the people who had gathered "      strText &= "around to watch. Happily, the people followed the "      strText &= "mighty one to King Williamâ_S castle where she      strText &= "freed him. In gratitude, good King William declared "      strText &= "a holiday and ordered his cooks to prepare a great "      strText &= "feast of meat, wine and " & strFood & ". At the "      strText &= "feast, King William offered to give his kingdom over "      strText &= "to Mighty Molly and he knelt at her knees and "      strText &= "offered up his crown. But Mighty Molly turned down "      strText &= "his offer, for she knew that King William was the "      strText &= "true king and that as mighty as she was, she needed "      strText &= "to be mightier still to rule as wisely as King "      strText &= "William." & ControlChars.NewLine      strText &= ControlChars.NewLine & "The End."      'Display the fully assembled story       MessageBox.Show(strText, cTitleBarMsg)      'Get the game ready to tell another story      StatusBarPanel1.Text = "Game Ready!" 'Update statusbar message      btnPlayGame.Enabled = False   'Disable the Button labeled Play      'Reset variables to prepare the game to allow the player to create      'a new story      strCreature = ""      strRoom = ""      strColor = ""      strWeapon = ""      strFood = ""      'Reset Button controls back to their original colors      btnQuestion1.BackColor = Color.LightYellow      btnQuestion2.BackColor = Color.LightGray      btnQuestion3.BackColor = Color.LightGray      btnQuestion4.BackColor = Color.LightGray      btnQuestion5.BackColor = Color.LightGray      btnPlayGame.BackColor = Color.LightGray      'Reset the ProgressBar control's value property to zero      prbControl.Value = 0      'Enable the first Button control      btnQuestion1.Enabled = True End Sub 

The first statement in this procedure declares a variable named strText, which will be used to hold the game's story line. The second statement posts a message of "Let's Rock!" in the left-hand status bar pane. The next series of statements builds the game's story line using the &= operator to append the lines of the story together. If you look closely at the text of the story, you see where the variables that contain the input provided by the player have been inserted throughout the story line. The MessageBox.Show method is then used to display the fully assembled story.

Trick 

The &= operator can be used as a shortcut for concatenating a text string to a String variable. It can be use as shown below.

 strText &= "Once upon a time..." 

An alternative to using the &= operator is to take the String variable and set it equal to itself before using the & operator to append a text string as demonstrated below.

 strText = strText & "Once upon a time..." 

As you can see, using the &= operator is easier and reduces the length of your code statements.

  • Display the string text "Game Ready!" in the left status bar pane

  • Disable the btnPlayGame button

  • Reset the value stored in each of the games variables to " "

  • Set the background color of btnQuestion1 to LightYellow and the background of all the other buttons to LightGray

  • Reset the Value property of the ProgressBar control to 0

  • Enable the btnQuestion1 button

Step 5: Testing the Execution of the Story of Mighty Molly Game

OK. That's it. The Story of Mighty Molly should be ready to run. Press F5 and put the game through its paces. If you have any errors, double-check your typing. Once you think you have things working the way you want, try testing the game again, this time feeding data that the game doesn't expect to receive and see how it handles it.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net