ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Create Rolling Numbers Game in VB.Net

In this tutorial, I will teach you how to create simple games in VB.Net. This is called the “Rolling Numbes”. The features of these are; the numbers are randomly rolled and it has a Progress Bar that serves as a timer.

Let’s begin:

Open the Visual Basic, create a new Windows Form Application and do the Form just like this.
rolingnumberform
After setting up the Form, double click the Timer to fire the Tick event handler. You have to set the rolled numbers randomly every time the timer ticks.

  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         'INITIALIZE A PSUEDO RANDOM GENERATOR.
  3.         Dim RAN As Random = New Random()
  4.         'DECLARING AN INTEGER VARIABLE TO STORE THE NONNEGATIVE RANDOM NUMBERS.
  5.         'AND SET THE NUMBERS WITHIN A SPECIFIED RANGE.
  6.         Dim RES As Integer = RAN.Next(1, 7)
  7.         'PUT THE RESULT IN THE TEXTBOX.
  8.         TextBox1.Text = RES.ToString
  9.         'INCREASE BY 1.
  10.         ProgressBar1.Value += 1
  11.         'CHECKING IF THE PROGRESSBAR VALUE REACHED TO 100
  12.         'IF IT REACHES TO 100 THE TIMER WILL STOP AND THE NUMBBER IN THE TEXTBOX WILL ALSO STOP ROLLING.
  13.         If ProgressBar1.Value = 100 Then
  14.             'IF THE CHECKBOX1 IS CHECKED THE MESSAGE WILL APPEAR IN THE LISTBOX.
  15.             If CheckBox1.CheckState = CheckState.Checked Then
  16.                 If TextBox1.Text = CheckBox1.Text Then
  17.                     'ADDING STRING MESSAGE IN THE LISTBOX.
  18.                     ListBox1.Items.Add("The result is " & TextBox1.Text & "!")
  19.                 End If
  20.             End If
  21.  
  22.             If CheckBox2.CheckState = CheckState.Checked Then
  23.                 If TextBox1.Text = CheckBox2.Text Then
  24.                     ListBox1.Items.Add("The result is " & TextBox1.Text & "!")
  25.                 End If
  26.             End If
  27.             If CheckBox3.CheckState = CheckState.Checked Then
  28.                 If TextBox1.Text = CheckBox3.Text Then
  29.                     ListBox1.Items.Add("The result is " & TextBox1.Text &  "!")
  30.                 End If
  31.             End If
  32.  
  33.             If CheckBox4.CheckState = CheckState.Checked Then
  34.                 If TextBox1.Text = CheckBox4.Text Then
  35.                     ListBox1.Items.Add("The result is " & TextBox1.Text & "!")
  36.                 End If
  37.             End If
  38.  
  39.             If CheckBox5.CheckState = CheckState.Checked Then
  40.                 If TextBox1.Text = CheckBox5.Text Then
  41.                     ListBox1.Items.Add("The result is " & TextBox1.Text & "!")
  42.                 End If
  43.             End If
  44.  
  45.             If CheckBox6.CheckState = CheckState.Checked Then
  46.                 If TextBox1.Text = CheckBox6.Text Then
  47.                     ListBox1.Items.Add("The result is " & TextBox1.Text &  "!")
  48.                 End If
  49.             End If
  50.  
  51.             If CheckBox7.CheckState = CheckState.Checked Then
  52.                 If TextBox1.Text = CheckBox7.Text Then
  53.                     ListBox1.Items.Add("The result is " & TextBox1.Text & "!")
  54.                 End If
  55.             End If
  56.             Timer1.Stop()
  57.         End If
  58.  
  59.     End Sub

After setting up the rolled number, go back to the Form Design and double click the Button. You have to start the timer and assign the Progress Bar into its minimum value in the Sub Procedure.

  1. Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
  2.         'CLEARING THE LISTBOX
  3.         ListBox1.Items.Clear()
  4.         'START THE TIMER
  5.         Timer1.Start()
  6.         'THE VALUE OF THE PROGRESSBAR WILL RETURN TO THE DEFAULT EVERY CLICK THE BUTTON
  7.         ProgressBar1.Value = 0
  8.     End Sub

The complete source code is included and you can download it.

Download here.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!