Overview

This game has one core object - the Game class. In this clsss there is one public method, and also one public Event.

The public createNew method creates a new game, with fourteen resulting variables that it is necessary to pass back to the main Form. A function returning Object() could've 

been used to return these variables, but a cleaner solution is to raise an event, passing these variables in a custom EventArgs argument.

In this example, you'll see how to create a custom EventArgs class, how to create a custom Event, and how to declare the Game Object in the main form...


The GameEventArgs Class

This Class contains fourteen public variables

Public Class GameEventArgs
    Public column1XIndex As Integer
    Public column2XIndex As Integer
    Public column3XIndex As Integer
 
    Public lockWord1 As String
    Public lockWord2 As String
    Public lockWord3 As String
 
    Public pinWord1 As String
    Public pinWord2 As String
    Public pinWord3 As String
    Public pinWord4 As String
 
    Public xIndex1 As Integer
    Public xIndex2 As Integer
    Public xIndex3 As Integer
    Public xIndex4 As Integer
End Class

Declaring the custom Event

The core Game class contains the custom Event

Public Event GameCreated(ByVal sender As Object, ByVal e As GameEventArgs)

Defining an object WithEvents

In the main Form, it's necessary to declare the object of type Game

Private WithEvents game As New Game()

By declaring WithEvents, the object becomes visible in the code editor dropdown lists, and you can see and choose any of the events it contains.


The GameCreated eventHandler

Private Sub game_GameCreated(ByVal sender As Object, ByVal e As GameEventArgs) Handles game.GameCreated
     'This is raised when the Game.createNew method has completed

    'The values passed in the EventArgs are needed by the Form to setup the game for play

End Sub

Raising the custom event in the Game Class

RaiseEvent GameCreated(Me, New GameEventArgs With { _
                                   .column1XIndex = column1XIndex, _
                                   .column2XIndex = column2XIndex, _
                                   .column3XIndex = column3XIndex, _
                                   .lockWord1 = lockWord1, _
                                   .lockWord2 = lockWord2, _
                                   .lockWord3 = lockWord3, _
                                   .pinWord1 = pinWord1, _
                                   .pinWord2 = pinWord2, _
                                   .pinWord3 = pinWord3, _
                                   .pinWord4 = pinWord4, _
                                   .xIndex1 = xIndex1, _
                                   .xIndex2 = xIndex2, _
                                   .xIndex3 = xIndex3, _
                                   .xIndex4 = xIndex4})

Here the variables all have the same name as the EventArgs Properties, but the Properties can be assigned any value or variable.


Conclusion

In Event Driven Programming, the state of the program is changed by Events, such as a Button when clicked which fires the Button.Click event. It is possible to create custom Events with custom EventArgs, and fire those Events at an appropriate time in the applications execution. An object can contain one, or many custom Events, all of which can be setup and used as shown in this example.


Articles related to game programming



VB.Net - WordSearch
VB.Net - Vertex
VB.Net - Perspective
VB.Net - MasterMind
VB.Net - OOP BlackJack
VB.Net - Numbers Game
VB.Net - HangMan
Console BlackJack - VB.Net | C#
TicTacToe - VB.Net | C#
OOP Sudoku - VB.Net | C#
OctoWords VB.Net | C#
OOP Buttons Guessing Game VB.Net | C#
OOP Tangram Shapes Game VB.Net | C#
VB.Net - Three-card Monte
VB.Net - Pascal's Pyramid
VB.Net - Split Decisions
VB.Net - Random Maze Games
C# - Crack the Lock


Download

Download here...