PDA

Click to See Complete Forum and Search --> : Buzzing in!!!!


Kerlfop
Mar 14th, 2001, 01:36 PM
I am making a Jeopardy game for my VB class and was needing some basic code. I need to allow for multiple players to be able to buzz in to answer the question. I figure it could be by pressing one of three keys. Any suggestion?

Bob

Sastraxi
Mar 14th, 2001, 06:15 PM
Okay... I will refer to these functions in my code:

PlaySnd - a function that plays a sound
HndBuzz - the function that handles the buzz (what happens when a player buzzes)

Okey! Lets see the code, it's commented so you can learn from it:


Const Player1=vbKeyA 'Player 1's buzz-in key
Const Player2=vbKeyB 'Player 2's buzz-in key
Const Player3=vbKeyC 'Player 3's buzz-in key

Dim PlayerBuzz as Long

Private Sub Form_KeyDown(vb declarations, just here to tell you which sub it's in)
PlayerBuzz=keycode 'puts the keystoke into the variable so we can read it in another sub

End Sub

Sub SeeIfBuzz() 'user-sub: call it when you want to check if there's a buzz-in.

If PlayerBuzz <> 0 Then
If PlayerBuzz = Player1 Then
HndBuzz 1 'call the handling funct. for buzzing for player 1
ElseIf PlayerBuzz = Player2 Then
HndBuzz 2 'call the funct. for p 2
ElseIf PlayerBuzz = Player3 Then
HndBuzz 3 'once again, call funct for p 3
End If

End Sub

(here's an example of how to use this code:)

Private Sub Timer1_Timer()
SeeIfBuzz
'... other game code here...
End Sub


I can post more if you want, this is just to handle the buzz. I made it easy to change key configs, you just change the keys of player1, player2, and player3 consts accordingly.

Just some humor here, ya know what would be funny? Setting the keys to CTRL, ALT and DELETE. :D :)

-just a bit of my humor, DONT do that. :D

I'm only an email away - Sastraxi

creamPuff
Jun 12th, 2003, 03:08 PM
knock, knock,... i just found this thread and im having same difficulties, can you please elaborate more so it would be easy for me to understand this code, thanks soo much!

Sastraxi
Jun 12th, 2003, 05:09 PM
Wow... what an old thread. You're lucky I'm still around :p

Anyway, I wouldn't recommend this approach anymore; use the GetAsyncKeyState API instead. Here's some updated code ;)Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

Private Const VK_CONTROL = &H11
Private Const VK_SPACE = &H20
Private Const VK_NUMPAD0 = &H60

Const Player1 = VK_CONTROL 'Player 1's buzz-in key
Const Player2 = VK_SPACE 'Player 2's buzz-in key
Const Player3 = VK_NUMPAD0 'Player 3's buzz-in key

Private bStop As Boolean 'Set this to True when the question has been answered

Private Sub QuestionAnswerPeriod()
'call this when you have asked a question

bStop = False
Do While Not bStop
DoEvents
If GetAsyncKeyState(Player1) And 1 Then
... what you want to do to ask them (for player #1)
ElseIf GetAsyncKeyState(Player2) And 1 Then
... what you want to do to ask them (for player #2)
ElseIf GetAsyncKeyState(Player3) And 1 Then
... what you want to do to ask them (for player #3)
End If
Loop

End SubHope it helps... :)