Adding Sizzle to a Video Slot Game with VB.NET

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

In the previous two installments of this series we have concentrated on how things work, and making the logic work. With this final part, we will build in a way to win or lose, as well as add some effects when a win takes place. We will also polish off this article, so that everything works as it should, and that it is a real working game.

Design

Not much to do here. We just need to add a place where we can notify the user that he / she has won or lost and a way to add credits. Let us add the following to the form :

Control Property Setting
Button Name btnAddMoreCredits
  Location 554, 175
  Text Add Credits
Button Name btnPayOut
  Location 554, 204
  Text Pay Out
PictureBox Name picScore
  Location 24, 470
  Size 372, 50

Code

As usual, let us add all the necessary objects to our General Declarations section :

    Private arrAnimImages(9) As Image 'Array for GIF Animatd images
    Private Winnings As Integer 'Total winnings
    Private MaxBet As Boolean 'Max Bet / Normal Bet
    Private ColCounter As Integer 'Column match counter
    Private LineWinnings As Integer 'How much won per line
    Private ColWinnings As Integer 'How much won per column
    Private MainLineWinnings As Integer 'How much won for MAIN lines
    Private MainLineWinnings2 As Integer 'How much won for part of MAIN line
    Private MainLineCounter2 As Integer 'Part of MAIN line match counter

The bulk of our work now is to get our animations to work. These files were downloaded from the internet, I did not create them. I chose to make use of GIF files here, to show you how easy it is to add tiny animations to your projects. You do not always need additional libraries or long pieces of code to do it – the PictureBox in VS 2010 is extremely capable of handling animations. All you need to do is to set its Image property to the appropriate image, and voila, the animation will start! Be careful though, some GIF files might not be saved correctly, so then, and only then, the PictureBox will have issues displaying them. Initialize them inside Form_Load, under the arrImages array :

        'Load GIF files for animation purposes
        arrAnimImages(0) = My.Resources.applesmall
        arrAnimImages(1) = My.Resources.bananasmall
        arrAnimImages(2) = My.Resources.cherry2
        arrAnimImages(3) = My.Resources.fruitbowlz
        arrAnimImages(4) = My.Resources.grapesmall
        arrAnimImages(5) = My.Resources.lemonsmall
        arrAnimImages(6) = My.Resources.orangesmall
        arrAnimImages(7) = My.Resources.strawberrysmall
        arrAnimImages(8) = My.Resources.Kiwi
        arrAnimImages(9) = My.Resources.Pineapple_jumps

These files were also added to the project’s Resources, but feel free to use your own.

Because these animations should play once a winning combination has been struck, we need to edit our existing subs to accommodate this feature. Change your CheckMainLines and CheckRows as follows :

    Private Sub CheckMainLines()

        For i As Integer = 0 To 9
            'Line 1
            '0	0	0	0	0
            If Column1(0).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) And Column5(0).Image Is arrImages(i) Then
                ' Line 1 Match
                Column1(0).Image = arrAnimImages(i)
                Column2(0).Image = arrAnimImages(i)
                Column3(0).Image = arrAnimImages(i)
                Column4(0).Image = arrAnimImages(i)
                Column5(0).Image = arrAnimImages(i)
                MainLineCounter += 1 'Increment counter

                'Line 2
                '1	1	1	1	1
            ElseIf Column1(1).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And Column5(1).Image Is arrImages(i) Then
                ' Line 2 Match
                Column1(1).Image = arrAnimImages(i)
                Column2(1).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                Column4(1).Image = arrAnimImages(i)
                Column5(1).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 3
                '2	2	2	2	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) Then
                ' Line 3 Match
                Column1(2).Image = arrAnimImages(i)
                Column2(2).Image = arrAnimImages(i)
                Column3(2).Image = arrAnimImages(i)
                Column4(2).Image = arrAnimImages(i)
                Column5(2).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 4
                '3	3	3	3	3
            ElseIf Column1(3).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) And Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) And Column5(3).Image Is arrImages(i) Then
                ' Line 4 Match
                Column1(3).Image = arrAnimImages(i)
                Column2(3).Image = arrAnimImages(i)
                Column3(3).Image = arrAnimImages(i)
                Column4(3).Image = arrAnimImages(i)
                Column5(3).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 5
                '3	2	1	2	3
            ElseIf Column1(3).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) And Column5(3).Image Is arrImages(i) Then
                ' Line 5 Match
                Column1(3).Image = arrAnimImages(i)
                Column2(2).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                Column4(2).Image = arrAnimImages(i)
                Column5(3).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 6
                '0	1	2	1	0
            ElseIf Column1(0).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And Column5(0).Image Is arrImages(i) Then
                ' Line 6 Match
                Column1(0).Image = arrAnimImages(i)
                Column2(1).Image = arrAnimImages(i)
                Column3(2).Image = arrAnimImages(i)
                Column4(1).Image = arrAnimImages(i)
                Column5(0).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 7
                '1	0	1	0	1
            ElseIf Column1(1).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) And Column5(1).Image Is arrImages(i) Then
                ' Line 7 Match
                Column1(1).Image = arrAnimImages(i)
                Column2(0).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                Column4(0).Image = arrAnimImages(i)
                Column5(1).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 8
                '2	3	2	3	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) Then
                ' Line 8 Match
                Column1(2).Image = arrAnimImages(i)
                Column2(3).Image = arrAnimImages(i)
                Column3(2).Image = arrAnimImages(i)
                Column4(3).Image = arrAnimImages(i)
                Column5(2).Image = arrAnimImages(i)
                MainLineCounter += 1

                'Line 9
                '2	1	1	1	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) Then
                ' Line 9 Match
                Column1(2).Image = arrAnimImages(i)
                Column2(1).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                Column4(1).Image = arrAnimImages(i)
                Column5(2).Image = arrAnimImages(i)
                MainLineCounter += 1


            End If
        Next

        If MainLineCounter > SelLines Then MainLineCounter = SelLines

        If MainLineCounter > 0 And MainLineCounter < 10 Then
            If Not MaxBet Then
                MainLineWinnings = MainLineCounter * 15 + Bet
            Else
                MainLineWinnings = MainLineCounter * 30 + (Bet * 2)
            End If
        Else
            MainLineCounter = 0
            MainLineWinnings = 0
            Winnings = 0
        End If
    End Sub

    Private Sub CheckRows()

        For i As Integer = 0 To 9
            If Column1(0).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And Column3(0).Image Is arrImages(i) Then
                ' Row 1 Match 1
                ' Three possible matches
                Column1(0).Image = arrAnimImages(i)
                Column2(0).Image = arrAnimImages(i)
                Column3(0).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column2(0).Image Is arrImages(i) And Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) Then
                ' Row 1 Match 2
                Column2(0).Image = arrAnimImages(i)
                Column3(0).Image = arrAnimImages(i)
                Column4(0).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) And Column5(0).Image Is arrImages(i) Then
                ' Row 1 Match 3
                Column3(0).Image = arrAnimImages(i)
                Column4(0).Image = arrAnimImages(i)
                Column5(0).Image = arrAnimImages(i)
                LineCounter += 1

            ElseIf Column1(1).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) Then
                ' Row 2 Match 1
                Column1(1).Image = arrAnimImages(i)
                Column2(1).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) Then
                ' Row 2 Match 2
                Column2(1).Image = arrAnimImages(i)
                Column3(1).Image = arrAnimImages(i)
                Column4(1).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And Column5(1).Image Is arrImages(i) Then
                ' Row 2 Match 3
                Column3(1).Image = arrAnimImages(i)
                Column4(1).Image = arrAnimImages(i)
                Column5(1).Image = arrAnimImages(i)
                LineCounter += 1

            ElseIf Column1(2).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) Then
                ' Row 3 Match 1
                Column1(2).Image = arrAnimImages(i)
                Column2(2).Image = arrAnimImages(i)
                Column3(2).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column2(2).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) Then
                ' Row 3 Match 2
                Column2(2).Image = arrAnimImages(i)
                Column3(2).Image = arrAnimImages(i)
                Column4(2).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) Then
                ' Row 3 Match 3
                Column3(2).Image = arrAnimImages(i)
                Column4(2).Image = arrAnimImages(i)
                Column5(2).Image = arrAnimImages(i)
                LineCounter += 1

            ElseIf Column1(3).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) And Column3(3).Image Is arrImages(i) Then
                ' Row 4 Match 1
                Column1(3).Image = arrAnimImages(i)
                Column2(3).Image = arrAnimImages(i)
                Column3(3).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column2(3).Image Is arrImages(i) And Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) Then
                ' Row 4 Match 2
                Column2(3).Image = arrAnimImages(i)
                Column3(3).Image = arrAnimImages(i)
                Column4(3).Image = arrAnimImages(i)
                LineCounter += 1
            ElseIf Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) And Column5(3).Image Is arrImages(i) Then
                ' Row 4 Match 3
                Column3(3).Image = arrAnimImages(i)
                Column4(3).Image = arrAnimImages(i)
                Column5(3).Image = arrAnimImages(i)
                LineCounter += 1
            End If
        Next
        '''''''''''''''''''''''''''''''''''''''

        If LineCounter > SelLines Then LineCounter = SelLines

        If LineCounter > 0 And LineCounter < 10 Then
            If Not MaxBet Then
                LineWinnings = LineCounter * 2 + Bet
            Else
                LineWinnings = LineCounter * 4 + (Bet * 2)
            End If
        Else
            LineCounter = 0
            LineWinnings = 0
            Winnings = 0
        End If
    End Sub
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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read