# Creating a Video Slot game with VB.NET Part 2 - Gameplay

Thursday Oct 4th 2012 by Hannes Du Preez
Share:

This installment of Creating a Video Slot game with VB.NET shows you how to determine whether or not three pictures form part of a winning line, how to enable the facility to enter an amount of credits, and how to track them during gameplay.

## Introduction

Welcome back! With this installment we will mainly concentrate in determining whether or not three pictures form part of a winning line. We will also enable the facility to enter an amount of credits, and track them during gameplay.

## How Does it Work?

A line is basically the row in which the matching pictures were found. It doesn't have to be a straight line, it can be any variant. If you take 5 columns with 4 pictures each, you can have many possibilities, as explained in the first installment of this series. We need to determine which pictures are next to each other. Let us start with the 'complicated' stuff first... for a change!

## Finding Matches

Create the following variables inside General Declarations :

```    Private LineCounter As Integer 'Count winning lines ( rows )
Private SelLines As Integer 'How many lines were selected
Private MainLineCounter As Integer 'Count winning MAIN Lines - which can be skew, straight, zig-zag etc.
Private Credits As Integer 'How many credits
Private LineBet As Integer 'How much to bet per line
Private Bet As Integer 'Total bet
Private strBetSummary As String 'summarise lines + bet per line
Private LineBetCounter As Integer = 0 'Limit bet per line```

Now add the following sub procedure :

```    Private Sub CheckMainLines() 'Determine winning sequence for MAIN lines

For i As Integer = 0 To 9 'loop through all images in arrImages array
'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 'Check if neighbouring pictures are the same
MessageBox.Show("Line 1 Match") 'Match - More matches are possible, but that will be done in Part 3
MainLineCounter += 1 'Increment MainLine counter - to indicate how many main lines have been struck
'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
MessageBox.Show("Line 2 Match")
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
MessageBox.Show("Line 3 Match")
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
MessageBox.Show("Line 4 Match")
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
MessageBox.Show("Line 5 Match")
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
MessageBox.Show("Line 6 Match")
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
MessageBox.Show("Line 7 Match")
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
MessageBox.Show("Line 8 Match")
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
MessageBox.Show("Line 9 Match")
MainLineCounter += 1

End If
Next

End Sub
```

There are hundreds of possible matches with our screen of twenty pictures, remember that. Now, with this sub, I am checking if all the matches for one full line have been found. In other words, I check to see if the pictures displayed correspond precisely with my built in lines. How? Let us take a look at how the columns and rows are formed. The pictures are organized according to Figure 1

Figure 1 - How Pictures are organized

Obviously arrays start counting at 0, so technically our pictures start at 0 and go on to 19. The lines look as follows :

Figure 2 - How our lines are organized

As you can see, the first four Main lines are straightforward in the fact that all of them have the same index in each different PictureBox array ( Column1 - Column 5 ). From line 5 onwards, I tried to create different patterns for the lines to follow. In real Video slot games, there are many more combinations, but this is just an example of how the logic works.

This sub deals with the Main lines. I call them Main lines, because they are worth the most. A full line needs to be identified in order for the line to win. In the sub I compare the pictures with a little keyword called Is. Remember in Part 1 I declared arrImages as Image? Well, it is because of this. It makes it easier to compare pictures this way, and you get the correct result each time; instead of using pixel detection and so forth, which may give unexpected results. We will work more with the Main Lines in Part 3 again. When a match is found, I increment the MainLineCounter variable to keep track of how many lines were struck.

The next two subs use the same principle. I just take it a step further to detect all possible matches. Add the CheckCols and CheckRows subs :

```    Private Sub CheckCols() 'Determine winning sequence for columns

For i As Integer = 0 To 9 'Loop through arrImages
If Column1(0).Image Is arrImages(i) And Column1(1).Image Is arrImages(i) _
And Column1(2).Image Is arrImages(i) Then 'Determine three matching neighbouring images
MessageBox.Show("column 1 Match 1") 'Match - Two matches are possible
'No Column counter yet, just concetrating on the lines Now - Completed with Part 3
ElseIf Column1(1).Image Is arrImages(i) And Column1(2).Image Is arrImages(i) _
And Column1(3).Image Is arrImages(i) Then
MessageBox.Show("column 1 Match 2")

ElseIf Column2(0).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) _
And Column2(2).Image Is arrImages(i) Then
MessageBox.Show("column 2 Match 1")

ElseIf Column2(1).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) _
And Column2(3).Image Is arrImages(i) Then
MessageBox.Show("column 2 Match 2")

ElseIf Column3(0).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) _
And Column3(2).Image Is arrImages(i) Then
MessageBox.Show("column 3 Match 1")

ElseIf Column3(1).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) _
And Column3(3).Image Is arrImages(i) Then
MessageBox.Show("column 3 Match 2")

ElseIf Column4(0).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) _
And Column4(2).Image Is arrImages(i) Then
MessageBox.Show("column 4 Match 1")

ElseIf Column4(1).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) _
And Column4(3).Image Is arrImages(i) Then
MessageBox.Show("column 4 Match 2")

ElseIf Column5(0).Image Is arrImages(i) And Column5(1).Image Is arrImages(i) _
And Column5(2).Image Is arrImages(i) Then
MessageBox.Show("column 5 Match 1")

ElseIf Column5(1).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) _
And Column5(3).Image Is arrImages(i) Then
MessageBox.Show("column 5 Match 2")

End If
Next

End Sub

Private Sub CheckRows() 'Determine winning sequence for lines ( straight rows )
For i As Integer = 0 To 9 'Loop arrImages

If Column1(0).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And _
Column3(0).Image Is arrImages(i) Then 'Determine similarity between neighbouring pictures
MessageBox.Show("row 1 Match 1") 'Three matches are possible
LineCounter += 1 ' Count each match distinctly
ElseIf Column2(0).Image Is arrImages(i) And Column3(0).Image Is arrImages(i) _
And Column4(0).Image Is arrImages(i) Then
MessageBox.Show("row 1 Match 2")
LineCounter += 1
ElseIf Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) _
And Column5(0).Image Is arrImages(i) Then
MessageBox.Show("row 1 Match 3")
LineCounter += 1
ElseIf Column1(1).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) _
And Column3(1).Image Is arrImages(i) Then
MessageBox.Show("row 2 Match 1")
LineCounter += 1
ElseIf Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) _
And Column4(1).Image Is arrImages(i) Then
MessageBox.Show("row 2 Match 2")
LineCounter += 1
ElseIf Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) _
And Column5(1).Image Is arrImages(i) Then
MessageBox.Show("row 2 Match 3")
LineCounter += 1
ElseIf Column1(2).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) _
And Column3(2).Image Is arrImages(i) Then
MessageBox.Show("row 3 Match 1")
LineCounter += 1
ElseIf Column2(2).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) _
And Column4(2).Image Is arrImages(i) Then
MessageBox.Show("row 3 Match 2")
LineCounter += 1
ElseIf Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) _
And Column5(2).Image Is arrImages(i) Then
MessageBox.Show("row 3 Match 3")
LineCounter += 1
ElseIf Column1(3).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) _
And Column3(3).Image Is arrImages(i) Then
MessageBox.Show("row 4 Match 1")
LineCounter += 1
ElseIf Column2(3).Image Is arrImages(i) And Column3(3).Image Is arrImages(i) _
And Column4(3).Image Is arrImages(i) Then
MessageBox.Show("row 4 Match 2")
LineCounter += 1
ElseIf Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) _
And Column5(3).Image Is arrImages(i) Then
MessageBox.Show("row 4 Match 3")
LineCounter += 1
End If
Next

End Sub
```

We are now able to detect all matches. When a macth is found, a MessageBox will show, and the associated counter will increment. In Part 3, the MessageBoxes will be removed. Add the call to these three subs to tmrPause_Tick, which will now look like :

```    Private Sub tmrPause_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPause.Tick
''''''''''''''''''''''
''Part 1
If Col <= 5 Then
Col += 1
TimeCounter = 0
tmrPause.Enabled = False
tmrSlots.Enabled = True
Else
Col = 1
TimeCounter = 0
tmrPause.Enabled = False
tmrSlots.Enabled = False
picSpin.Enabled = True

Panel5.BackColor = Color.White
picSpin.Image = My.Resources.SPIN
'''''''''''''''''''''''''''''

CheckMainLines() 'Determine all matches
CheckCols()
CheckRows()

End If

End Sub```

## Design

In order to proceed, we need to enhance our design to compensate for the betting process, line selection, and credit information. Add the following objects to your form.

ControlPropertySetting
Label Location 554, 26
Size 39, 13
Text Credits
Label Name lblCredits
BorderStyle FixedSingle
Location 557, 48
Text "" ( Empty )
Label Location 554, 72
Size 23, 13
Text Bet
Label Name lblBet
BorderStyle FixedSingle
Location 557, 85
PictureBox Name pic1Line
Image HTG_Slots.My.Resources.Resources._1_Line ( Or your own )
Location 24, 324
Size 100, 50
PictureBox Name pic3Lines
Image HTG_Slots.My.Resources.Resources._3_Lines ( Or your own )
Location 130, 324
Size 100, 50
PictureBox Name pic5Lines
Image HTG_Slots.My.Resources.Resources._5_Lines ( Or your own )
Location 236, 324
Size 100, 50
PictureBox Name pic7Lines
Image HTG_Slots.My.Resources.Resources._7_Lines ( Or your own )
Location 342, 324
Size 100, 50
PictureBox Name pic9Lines
Image HTG_Slots.My.Resources.Resources._9_Lines ( Or your own )
Location 448, 324
Size 100, 50
PictureBox Name picLineSummary
Location 24, 414
Size 372, 50
PictureBox Name picLineBet
Image HTG_Slots.My.Resources.Resources.Line_Bet ( Or your own )
Location 406, 414
Size 100, 50
PictureBox Name picMaxBet
Image HTG_Slots.My.Resources.Resources.Max_Bet ( Or your own )
Location 406, 470
Size 100, 50

## Credits

Add the next small piece of code to Form_Load, above the arrImages' initialization :

```        Credits = InputBox("Please enter Credits") 'Enter credits to play with
lblCredits.Text = Credits 'Display total credits```

This allows us to enter the number of credits we want to play with. usually with these types of games, we enter the amount we have, then specify the credit values, for example 0.01 will be one cent per credit and 0.1 will be 10cents per credit.

## Setting the Lines

Add the next code blocks :

```    Private Sub pic1Line_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic1Line.Click
SelLines = 1 '1 line
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString() 'Update strBetSummary
BetInfo() 'Write this info

pic1Line.Image = My.Resources._1_Line_s 'Change picture
End Sub

Private Sub pic3Lines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic3Lines.Click
SelLines = 3 '3 lines
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString()
BetInfo()
pic3Lines.Image = My.Resources._3_Lines_s
End Sub

Private Sub pic5Lines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic5Lines.Click
SelLines = 5 '5 lines
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString()
BetInfo()
pic5Lines.Image = My.Resources._5_Lines_s
End Sub

Private Sub pic7Lines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic7Lines.Click
SelLines = 7 '7 lines
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString()
BetInfo()
pic7Lines.Image = My.Resources._7_Lines_s
End Sub

Private Sub pic9Lines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic9Lines.Click
SelLines = 9 '9 lines
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString()
BetInfo()
pic9Lines.Image = My.Resources._9_Lines_s
End Sub
```

Once a number of lines have been chosen, it will set the SelLines variable to the appropriate value, and build the strBetSummary string to be drawn with the BetInfo sub. Then, we just indicate which option was chosen. There is still some work here, but we'll polish this off in Part 3. Add the BetInfo sub :

```    Private Sub BetInfo() 'Displays current bet info

Dim brHTGSlots As Brush = Brushes.ForestGreen 'Font colour

Dim fntHTGSlots As New Font("Comic Sans MS", 12, FontStyle.Bold) 'Font style

Dim x_Location, y_Location As Single 'Location to write text

x_Location = 10
y_Location = 20

picLineSummary.Refresh() 'Clear previous text

picLineSummary.CreateGraphics().DrawString(strBetSummary, fntHTGSlots, brHTGSlots, _
x_Location, _
y_Location) 'Write info

End Sub```

The above sub simply draws how many lines were chosen, as well as how much was bet on those lines, on screen.

## Finishing Off

Let us finish off the betting. Add these two procedures :

```    Private Sub picLineBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picLineBet.Click

If LineBetCounter < 5 Then 'Limit bet per line to 5
LineBetCounter += 1 'Increment on each click
Bet = LineBetCounter * SelLines 'Calculate total bet
lblBet.Text = Bet 'Display bet
strBetSummary = SelLines.ToString & " Line(s) for " & LineBetCounter.ToString() 'Update strBetSummary
BetInfo()
Else
LineBetCounter = 0 'Start over if more than 5

End If

End Sub

Private Sub picMaxBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picMaxBet.Click
Bet = 100 'Maximum bet is 100
lblBet.Text = Bet
strBetSummary = SelLines.ToString & " Line(s) for " & Bet.ToString() 'Update info
BetInfo()
End Sub```

This where the bet per line, and maximum bets are done. We can only bet 5 per line, whereas the maximum bet is 100 flat.

Last but not least, we need to edit the Spin button's code to compensate for the credits, and to reset all the counters properly :

```    Private Sub picSpin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picSpin.Click

''''''''''''''''''''''''''''
''Part 1
RandGen = New Random(Now.Millisecond)

tmrSlots.Enabled = True
picSpin.Enabled = False
picSpin.Image = My.Resources.SPIN2
'''''''''''''''''''''''''''''''

LineCounter = 0 'Reset to 0
MainLineCounter = 0 'Reset to 0

Credits -= Bet 'Decrement credits accodring to bet total
lblCredits.Text = Credits 'Display balance
End Sub```

There is still a lot of polishing in this project. I do not know why I forgot to add a counter for the columns, but luckily that is done in Part 3. Figure 3 shows our program in action :

Figure 3 - Our program in action

## Conclusion

Thanks for reading, I hope you have enjoyed this article. Stick around for Part 3, which will cover the winnings, as well as polishing off everything nicely - until then, cheers!

Share:
Home
Mobile Site | Full Site