Game Development Reference
In-Depth Information
made the mistake. After we add the ability to mark mines with flags, the game will
be complete and ready for the AI.
Making It Fun: Adding Flags and a Safety Feature
To flag a blank square, the player right-clicks it. The player right-clicks a second
time to remove the flag. It makes no sense to do this to a revealed square.
Examine the events available to the Square class. Note that the Click event is in
bold to indicate an event handler is present for it. Scan the list carefully. There is
no right-click event. How will we detect a right-click? The list does have the
MouseUp event and many other mouse-related events. A control will get a MouseUp
event each time the user releases a mouse button, provided the user pressed the
button while the mouse was over that control. The MouseUp event always fires for
the control that got the MouseDown event. The behavior here is different from a
Click event. The Click event will not fire if you move the mouse off the control
between button press and button release.
Select the MouseUp event to get a code skeleton. Then add code to complete that
skeleton as shown here:
Private Sub Square_MouseUp(ByVal sender As Object, ByVal e As System.Windows.
Forms.MouseEventArgs) Handles Me.MouseUp
'This is where we catch right-click - but we have to
'check which button came up
If e.Button = Windows.Forms.MouseButtons.Right Then
'We should be part of a playing field
Dim theField As PlayingField = Me.Parent
'If not, we can't tell it anything
If theField Is Nothing Then Exit Sub
If Not Revealed Then
'We change the marking on the tile
'What is on the tile now?
Select Case Me.Text
Case ""
'Blanks get a flag
Me.Text = ShowFlag
Search Nedrilad ::

Custom Search