Visual Basic Games: Week 3

Visual Basic Games: Week 3
Global variables, parameters, Select,
KeyDown
Enable, Visible, Focus
State of Game
Read chapter 3
Global versus Local Variables
• Variables (aka internal variables) hold data
independent of properties in controls.
• Global variables are defined (persist) independently of
any particular procedure. Declared (using DIM) in the
(General) section ‘above’ all the procedures.
• Local variables are defined & only persist within a
procedure.
• Rule: local, except when a value must be global.
• Rule: don’t economize on names or length of names.
Use i, j, n, for simple indexing, otherwise use
meaningful names.
• Convention: use int, sng, bln, etc. as part of name.
Option Explicit
• Type in as first line in (General) section
(this is first line in the code window).
• This will force an error if and when you use
a variable without first declaring it in a DIM
statement.
• WHY do this? This will warn you of any
spelling errors.
Parameters
• Procedures (such as event procedures or
procedures you will define yourself in the later
projects) can have parameters. These are values
set by VB or by the calling program.
Sub lblCard_Click(Index as Integer)
Sub Form_KeyDown(KeyCode as Integer, Shift as Integer)
• When you define your own procedures, these two facts will be
significant (and will be repeated)
– Parameters are ‘call by reference’. The called procedure can change the
value
– The name of the parameter has local scope. The name in the calling
procedure can be different than the name in the called procedure.
• In Cannonball, hittarget is called for two different reasons and different names
are used.
Conditional statements
• IF condition Then
1 or more lines of code
[Else
1 or more lines of code]
End If
• Select Case expression
Case 1 or more values OR conditions
code
Case 1 or more values or conditions
code
[Case Else]
code ]
End Select
Chance game features
• Rules of the game:
– Add up values on the dice
– on a ‘first throw’, 7 or 11 wins, 2, 3 or 12 loses. For
anything else, the value becomes ‘the point’
– On follow-up throws, throwing the point again means a
win. Throwing a 7 means a loss.
• Implications for implementation: need to maintain
information on what the status is. This is called the
state of the game. The state of the chance game is
– Is it a first or a follow-up throw?
– What is ‘the point’
Chance Design Tasks
• State of the game—done with global variables
• Get (pseudo-) random values—use Randomize and Rnd.
Also use expression:
Int(Rnd*6) + 1
• Selectively show images of dice—done with image
controls, the Visible property, and using an extra control in
the case of doubles (e.g., two 2s)
• Display rules: command button code: MsgBox “…”
• End game: use command button code: End
• Rules of game: done using Select statements within Select
statement (outer statement distinguishes between first
throw and follow-up throw)
Preview: Timers
• The timer control has one event: timer.
• and two critical properties:
– Interval
– Enabled
• The timer event will happen if Enabled is set to
True and Interval is non-zero. The timer event
will happen every Interval milliseconds.
– If Interval is set to 500, the timer event will happen
every ½ second.
• Timer is more like an alarm clock.
Top
Left
Top and Left of the
bounding rectangle
Top has been increased
Moving ball
• Place a timer control on the form (it will not
be visible). Set interval to 300, enabled to
True. Place a shape control called shpBall.
(make it a solid circle).
• Code in timer1.timer:
shpBall.Top = shpBall.Top + .5*shpBall.Height
• Run program. You will need to use the stop button to stop
it.
How to move ball….
• 1000 twips:
– up
– left
– right
• How to move ball a different amount?
Project: control direction of
object
• Objective: use keys in keypad to set (change)
direction of moving ball.
• Need
–
–
–
–
–
Way to move (animate) a ball
Event that signals key down = Form_KeyDown
Key codes associated with the selected keys
Select statement for specifying action for each case
Way to encode right, left, up, down, and later diagonal
directions
Move a ball
• Do animation using timer control and a start
button that sets the interval and enables the
time.
• Code in tmr.Timer (happens every interval)
shpBall.Top = shpBall.Top + vspeed * shpBall.Height
shpBall.Left = shpBall.Left + hspeed * shpBall.Width
• vspeed and hspeed will be changed in the event procedure
for KeyDown
Setting the two speeds
Sub Form_KeyDown(KeyCode as Integer, Shift as Integer)
Select Case KeyCode
case leftkey
hspeed = -unit
vspeed = 0
case upkey
hspeed = 0
vspeed = -unit
case sekey
hspeed = unit
vspeed = unit
….
Case Else
• Player should be given feedback for a bad
keystroke
…
Case else
MsgBox “Click one of the direction keys”
or (for your own debugging)
MsgBox “You clicked key” & Str(KeyCode)
End Select
KeyDown
• Need to make sure that the Form_KeyDown
event is triggered. This requires you do
disable (set the enabled property to false)
other controls on the board and also
setfocus for the form.
How do you know to do this: I learned from
experience….
cmdStart_Click
•
•
•
•
tmrBall.Interval = 100
tmrBall.Enabled = True
cmdStart.Enabled = False
frmControl.SetFocus
Form_Load
unit = .3
upkey = 104
rightkey = 102
downkey = 98
leftkey = 100
nekey = 105
sekey = 99
swkey = 97
nwkey = 103
How to stop?
• Use a designated key (add to Form_Load)
stopkey = 101
• In the Select Case statement in KeyDown
add,
Case stopkey
tmrBall.Enabled = False
cmdStart.Enabled = True
Global variables
• Normally, variables are active / accessible / alive
just in the local subroutines and functions
• … but, you need all these variables to persist
between events
– vspeed, hspeed are set in KeyDown and used in
tmrBall.Timer
– leftkey, rightkey, etc. are set in form_Load and used in
KeyDown
So, you need to have a DIM statement (called a
declaration) in the (General) section
Review: general
• Incremental development
• Important: you don’t think of things in the
complete, proper, final order. Divide and
conquer = take the task and divide it into
subtasks.
• Incremental testing: do first with just two
directions (left and right), then up and
down, then diagonals, then stop.
Review: specific
• tmrBall.Timer eventhandler moves the ball using
the vspeed and hspeed, which were set elsewhere
• form_keydown eventhandler uses cases within a
select case statement to set vspeed and hspeed
– one of the cases will turn off the timer and reset the
start button
• cmdStart_Click eventhandler starts off the
animation by setting tmrBall.Interval and
tmrBall.Enabled. It also turns itself off (sets
cmdStart.Enabled to false) and frmControl.Focus
to True.
Assignments
• In lab,
– implement the control exercise or
– work on chance (dice) game
• Work on controlling ball exercise
• Complete chance to show. Work on
bouncing ball, if you finish chance early.