How to Create a Ninja Defuse [Minigame] in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 1375 times.

Introduction:
Welcome to a tutorial on a small game to be made in Visual Basic - Ninja Defuse. A game to guess a randomly generated three digit code before the time runs out.

Steps of Creation:
Step 1:

First we need to create our form with...
- Textbox1 - First digit of guessed code
- Textbox2 - Second
- Textbox3 - Third
- Label1 - Information of time
- Label2 - Time remaining
- Button1 - Begin game/countdown

Ensure that the maximum character limit for all the textboxes are 1.

Step 2:
Lets create our variables and threads which we will be using...

  1. Dim trd As Threading.Thread = New Threading.Thread(AddressOf timeDown)
  2. Dim trd2 As Threading.Thread = New Threading.Thread(AddressOf checkKey)
  3. Dim key As String = "111"
  4. Dim seconds As Integer = 60
  5. Dim ran As Boolean = False
  6. Private Function isNumber(ByVal s As String)
  7. If (s = "0" Or s = "1" Or s = "2" Or s = "3" Or s = "4" Or s = "5" Or s = "6" Or s = "7" Or s = "8" Or s = "9") Then
  8. Return True
  9. End If
  10. Return False
  11. End Function

Our function will also be used later which will ensure any text entered in to our three, single digit textboxes are digits instead of symbols or letters.

Step 3:
Now we want to ensure that any text entered in to our digit boxes are numbers (using our custom functions)...

  1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
  2. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  3. TextBox1.Text = ""
  4. End If
  5. End Sub
  6.  
  7. Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
  8. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  9. TextBox2.Text = ""
  10. End If
  11. End Sub
  12.  
  13. Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
  14. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  15. TextBox3.Text = ""
  16. End If
  17. End Sub

Step 4:
In order to use our custom threads to interact with the time label we want to make illegalcrossthreadcalls unchecked, so on form1 add this:

  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. CheckForIllegalCrossThreadCalls = False
  3. TextBox1.Enabled = False
  4. TextBox2.Enabled = False
  5. TextBox3.Enabled = False
  6. End Sub

Step 5:
For the begin button we want to generate our randomised key, ensure our textboxes are enabled and initiate our threads...

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. If (Not ran) Then
  3. ran = True
  4. Dim rand As Random = New Random
  5. key = ""
  6. For i As Integer = 0 To 2
  7. Dim r As Integer = rand.Next(9)
  8. key &= r
  9. r = Nothing
  10. Next
  11. 'MsgBox(key)' Uncomment to see the key for testing.
  12. TextBox1.Enabled = True
  13. TextBox2.Enabled = True
  14. TextBox3.Enabled = True
  15. trd.IsBackground = True
  16. trd.Start()
  17. trd2.IsBackground = True
  18. trd2.Start()
  19. End If
  20. End Sub

Step 6:
Finally lets add our two more custom functions courtesy of our threads which will; Countdown the time remaining, and check the entered key...

  1. Private Function checkKey()
  2. Do Until seconds = 0
  3. Dim s As String = TextBox1.Text & TextBox2.Text & TextBox3.Text
  4. If (s = key) Then
  5. MsgBox("You found the key! You Ninja.")
  6. If (trd.IsAlive) Then trd.Suspend()
  7. If (trd2.IsAlive) Then trd2.Suspend()
  8. TextBox1.Enabled = False
  9. TextBox2.Enabled = False
  10. TextBox3.Enabled = False
  11. seconds = 0
  12. End If
  13. Loop
  14. seconds = 60
  15. End Function
  16.  
  17. Private Function timeDown()
  18. Do Until seconds = 0
  19. seconds -= 1
  20. Label2.Text = seconds
  21. Threading.Thread.Sleep(1000)
  22. Loop
  23. TextBox1.Enabled = False
  24. TextBox2.Enabled = False
  25. TextBox3.Enabled = False
  26. End Function

Project Complete!
Thank you for reading my tutorial. Below is the full source code and download for the project files.

  1. Public Class Form1
  2. Dim trd As Threading.Thread = New Threading.Thread(AddressOf timeDown)
  3. Dim trd2 As Threading.Thread = New Threading.Thread(AddressOf checkKey)
  4. Dim key As String = "111"
  5. Dim seconds As Integer = 60
  6. Dim ran As Boolean = False
  7. Private Function isNumber(ByVal s As String)
  8. If (s = "0" Or s = "1" Or s = "2" Or s = "3" Or s = "4" Or s = "5" Or s = "6" Or s = "7" Or s = "8" Or s = "9") Then
  9. Return True
  10. End If
  11. Return False
  12. End Function
  13. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
  14. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  15. TextBox1.Text = ""
  16. End If
  17. End Sub
  18.  
  19. Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
  20. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  21. TextBox2.Text = ""
  22. End If
  23. End Sub
  24.  
  25. Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
  26. If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
  27. TextBox3.Text = ""
  28. End If
  29. End Sub
  30.  
  31. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  32. CheckForIllegalCrossThreadCalls = False
  33. TextBox1.Enabled = False
  34. TextBox2.Enabled = False
  35. TextBox3.Enabled = False
  36. End Sub
  37.  
  38. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  39. If (Not ran) Then
  40. ran = True
  41. Dim rand As Random = New Random
  42. key = ""
  43. For i As Integer = 0 To 2
  44. Dim r As Integer = rand.Next(9)
  45. key &= r
  46. r = Nothing
  47. Next
  48. 'MsgBox(key)' Uncomment to see the key for testing.
  49. TextBox1.Enabled = True
  50. TextBox2.Enabled = True
  51. TextBox3.Enabled = True
  52. trd.IsBackground = True
  53. trd.Start()
  54. trd2.IsBackground = True
  55. trd2.Start()
  56. End If
  57. End Sub
  58.  
  59. Private Function checkKey()
  60. Do Until seconds = 0
  61. Dim s As String = TextBox1.Text & TextBox2.Text & TextBox3.Text
  62. If (s = key) Then
  63. MsgBox("You found the key! You Ninja.")
  64. If (trd.IsAlive) Then trd.Suspend()
  65. If (trd2.IsAlive) Then trd2.Suspend()
  66. TextBox1.Enabled = False
  67. TextBox2.Enabled = False
  68. TextBox3.Enabled = False
  69. seconds = 0
  70. End If
  71. Loop
  72. seconds = 60
  73. End Function
  74.  
  75. Private Function timeDown()
  76. Do Until seconds = 0
  77. seconds -= 1
  78. Label2.Text = seconds
  79. Threading.Thread.Sleep(1000)
  80. Loop
  81. TextBox1.Enabled = False
  82. TextBox2.Enabled = False
  83. TextBox3.Enabled = False
  84. End Function
  85. 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.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.