Random Rolling Numbers Game in C#

In this tutorial, we will create a game called Random Rolling Game using C# that is rolled randomly with its number and has a Progress Bar that serves as a timer. 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Design your interface like this: design 3. Create this code for your Timer1_Tick to be able to randomly roll the numbers in textbox. Put this code below:
  1. private void Timer1_Tick(System.Object sender, System.EventArgs e)
  2. {
  3.        
  4.         //declaring a random variable to store the numbers on it.
  5.         //it represents a psuedo random generator.
  6.         Random num = new Random();
  7.         //declaring an integer variable to store the nonnegative random numbers.
  8.         //starts with the minimum value to the maximum value
  9.         //next represents to return a random number within a specified range.
  10.         int result = System.Convert.ToInt32(num.Next(1, 7));
  11.         //put the result in the textbox.
  12.         TextBox1.Text = result.ToString();
  13.         //progressbar increased by 1.
  14.         ProgressBar1.Value += 1;
  15.         //the condition is, when the progressbar value reached to 100
  16.         //then the timer will stop and the number in the textbox will stop rolling too.
  17.         if (ProgressBar1.Value == 100)
  18.         {
  19.                 //filtering the checkbox.
  20.                 //if the checkbox1 is checked the message will appear in the listbox.
  21.                 if (CheckBox1.CheckState == CheckState.Checked)
  22.                 {
  23.                         if (TextBox1.Text == CheckBox1.Text)
  24.                         {
  25.                                 //add the string message in the listbox.
  26.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  27.                         }
  28.                 }
  29.                
  30.                 if (CheckBox2.CheckState == CheckState.Checked)
  31.                 {
  32.                         if (TextBox1.Text == CheckBox2.Text)
  33.                         {
  34.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  35.                         }
  36.                 }
  37.                 if (CheckBox3.CheckState == CheckState.Checked)
  38.                 {
  39.                         if (TextBox1.Text == CheckBox3.Text)
  40.                         {
  41.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  42.                         }
  43.                 }
  44.                
  45.                 if (CheckBox4.CheckState == CheckState.Checked)
  46.                 {
  47.                         if (TextBox1.Text == CheckBox4.Text)
  48.                         {
  49.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  50.                         }
  51.                 }
  52.                
  53.                 if (CheckBox5.CheckState == CheckState.Checked)
  54.                 {
  55.                         if (TextBox1.Text == CheckBox5.Text)
  56.                         {
  57.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  58.                         }
  59.                 }
  60.                
  61.                 if (CheckBox6.CheckState == CheckState.Checked)
  62.                 {
  63.                         if (TextBox1.Text == CheckBox6.Text)
  64.                         {
  65.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  66.                         }
  67.                 }
  68.                
  69.                 if (CheckBox7.CheckState == CheckState.Checked)
  70.                 {
  71.                         if (TextBox1.Text == CheckBox7.Text)
  72.                         {
  73.                                 ListBox1.Items.Add("The number " + TextBox1.Text + " won!");
  74.                         }
  75.                 }
  76.                 Timer1.Stop();
  77.         }
  78.        
  79. }
4. In your Button put this code below to start the timer and assigning the Progress Bar of a minimum value.
  1. private void btnRoll_Click(System.Object sender, System.EventArgs e)
  2. {
  3.         //clearing the listbox
  4.         ListBox1.Items.Clear();
  5.         //timer starts
  6.         Timer1.Start();
  7.         //progressbar value returns to 0 everytime you click the button
  8.         ProgressBar1.Value = 0;
  9. }

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment