vb5 game help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JMy
    New Member
    • May 2006
    • 3

    #1

    vb5 game help

    i have a vb5 game in the works and i need help with it. it needs a random number genorator because the player must later guess the number. there is a problom i can't solve. the number is never actually returned. please help. this is the code i have for this function:


    Private Sub Command1_Click( )
    If Option1.Value = True Then
    Randomize
    thenumb = Int((15 * Rnd) + 1)
    End If
    If Option2.Value = True Then
    Randomize
    thenumb = Int((30 * Rnd) + 1)
    End If
    If Option3.Value = True Then
    Randomize
    thenumb = Int((50 * Rnd) + 1)
    End If
    If Option1.Value = False Then
    If Option2.Value = False Then
    If Option3.Value = False Then
    Randomize
    thenumb = Int((1000000 * Rnd) + 1)
    End If
    End If
    End If
    Form1.Visible = False
    Form2.Visible = True
    End Sub
  • CaptainD
    New Member
    • Mar 2006
    • 135

    #2
    First off, you say the game works, does that mean you are trying to make a change to it?

    As for your code, I made a simple change to test your code and it works.
    Add a label to your form and paste this into your click event and see what happens.
    Code:
    Private Sub Command1_Click()
    Dim thenumb As Long 'Added this for the variable to hold the random number
    If Option1.Value = True Then
    Randomize
    thenumb = Int((15 * Rnd) + 1)
    End If
    If Option2.Value = True Then
    Randomize
    thenumb = Int((30 * Rnd) + 1)
    End If
    If Option3.Value = True Then
    Randomize
    thenumb = Int((50 * Rnd) + 1)
    End If
    If Option1.Value = False Then
    If Option2.Value = False Then
    If Option3.Value = False Then
    Randomize
    thenumb = Int((1000000 * Rnd) + 1)
    End If
    End If
    End If
    'Form1.Visible = False
    'Form2.Visible = True
    Label1.Caption = thenumb 'Added this to display the results
    End Sub
    Second, this is a sub event, not a function. Do you understand how a function works? (Subs do work where functions return values)

    Here is a simple function. Add a second command button and paste the following code then click the second command button
    Code:
    Private Function ReturnRandomNumber(iPassedNumber As Long) As Long
    'The number passed in is a long (iPassedNumber) and "ReturnRandomNumber"
    ' is the output, also a long
    Randomize
    ReturnRandomNumber = Int((iPassedNumber * Rnd) + 1)
    
    End Function
    
    Private Sub Command2_Click()
    Dim lngNumberToPass As Long
    
    If Option1.Value = True Then
        lngNumberToPass = 15
    End If
    
    If Option2.Value = True Then
        lngNumberToPass = 30
    End If
    If Option3.Value = True Then
        lngNumberToPass = 50
    End If
    If (Option1.Value And Option2.Value And Option3.Value) = False Then
        lngNumberToPass = 1000000
    End If
    Label1.Caption = CStr(ReturnRandomNumber(lngNumberToPass))
    
    End Sub

    Comment

    • JMy
      New Member
      • May 2006
      • 3

      #3
      thanks, i forgot to mention the moduel (can't spell) that had thenumb. thanks again!

      i ran it, and it works, but now the second form doesn't...

      Private Sub Command1_Click( )
      pguess = Text1.Text
      If pguess = thenumb Then
      Form2.Visible = False
      Form3.Visible = True
      End If
      If pguess < thenumb Then
      Label1.Caption = "too small"
      pscore = pscore - 1
      End If
      If pguess > thenumb Then
      Label1.Caption = "too big"
      pscore = pscore - 1
      End If
      Command1.Value = False
      End Sub

      i want it to run again, but it runs in a loop if you tell it to goto the top. i need it to stop here, then rerun...
      Last edited by JMy; May 22 '06, 12:25 AM.

      Comment

      • CaptainD
        New Member
        • Mar 2006
        • 135

        #4
        Couple of things.

        First, your new code does not reference Form1 like it initially did and you now have a Form3. So, I'm not sure what's missing or what is supposed to happen.

        You should try to explain you problem a little clearer since no one here can see your program. you also should post code where the problem exist and note what type of problem.

        Do you know how to "Step Through" code? If you click on the bar to the left of where you type the code it will place a "Break Point" (Red Dot) where you clicked.(try to do it at the beginning of where you want to check your code)
        Once the4 cursor stops at the break point you can press F8 and move through the code checking values.

        On you coding, it's a common practice to Type with both Lower and Upper case to separate words that are joined, it makes them easier to read.
        Example: NumberToPass is easier to read then numbertopass
        Also, try indenting your code.
        Example:
        Code:
        If bThisValue = True Then
            DoSomeWork
        Else
            If bAnotherValue = True Then
                DoMoreWork
            End If
        End If
        Post more information and I'll see what I can find, as it is, I can not see a "Loop"

        Comment

        Working...