Dim X as Integer 'Creates an Integer Variable Dim Name as String 'Creates a String VariableThere are several different kinds of common variables:
Your average text string. It can hold all letters and numbers.
Will hold any number from -256 to 256.
Can hold numbers from -2,147,483,648 to 2,147,483,647.
The smallest variables. It can only store 1 and 0. These are normally used as true/false variables. 1 being true 0 being false.
Variable scope is where in your program your variables can be seen. A variable declared in the declarations section (this coding section can be found by pulling down the left combo box of the coding form) has a global scope and can be accessed anywhere in your project.
While a variable that is declared inside your code, between the Sub and the End Sub, can only be accessed in that sub.
if text1 = "Yes" then
The operator would be the "=". I'm sure we are very familiar with the "=" operator.
Other operators are:
= equals <= less then or equal to <> does not equal => greater or equal too* only the = and <> equal operators can be used with text.
If [condition] then [statement] end ifFor Example
If text1.text = "Programmers are lovable" then text2.text = "Programmers are very lovable" End ifThis Function would display "Programmers are very lovable" in text2 only if "Programmers are lovable" was displayed in Text 1.
A continuation of this is the elseif and else condition statements. They are used after an If statement. The syntax is:
If [first condition] then [code] elseif [second condition] then [code] elseif [third condition] then [code] else 'does not need a condition because it is always triggered if 'the other conditions are all not. [code] end ifThere may be as many elseif statements as needed but only one if and else statement per conditional group.
Add these controls to the form:
Now adjust the caption properties of the labels property so that they
look like the following. Also for the sake of programming conventions adjust
the text box names to match that of the caption beside it. Start the names
for the user with "Y" and the opponents with "O". For example the last
text box's name would be YHP.
Now we start on the code.
The syntax for adding combo box items is:
[combo box].additem [item]
The code we are going to add into the form1 code is triggered by the form loading (appearing). In order to do this double click on the form and it will automatically go to "Load" event as that is the default event. Add the script:
Private Sub Form1_load() 'add the items for the different commands. combo1.additem "Attack" combo1.additem "Retreat" combo1.additem "Flank" Combo1.additem "Approach" combo1.additem "Board" 'Enters the starting caption in the event label label13.caption = "There is a ship attacking us. What would you like do captain?" 'Starting values YType.Text = "Barque" 'sets type YArmour.Text = "25" 'sets armor YCannon.Text = "20" 'number of cannon YMen.Text = "100" 'men YShots.Text = "500" 'shots YHp.Text = "750" 'hp 'starting opponents values OType = "Corvette" 'sets type OArmour = "10" 'armor OCannon = "25" 'cannon OMen = "150" 'men OShots = "750" 'shots OHp = "750" 'hp End subNow lastly we have to make the commands and add some AI (artificial intelligence). We do this with the command button: (oh just for those who think AI is mystical and magical, it is not. It is but a series of algorithms to base computer moves on certain conditions rather then any means of sentience.) If you notice while originally we had to add ".caption" to our labels and ".text" to access the properties of our labels and text boxes, this is not required for ONLY these properties as they default to caption or text if no property is given.
Private Sub Command1_Click() If Combo1 = "Attack" Then If Distance >= 100 Then Label13 = "They are too far." ElseIf YPosition = 0 Then Label13 = "You are not in a attack position" ElseIf Distance < 100 Then OHP = OHP - 2 * YCannon + OArmour OMen = OMen - 0.5 * YCannon YShots = YShots - YCannon Label13 = "They have been hit." YPosition = 0 Combo1 = "Flank" End If ElseIf Combo1.Text = "Retreat" Then Label13 = "You are retreating." YPosition = 0 ElseIf Combo1.Text = "Flank" Then YPosition = 1 Label13 = "We are now ready to attack them." Combo1.Text = "Attack" ElseIf Combo1.Text = "Approach" Then YPosition = 1 Distance = Distance - 20 ElseIf Combo1.Text = "Board" Then If Distance < 10 Then YMen = YMen - OMen If YMen > 0 Then Label13 = "You Win!!!" Else Label13 = "All your men where destroyed." End If Else Label13 = "You are too far away." End If End If 'the & command adds the strings so that they form one 'long string Form1.Caption = "(Distance =" & Distance & " )" 'The Events If OHP < 0 Then Label13 = "Their ship is sunk." ElseIf OMen < 0 Then Label13 = "You Win." ElseIf YMen < 0 Then Label13 = "You loose." 'The AI ElseIf Distance >= 100 Then Distance = Distance - 20 ElseIf OPosition = 0 Then OPosition = 1 Else YHP = YHP - 2 * OCannon + YArmour 'the int command rounds the produced number to the nearest 'integer YMen = Int(YMen - OCannon.Text * 0.5) OPosition = 0 OShots = OShots - OCannon End If End SubWe will do more on this project in the next chapter with random numbers, message boxes , & , and/or operator and form switching.
PREVIOUS CHAPTER HOME