How to Create a Slot Machine Game [Visual Basic]

Language

Introduction: Welcome to a tutorial on how to create a Slot Machine type game in Visual Basic. Steps of Creation: Step 1: First we need a form. I have mine set out as the following... Textbox5 = Current Money Textbox4 = Current Bid Textbox1 = Slot 1 Textbox2 = Slot 2 Textbox3 = Slot 3 Label4 = Status Button1 = Spin Step 2: The first bit of code we want is to create our myMoney and myBid Integers and to set our textboxes appropriately on form load.
  1. Dim myMoney As Integer = 10000, myBid As Integer = 100
  2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3. TextBox5.Text = myMoney
  4. TextBox4.Text = myBid
  5. End Sub
Step 3: Now for the spin button. We first check that we have sufficient money for the entered bid, and if we do, we set the myBid value and run the slots functions.
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. If (Integer.Parse(TextBox4.Text) >= Integer.Parse(TextBox5.Text)) Then
  3. MsgBox("You can not bid more money than you have!")
  4. Else
  5. myBid = Integer.Parse(TextBox4.Text)
  6. updateSlots()
  7. checkSlots()
  8. TextBox5.Text = myMoney
  9. End If
  10. End Sub
Step 4: So we are using two custom functions but they don't exist yet, guess what's next!
  1. Private Function updateSlots()
  2. Dim rand As Random = New Random()
  3. Dim slots As New List(Of TextBox)
  4. slots.Add(TextBox1)
  5. slots.Add(TextBox2)
  6. slots.Add(TextBox3)
  7. For i As Integer = 0 To 2
  8. Dim r As Integer = rand.Next(9)
  9. slots(i).Text = r
  10. r = Nothing
  11. Next
  12. End Function
Our updateSlots function simply adds our slot textboxes to a list, creates a random integer from 0 to 9 three times and puts each value in to our three slot textboxes.
  1. Private Function checkSlots()
  2. Dim t1 As Integer = TextBox1.Text, t2 As Integer = TextBox2.Text, t3 As Integer = TextBox3.Text
  3. If ((t1 = t2) And t2 = t3) Then
  4. Label4.Text = "Triple Money!"
  5. myMoney += (myBid * 3)
  6. ElseIf ((t1 = t2) Or (t1 = t3) Or (t2 = t3)) Then
  7. Label4.Text = "Double Money!"
  8. myMoney += (myBid * 2)
  9. Else
  10. Label4.Text = "Bid Lost."
  11. myMoney -= myBid
  12. End If
  13. End Function
Finally, our checkSlots function compares the three new slot numbers and checks if they; Are all the same - Triples bid money. Have two the same - Doubles bid money. Have none the same - Loses bid money. Project Complete! That's it! Below is the full source code and download to the project files.
  1. Public Class Form1
  2. Dim myMoney As Integer = 10000, myBid As Integer = 100
  3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4. TextBox5.Text = myMoney
  5. TextBox4.Text = myBid
  6. End Sub
  7.  
  8. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  9. If (Integer.Parse(TextBox4.Text) >= Integer.Parse(TextBox5.Text)) Then
  10. MsgBox("You can not bid more money than you have!")
  11. Else
  12. myBid = Integer.Parse(TextBox4.Text)
  13. updateSlots()
  14. checkSlots()
  15. TextBox5.Text = myMoney
  16. End If
  17. End Sub
  18.  
  19. Private Function updateSlots()
  20. Dim rand As Random = New Random()
  21. Dim slots As New List(Of TextBox)
  22. slots.Add(TextBox1)
  23. slots.Add(TextBox2)
  24. slots.Add(TextBox3)
  25. For i As Integer = 0 To 2
  26. Dim r As Integer = rand.Next(9)
  27. slots(i).Text = r
  28. r = Nothing
  29. Next
  30. End Function
  31.  
  32. Private Function checkSlots()
  33. Dim t1 As Integer = TextBox1.Text, t2 As Integer = TextBox2.Text, t3 As Integer = TextBox3.Text
  34. If ((t1 = t2) And t2 = t3) Then
  35. Label4.Text = "Triple Money!"
  36. myMoney += (myBid * 3)
  37. ElseIf ((t1 = t2) Or (t1 = t3) Or (t2 = t3)) Then
  38. Label4.Text = "Double Money!"
  39. myMoney += (myBid * 2)
  40. Else
  41. Label4.Text = "Bid Lost."
  42. myMoney -= myBid
  43. End If
  44. End Function
  45. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byRAJA ABDUL MOEEZ (not verified)on Sat, 01/13/2018 - 04:19

very nice coding appreciate that

Add new comment