質問

I am creating a game where the player answers a question and then if they answer correctly, then they get to flip two cards over to see if they match (bit like pairs game). However, I am struggling to figure out how I can stop the input box (how the user enters their answer), if the player gets the question right, from appearing for a period of time until the player has selected two cards.

I am using two arrays, one for holding the questions and the other for holding the answers. P.s. Excuse the last few questions, trying to get it to work before I finish them all.

Private Sub Questions()

    strQuestions(0) = "A common noun refers to the name of things."
    strQuestions(1) = "A pronoun is a word that takes the place of nouns."
    strQuestions(2) = "'She' is a pronoun that would replace a woman's name."
    strQuestions(3) = "The days of the week are proper nouns."
    strQuestions(4) = "'He', 'She' and 'them' are all pronouns."
    strQuestions(5) = "Spain is a proper noun."
    strQuestions(6) = "Proper nouns always start with a capital letter."
    strQuestions(7) = "The place 'England' is referred to as a proper noun."
    strQuestions(8) = "A 'camera' is a common noun."
    strQuestions(9) = "FE"
    strQuestions(10) = "GR"

    strAnswers(0) = "TRUE"
    strAnswers(1) = "TRUE"
    strAnswers(2) = "TRUE"
    strAnswers(3) = "TRUE"
    strAnswers(4) = "TRUE"
    strAnswers(5) = "TRUE"
    strAnswers(6) = "TRUE"
    strAnswers(7) = "TRUE"
    strAnswers(8) = "TRUE"
    strAnswers(9) = "TRUE"
    strAnswers(10) = "TRUE"

    Dim userAnswer As String

    For i = 0 To UBound(strQuestions)
        userAnswer = InputBox(strQuestions(i))
        If userAnswer <> strAnswers(i) Then
            MsgBox("Incorrect. Try again.")
        Else
            MsgBox("Correct! Make your move.")
            WHAT GOES HERE?
        End If
    Next i
End Sub

Private Sub label_click(ByVal sender As System.Object,
                        ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click,
                    Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click,
                    Label11.Click, Label10.Click, Label1.Click

    ' The timer is only on after two non-matching 
    ' icons have been shown to the player, 
    ' so ignore any clicks if the timer is running
    If Timer1.Enabled Then Exit Sub

    Dim clickedLabel = TryCast(sender, Label)

    If clickedLabel IsNot Nothing Then

    End If
    ' If the clicked label is black, the player clicked
    ' an icon that's already been revealed --
    ' ignore the click
    If clickedLabel.ForeColor = Color.Black Then Exit Sub

    ' If firstClicked is Nothing, this is the first icon 
    ' in the pair that the player clicked, 
    ' so set firstClicked to the label that the player 
    ' clicked, change its color to black, and return
    If firstClicked Is Nothing Then
        firstClicked = clickedLabel
        firstClicked.ForeColor = Color.Black
        Exit Sub
    End If

    ' If the player gets this far, the timer isn't 
    ' running and firstClicked isn't Nothing, 
    ' so this must be the second icon the player clicked
    ' Set its color to black
    secondClicked = clickedLabel
    secondClicked.ForeColor = Color.Black

    CheckForWinner()

    ' If the player clicked two matching icons, keep them 
    ' black and reset firstClicked and secondClicked 
    ' so the player can click another icon
    If firstClicked.Text = secondClicked.Text Then
        firstClicked = Nothing
        secondClicked = Nothing
        Questions()
        Exit Sub
    End If

    ' If the player gets this far, the player 
    ' clicked two different icons, so start the 
    ' timer (which will wait three quarters of 
    ' a second, and then hide the icons)
    Timer1.Start()

    Questions()
Exit Sub
役に立ちましたか?

解決

You need to restructure your program so that the part where the questions are asked is not in a loop. Instead consider using a sub which asks for the next answer. If the correct answer is given, the sub exits and the application waits for the user to pick a card.

Once the cards are selected, you call the ask a question sub again. The position through the list of questions is stored as a variable in the form. Each time the ask a question sub is executed, it checks if the end of the list has been reached and if so ends the game.

Something like this (although its been a while since I wrote any VB!)

Private CurrentQuestion As Integer

Private Sub Questions()
    CurrentQuestion = 0

    ...


    AskQuestion
End Sub

Private Sub AskQuestion
    If CurrentQuestion = UBound(strQuestions)
        EndGame
    Else
        userAnswer = InputBox(strQuestions(CurrentQuestion))
        If userAnswer <> strAnswers(CurrentQuestion) Then
            MsgBox("Incorrect. Try again.")
            AskQuestion
        Else
            CurrentQuestion = CurrentQuestion + 1
            MsgBox("Correct! Make your move.")
        End If
    End If
End Sub

Private Sub label_click(ByVal sender As System.Object,

    ....

    AskQuestion
End Sub
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top