Results 1 to 1 of 1

Thread: Pick Me! - a guessing game

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Lightbulb Pick Me! - a guessing game

    I am new to the vb.net environment, this was my first attempt at programing with the vb2008 ide.
    Click the button below the image you think the ball is under. Records right and wrong guesses and prints a percent correct in a textbox.
    Zipped entry includes images for the cup animation and other resources. Program code includes a variable scope example and a short and easy way to pause events by using the pause(1000) subroutine, one second for each 1000 passed to it. Using the rnd function example is also in the code. Below is the code for the program.


    Code:
    'Imports System
    
    Public Class mainFRM
        
        'These variables can be used by any sub in the form class.
        Dim guessTL As Integer = 0
        Dim correctTL As Integer = 0
    
        Private Sub mainFRM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lblwoot.Text = "Click the button you think the ball is under."
        End Sub
    
        Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
            Application.Exit()
        End Sub
    
        Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 3.
            Dim rndnum As Integer = CInt(Int((3 * Rnd()) + 1))
            If rndnum = 1 Then
                correctTL += 1
                updatepercent()
                txtcorrect.Text = correctTL
                PBox1.Image = My.Resources.bgupball
                PBox1.Update()
                picktext()
                pause(1000)
                PBox1.Image = My.Resources.bg1
            Else
                guessTL += 1
                updatepercent()
                txtguesses.Text = guessTL
                PBox1.Image = My.Resources.bgup
                PBox1.Update()
                pause(2000)
                PBox1.Image = My.Resources.bg1
            End If
        End Sub
    
        Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 3.
            Dim rndnum As Integer = CInt(Int((3 * Rnd()) + 1))
            If rndnum = 2 Then
                correctTL += 1
                updatepercent()
                txtcorrect.Text = correctTL
                PBox2.Image = My.Resources.bgupball
    
                'had to update the control so the picture would change
                PBox2.Update()                                  
                picktext()
                pause(1000)
                PBox2.Image = My.Resources.bg1
            Else
                guessTL += 1
                updatepercent()
                txtguesses.Text = guessTL
                PBox2.Image = My.Resources.bgup
                PBox2.Update()
                pause(2000)
                PBox2.Image = My.Resources.bg1
            End If
        End Sub
    
        Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 3.
            Dim rndnum As Integer = CInt(Int((3 * Rnd()) + 1))
            If rndnum = 3 Then
                correctTL += 1
                updatepercent()
                txtcorrect.Text = correctTL
                PBox3.Image = My.Resources.bgupball
                PBox3.Update()
                picktext()
                pause(1000)
                PBox3.Image = My.Resources.bg1
            Else
                guessTL += 1
                updatepercent()
                txtguesses.Text = guessTL
                PBox3.Image = My.Resources.bgup
                PBox3.Update()
                pause(2000)
                PBox3.Image = My.Resources.bg1
            End If
        End Sub
    
        Private Sub btnnewgame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewgame.Click
            correctTL = 0
            guessTL = 0
            txtguesses.Text = 0
            txtcorrect.Text = 0
            txtpercent.Text = 0
            lblwoot.ForeColor = Color.Red
            lblwoot.Text = "Resetting the game!"
            MsgBox("Game is reset.")
            lblwoot.Text = ""
            lblwoot.ForeColor = Color.Black
        End Sub
    
        Private Sub pause(ByVal howlong As Integer)
            System.Threading.Thread.Sleep(howlong)
        End Sub
    
        Private Sub updatepercent()
            Dim totalguesses As Integer
            Dim percent As Double
            totalguesses = correctTL + guessTL
            percent = Int((correctTL / totalguesses) * 100)
            txtpercent.Text = percent & "%"
        End Sub
    
        Private Sub picktext()
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 4.
            Dim rndnum As Integer = CInt(Int((4 * Rnd()) + 1))
            Select Case rndnum
                Case (1)
                    lblwoot.ForeColor = Color.LimeGreen
                    lblwoot.Text = "Way to go!!!!"
                    lblwoot.Update()
                    pause(1000)
                    lblwoot.Text = ""
                    lblwoot.ForeColor = Color.Black
                Case (2)
                    lblwoot.ForeColor = Color.Firebrick
                    lblwoot.Text = "Awsome!!!!"
                    lblwoot.Update()
                    pause(1000)
                    lblwoot.Text = ""
                    lblwoot.ForeColor = Color.Black
                Case (3)
                    lblwoot.ForeColor = Color.OrangeRed
                    lblwoot.Text = "You're incredible!!!!"
                    lblwoot.Update()
                    pause(1000)
                    lblwoot.Text = ""
                    lblwoot.ForeColor = Color.Black
                Case (4)
                    lblwoot.ForeColor = Color.Purple
                    lblwoot.Text = "You rock!!!!"
                    lblwoot.Update()
                    pause(1000)
                    lblwoot.Text = ""
                    lblwoot.ForeColor = Color.Black
            End Select
        End Sub
    
    End Class
    Attached Files Attached Files
    Last edited by Brian M.; Jul 25th, 2008 at 11:41 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width