How to make a Number Guesser Game!

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts Page 1 of 1
Contributors
User avatar
dradra43
New Member
New Member
Posts: 13
Joined: Sat Nov 06, 2010 9:56 pm

How to make a Number Guesser Game!
dradra43
Level = ★★★★☆
Rated ★★★★☆, user must: Have must around with Visual Basic 2010, and you know how to code on your own.
Tutorial time!

Ok, today I will show everyone how to make a simple number guessing game!
You will need:
  • 3 labels
    2 buttons
    1 group box
    3 radiobuttons
    1 textbox
Here is a picture of what it should/could look like:
Image

Purple = TextBox | Name = Textbox

Red = Labels | Label1.Name = LabelNumGuess | Label2.Name = LabelHighLow | Label3.Name = Label3

Blue = RadioButtons | RadioButton1.Name = RadioButton10 | RadioButton2.Name = RadioButton100 | RadioButton3.Name = RadioButton1000

Black = Buttons | keep the names

Ok, now time for the text!
Radio Buttons
  • RadioButton10.Text = "Game 1 - 10"
    RadioButton100.Text = "Game 1 - 100"
    RadioButton1000.Text = "Game 1 - 1000"
Buttons:
  • Button1.Text = "Submit"
    Button2.Text = "Start Game"
Labels:
  • LabelHighLow.Text = "Higher or Lower"
    LabelNumGuess.Text = "Number Of Guesses: *Yea there is a space!!*"
    Label3.Text = "Enter a number:"
Groupbox (Name is GroubBox1):
  • GroupBox1.Text = "Select A Game!"
Now, look at this picture, and move everything around acordingly:
Image
On the right:
  • The radiobuttons (INSIDE the group box)
    Button2 (the start game button)
On the left:
  • Labels
    TextBox
    Button1
Now, I am going into the coding, just to save me and you time, just copy+paste this code:
Code: Select all
Public Class Form1
    Dim ComputerNumber As Integer
    Dim Count As Integer
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Count = 0
        GroupBox1.Enabled = False
        Button2.Enabled = False
        Button1.Enabled = True
        TextBoxGuess.Enabled = True
        LabelNumGuess.Text = "Number Of Guesses: " + Str(Count)
        LabelHighLow.Text = ""
        TextBoxGuess.Focus()
        TextBoxGuess.Clear()
        GetNumber()
    End Sub
    Private Sub GetNumber()
        Randomize()
        If RadioButton10.Checked = True Then
            ComputerNumber = Rnd() * 9 + 1
        ElseIf RadioButton100.Checked = True Then
            ComputerNumber = Rnd() * 99 + 1
        ElseIf RadioButton1000.Checked = True Then
            ComputerNumber = Rnd() * 999 + 1
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBoxGuess.Text <> "" Then
            Try
                If Integer.Parse(TextBoxGuess.Text) = ComputerNumber Then
                    CorrectGuess()
                Else
                    NotCorrectGuess()
                End If
            Catch ex As Exception
                TextBoxGuess.Clear()
                TextBoxGuess.Focus()
                MsgBox(ex.Message)
            End Try
        End If
    End Sub
    Private Sub CorrectGuess()
        Count += 1
        LabelNumGuess.Text = "Number Of Guesses: " + Str(Count)
        GroupBox1.Enabled = True
        Button2.Enabled = True
        Button1.Enabled = False
        TextBoxGuess.Enabled = False
        MsgBox("You guessed my number! You guessed it in: " + Str(Count) + " guesses! YAY!!", MsgBoxStyle.Exclamation, "Congratulations!")
    End Sub
    Private Sub NotCorrectGuess()
        Count += 1
        LabelNumGuess.Text = "Number Of Guesses: " + Str(Count)
        If Integer.Parse(TextBoxGuess.Text) < ComputerNumber Then
            LabelHighLow.Text = "Higher!"
        Else
            LabelHighLow.Text = "Lower!"
        End If
        TextBoxGuess.Clear()
        TextBoxGuess.Focus()
    End Sub
End Class
If you have problems PM me, or just leave a comment, and I or another coder will help you out! Have fun coders
Image
Image
Image
~VB CODER~
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Nice tutorial!!! cooll; :)
Might help some beginners!!
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
dradra43
New Member
New Member
Posts: 13
Joined: Sat Nov 06, 2010 9:56 pm

CoderBoy1201 wrote:
Nice tutorial!!! cooll; :)
Might help some beginners!!
Thank you! I appreciate the nice comments
Image
Image
Image
~VB CODER~
User avatar
RishiRox
New Member
New Member
Posts: 16
Joined: Mon Jan 17, 2011 4:05 pm

showing 28 errors :cry: bhobhobho

cant solve dem
4 posts Page 1 of 1
Return to “Tutorials”