Creating your Own Hidden Object Game with VB.NET – Part 3 – Adding Advanced Features


Welcome back! This is the last installment of this series. In this installment we will learn how to add a Hint Facility, how to change screen resolution, and how to make the game much more finalized. That means that we will make an introduction screen, and an ending screen, as well as a next stage. We have lots of work to do, so let us get started. We’ll do it form by form.


The very first thing we need to do concerning the design, is to add the Hint resources and Hint Picturebox onto frmJJM. You are welcome to make use of my Hint pictures or to create your own. We will need two. One showing when a Hint hasn’t been clicked, and one showing when a Hint was clicked upon. On frmJJM add a new Picturebox, and give it the following properties:

Property Setting
Name picJJMHint
BackColor Black
Dock Left
Location 0, 0

Add a timer control onto frmJJM and give it the following properties

Property Setting
Name tmrJJMHint
Interval 1000

Add the following code behind the tmrJJMHint_Tick event :

    Private Sub tmrJJMHint_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrJJMHint.Tick
        Static i As Integer ' Keep live counter

        If i >= 5 Then 'After 5 seconds
            picItems(RandIndex).Image = picSources(RandIndex) 'Get Picture
            picJJMHint.Image = New Bitmap(My.Resources.hint1) 'Show Hint Busy Picture
            i = 0 'Reset Counter
        End If

        i += 1 'Increment Counter
    End Sub

This simply shows another Hint picture once it is clicked on. It waits five seconds then reverts back to the previous picture, cueing the user that the Hint feature is ready to be used again. To make the Hint feature work properly we will need to add the following variable :

Private intHintsUsed As Integer 'Keeps track of how many hints were used

Add the picJJMHint_Click event procedure:

    Private Sub picJJMHint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picJJMHint.Click

        If intRemain > 0 Then 'If there are items to be found
            picJJMHint.Image = New Bitmap(My.Resources.hint) 'Change Hint picture

            GenHint() 'Generate a random number, ie. random picture to be shown as a hint
            intScore -= 40 'Penalize player for using a Hint
            intHintsUsed += 1 'Start Hints Counter

            If intScore <= 0 Then intScore = 0 'Make sure Score isn't less than 0
        End If
    End Sub

I make mention of GenHint inside the above sub. This sub we will add next :

    Private Sub GenHint()

        RandGen = New Random(Now.Millisecond) 'Generate random number
        RandIndex = RandGen.Next(0, 19) 'Limit number between 0 and 19

        VisHint(picItems(RandIndex)) 'Give visible clue of an item to player

        tmrJJMHint.Start() 'Start Hint timer

    End Sub

This sub generates a random number between 0 and 19 and then, based on that, makes use of the VisHint sub to give a hint to the player. This hint will be in the form of a dimmed copy screen of the picture in question. This makes the item quite identifiable. Let us add the VisHint sub now :

    Private Sub VisHint(ByVal SelPic As PictureBox)
        SelPic.CreateGraphics() 'Enable Drawing Capabilities

        SelPic.CreateGraphics().Clear(SelPic.BackColor) 'Clear Existing Drawings

        Dim SelGradWidth As Integer = 50 'Border is 50

        'Set Colours for gradients
        Dim SelGradCol1 As Color = Color.Transparent
        Dim SelGradCol2 As Color = Color.Fuchsia

        Dim JJMPen As New Pen(Brushes.Black, 7) 'New Pen

        'Image size
        Dim SelPicHeight As Integer = SelPic.Image.Height
        Dim SelPicWidth As Integer = SelPic.Image.Width

        'Draw Rectangle Points
        Dim SelPicPoint1 = New Point(0, 0)
        Dim SelPicPoint2 = New Point(0, SelPicHeight + (SelGradWidth * 2))
        Dim SelPicPoint3 = New Point(SelPicWidth + (SelGradWidth * 2), (SelGradWidth * 2) + SelPicHeight)
        Dim SelPicPoint4 = New Point(SelPicWidth + (SelGradWidth * 2), 0)

        'Calculate Area
        Dim SelPicArea As New Rectangle(0, 0, (SelGradWidth * 2) + SelPicWidth, (SelGradWidth * 2) + SelPicHeight)

        Dim JJMBrush As New PathGradientBrush( _
            New Point() {SelPicPoint1, SelPicPoint2, SelPicPoint3, SelPicPoint4}) 'Normal Brush

        JJMBrush.CenterPoint = New PointF( _
            ((SelGradWidth * 2) + SelPicWidth) / 2, _
            ((SelGradWidth * 2) + SelPicHeight) / 2) 'Centered Brush

        JJMBrush.CenterColor = SelGradCol2 'Set Center Colour
        JJMBrush.SurroundColors = New Color() {SelGradCol1} 'Set Surrounding Colours

        SelPic.CreateGraphics().FillRectangle(JJMBrush, SelPicArea) 'Fill
        SelPic.CreateGraphics().DrawRectangle(JJMPen, SelPicArea) 'Draw

        ' Draw Copied Image
        SelPic.CreateGraphics().DrawImage(SelPic.Image, New PointF(SelGradWidth, SelGradWidth))
    End Sub

It looks more complicated than it is. The trick is to get the image in question’s dimensions and copy that onto a new image. With a twist… I created a gradient brush and made use of its CenterColor and SurroundColors properties to layer a gradient skin onto the copied image. This gives a nice effect. The next two pictures show how the Hint button changes when it is clicked, as well as a how the hint looks, in this case how it looks on the violin item.

Shows Hint is busy processing
Figure 1Shows Hint is busy processing

The Hint Effect
Figure 2The Hint effect.

Hints used on frmSummary
Figure 3Hints used on frmSummary

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read