How to Create a Ninja Defuse [Minigame] in Visual Basic
Submitted by Yorkiebar on Saturday, September 21, 2013 - 06:16.
Language
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...
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)...
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:
Step 5:
For the begin button we want to generate our randomised key, ensure our textboxes are enabled and initiate our threads...
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...
Project Complete!
Thank you for reading my tutorial. Below is the full source code and download for the project files.
- Dim trd As Threading.Thread = New Threading.Thread(AddressOf timeDown)
- Dim trd2 As Threading.Thread = New Threading.Thread(AddressOf checkKey)
- Dim key As String = "111"
- Dim seconds As Integer = 60
- Dim ran As Boolean = False
- Private Function isNumber(ByVal s As String)
- 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
- Return True
- End If
- Return False
- End Function
- Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox1.Text = ""
- End If
- End Sub
- Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox2.Text = ""
- End If
- End Sub
- Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox3.Text = ""
- End If
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not ran) Then
- ran = True
- Dim rand As Random = New Random
- key = ""
- For i As Integer = 0 To 2
- Dim r As Integer = rand.Next(9)
- key &= r
- r = Nothing
- Next
- 'MsgBox(key)' Uncomment to see the key for testing.
- TextBox1.Enabled = True
- TextBox2.Enabled = True
- TextBox3.Enabled = True
- trd.IsBackground = True
- trd.Start()
- trd2.IsBackground = True
- trd2.Start()
- End If
- End Sub
- Private Function checkKey()
- Do Until seconds = 0
- Dim s As String = TextBox1.Text & TextBox2.Text & TextBox3.Text
- If (s = key) Then
- MsgBox("You found the key! You Ninja.")
- If (trd.IsAlive) Then trd.Suspend()
- If (trd2.IsAlive) Then trd2.Suspend()
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- seconds = 0
- End If
- Loop
- seconds = 60
- End Function
- Private Function timeDown()
- Do Until seconds = 0
- seconds -= 1
- Label2.Text = seconds
- Threading.Thread.Sleep(1000)
- Loop
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- End Function
- Public Class Form1
- Dim trd As Threading.Thread = New Threading.Thread(AddressOf timeDown)
- Dim trd2 As Threading.Thread = New Threading.Thread(AddressOf checkKey)
- Dim key As String = "111"
- Dim seconds As Integer = 60
- Dim ran As Boolean = False
- Private Function isNumber(ByVal s As String)
- 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
- Return True
- End If
- Return False
- End Function
- Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox1.Text = ""
- End If
- End Sub
- Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox2.Text = ""
- End If
- End Sub
- Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
- If Not (isNumber(sender.ToString().Chars(sender.ToString().Length - 1))) Then
- TextBox3.Text = ""
- End If
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not ran) Then
- ran = True
- Dim rand As Random = New Random
- key = ""
- For i As Integer = 0 To 2
- Dim r As Integer = rand.Next(9)
- key &= r
- r = Nothing
- Next
- 'MsgBox(key)' Uncomment to see the key for testing.
- TextBox1.Enabled = True
- TextBox2.Enabled = True
- TextBox3.Enabled = True
- trd.IsBackground = True
- trd.Start()
- trd2.IsBackground = True
- trd2.Start()
- End If
- End Sub
- Private Function checkKey()
- Do Until seconds = 0
- Dim s As String = TextBox1.Text & TextBox2.Text & TextBox3.Text
- If (s = key) Then
- MsgBox("You found the key! You Ninja.")
- If (trd.IsAlive) Then trd.Suspend()
- If (trd2.IsAlive) Then trd2.Suspend()
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- seconds = 0
- End If
- Loop
- seconds = 60
- End Function
- Private Function timeDown()
- Do Until seconds = 0
- seconds -= 1
- Label2.Text = seconds
- Threading.Thread.Sleep(1000)
- Loop
- TextBox1.Enabled = False
- TextBox2.Enabled = False
- TextBox3.Enabled = False
- End Function
- 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
- 76 views