VB6 - Jogo de perguntas

SA-SS-A

Power Member
Boa tarde!
Estou a fazer um exercicio do qual estou com uma duvida:
É um jogo simples de perguntas e respostas, com uma folha do excell para as perguntas e outra para as respostas, sao 6 ao todo, havendo 3 temas.
Já me diz se as respostas estão certas ou não (sem variavel), mas chegou a uma altura que na segunda pergunta deixou-me de acusar a resposta certa!

Código:
    If lbl_numero.Caption = 2 And opt_1.Value Then
   MsgBox ("Certo")
   GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado
Código:
If lbl_numero.Caption = 2 And opt_1.Value Then
refere-se à respoas certa, e aparece sempre a mensagem de errado! Nao percebo este erro...
se me poderem ajudar ficaria grato.
cumps


Código:
Private Sub CommandButton2_Click()
End
End Sub

Private Sub Frame1_Click()

End Sub

Private Sub pergunta_seguinte_Click()

    If lbl_numero.Caption = 1 And opt_3.Value Then
    MsgBox ("Certo")
  GoTo saicerto
  Else
  MsgBox ("Errado")
  GoTo saierrado
End If

    If lbl_numero.Caption = 2 And opt_1.Value Then
   MsgBox ("Certo")
   GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado
End If

    If lbl_numero.Caption = 3 And opt_3.Value Then
  GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado

End If
 
    If lbl_numero.Caption = 4 And opt_4.Value Then
   GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado

End If
  
    If lbl_numero.Caption = 5 And opt_4.Value Then
    GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado

End If
  
    If lbl_numero.Caption = 6 And opt_3.Value Then
   GoTo saicerto
  Else
   MsgBox ("Errado")
   GoTo saierrado

End If
  

If opt_1.Value = False And opt_2.Value = False And opt_3.Value = False And opt_4.Value = False Then
MsgBox ("Tem de escolher uma resposta! O jogo vai fechar.")
End
End If


If lbl_numero.Caption >= 6 Then
MsgBox "Não há mais perguntas !"
pergunta_seguinte.Enabled = False
End
End If


saicerto:

  
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)


saierrado:


'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)

    End Sub


Private Sub UserForm_Initialize()
'Mostra no numero da pergunta!
lbl_numero.Caption = 1
'Mostrar a descricao da pergunta!
lbl_pergunta.Caption = Sheets(5).Cells(1, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(1, 2))
'mostrar as Respostas!
opt_1.Caption = Sheets(6).Cells(1, 1)
opt_2.Caption = Sheets(6).Cells(1, 2)
opt_3.Caption = Sheets(6).Cells(1, 3)
opt_4.Caption = Sheets(6).Cells(1, 4)

End Sub
 
Última edição pelo moderador:
Evita ao máximo usar o GoTo. Isso torna o código muito confuso.
Por exemplo, em vez de GoTo saicerto fazias "certo = true" e em vez de GoTo saierrado fazias "certo = false".

Depois, em vez do saicerto: e do saierrado: fazias If certo then (o que está no saicerto) Else (o que está no saierrado) End If.

E podias aprender a usar o Select Case e a usar arrays de controlos para evitar tantos IFs ;)

Em relação ao problema em especial, sê mais explícito. Ele "deixa de acusar a resposta certa" como?
 
Olá, obrigado pela dicas!
Eu tenho para quando a opçao 1 da pergunta 2 estiver seleccionada, ele vai dizer que esta certo e anda uma pergunta para a frente, se não, aparece msgbox dizer que esta errado e continua na mesma pergunta.

Ora ele esta a dizer que as 4 opt estao erradas!

Utilizei o goto porque quando carregava pergunta seguinte, aparecia a msgbox a dizer se estava certo ou errado, clicava OK e depois surgiam-me as outras msgbox que restavam no projecto...ate acabarem as msgbox.
cumps
 
Última edição:
Código:
Private Sub CommandButton2_Click()
End
End Sub

Private Sub Frame1_Click()

End Sub

Private Sub pergunta_seguinte_Click()

    If lbl_numero.Caption = 1 And opt_3.Value Then
    MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
 MsgBox ("Errado")
End If
    
    If lbl_numero.Caption = 2 And opt_2.Value Then
        MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
  MsgBox ("Errado") ''NAO DA PRA POR MSGBOX("ERRADO") PORQUE SE REPETE!!!!!''
End If

    If lbl_numero.Caption = 3 And opt_3.Value Then
         MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
End If
    If lbl_numero.Caption = 4 And opt_4.Value Then
        MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
End If
  
    If lbl_numero.Caption = 5 And opt_4.Value Then
        MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
End If
  
    If lbl_numero.Caption = 6 And opt_3.Value Then
       MsgBox ("Certo")
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem

'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
MsgBox "Esta foi a última pergunta !"
pergunta_seguinte.Enabled = False
End
  Else
End If
  
'opt_1.Value = False
'opt_2.Value = False
'opt_3.Value = False
'opt_4.Value = False
''If opt_1.Value = False And opt_2.Value = False And opt_3.Value = False And opt_4.Value = False Then
''MsgBox ("Tem de escolher uma resposta! O jogo vai fechar.")
''End
''End If


''If lbl_numero.Caption >= 6 Then
''MsgBox "Esta é a última pergunta !"
''pergunta_seguinte.Enabled = False

''End If


saierrado:

 
'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)

    End Sub


Private Sub UserForm_Initialize()
'Mostra no numero da pergunta!
lbl_numero.Caption = 1
'Mostrar a descricao da pergunta!
lbl_pergunta.Caption = Sheets(5).Cells(1, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(1, 2))
'mostrar as Respostas!
opt_1.Caption = Sheets(6).Cells(1, 1)
opt_2.Caption = Sheets(6).Cells(1, 2)
opt_3.Caption = Sheets(6).Cells(1, 3)
opt_4.Caption = Sheets(6).Cells(1, 4)

End Sub

Nao percebo porque é que quando carrego na resposta certa à primeira pergunta e aparece msgbox a dizer "certo" e logo de seguida aparece msgbox a dizer "errado".

Já fiz de varias maneiras e ainda nao consegui por isto a funcionar como deve ser...
 
Última edição pelo moderador:
depois de verificar a primeira resposta, e dar a MsgBox a dizer que está certo, não páras o procedimento. por isso o programa segue para a segunda pergunta automaticamente, e ai mostra a segunda MsgBox
 
Olá! Obrigado por teres respondido!

Código:
Private Sub pergunta_seguinte_Click()

    If lbl_numero.Caption = 1 And opt_3.Value Then
    MsgBox ("Certo")
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
  Else
 MsgBox ("Errado")
 End If


Pois ja me tinha apercebido disso, pensava que seguido do else, ele apenas mostrava a msgbox e parava! Entao como devo parar? Tambem googlei por essa instruçao e nao encontrei nada! Stop, sai do programa....lol. Desculpa é que sou novato nisto.

cumps
 
Última edição pelo moderador:
sendo novato e pelo que vejo do teu código, aposto que ainda não sabes trabalhar com funções nem arrays pois não? é que facilitava bastante isso tudo, pois era escusado estar a verificar cada pergunta individualmente

aqui vai um bocadinho de pseudocódigo para veres uma maneira de estruturar isso:

Código:
se pergunta = 1 então
      se resposta = 3 então  'sendo 3 a resposta certa
         msgbox("certo")
         aumentar pontuação
         passar para a pergunta seguinte
      senão
         msgbox("errado")
         diminuir pontuação ' ou outra coisa que queiras fazer no caso de estar errada
      fim Se
senão, se pergunta = 2 então
      'verificar pergunta 2 da mesma maneira que a anterior...
...
...
senão, se pergunta = 10 então
  ...

fim Se

acabei por misturar português, pseudocódigo e Visual Basic :p, mas acho que dá para perceber
 
O que eu disse acima é exactamente o que o MPalhas disse :)
A diferença é que em vez do "se pergunta = 1, senão, se pergunta = 2", era um "Select case pergunta", "case 1:".

Acho que antes de continuares, deves mesmo aprender a usar o IF e o SELECT CASE, e a imaginar o "fluxo" de execução do programa, para evitar erros como estes ;)

Usando arrays, só deves precisar de UM If.
Algo na onda do
respostacerta(1) = 3
respostacerta(2) = 2
respostacerta(3) = 4

if optionbox(respostacerta(perguntaactual)).value = true then msgbox "certo" else msgbox "errado"
Se a pergunta actual for 1, a resposta a essa pergunta é 3, e ele irá verificar se a optionbox correspondente à resposta 3 está marcada.
 
Última edição:
Obrigado aos dois pela ajuda realmente deu para facilitar:

Código:
Private Sub pergunta_seguinte_Click()

    If lbl_numero.Caption = 1 Then
    If opt_3.Value Then
      MsgBox ("certo")
      'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
         Else
        MsgBox ("errado")
        'Mostra no numero da pergunta!
'lbl_numero.Caption = lbl_numero.Caption
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
 End If
End If
    If lbl_numero.Caption = 2 Then
    If opt_2.Value Then
                MsgBox ("certo")
      'Mostra no numero da pergunta!
lbl_numero.Caption = lbl_numero.Caption + 1
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
opt_1.Value = False
opt_2.Value = False
opt_3.Value = False
opt_4.Value = False
Else
        MsgBox ("errado3")
        'Mostra no numero da pergunta!
'lbl_numero.Caption = lbl_numero.Caption
x = lbl_numero.Caption
'Mostrar a descricao da pergunta!'
lbl_pergunta.Caption = Sheets(5).Cells(x, 1)
'mostra a imagem
img_pergunta.Picture = LoadPicture(Workbooks(1).Path & "\tecnologia\" & Cells(x, 2))
'mostrar As Respostas
opt_1.Caption = Sheets(6).Cells(x, 1)
opt_2.Caption = Sheets(6).Cells(x, 2)
opt_3.Caption = Sheets(6).Cells(x, 3)
opt_4.Caption = Sheets(6).Cells(x, 4)
 End If
End If

Ja esta mais funcional, o problema é que na 1a pergunta se respondo certo surge msgbox "certo" e logo de seguida msgbox "errado3"...salta logo para essa msgbox...

se houvesse algum comando para fazer uma pausa no codigo....

cumos

cumps
 
Última edição pelo moderador:
Código:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

´Usas o comando Sleep, metes o tempo a frente, O tempo está milisegundos!
Sleep 2500

Ele funciona, mas nuca usei em nenhum programa por isso nao se serve para o que queres!

Cumps, Pegeot!
 
Back
Topo