My code for checking game input.

BlitzMax Forums/BlitzMax Beginners Area/My code for checking game input.

allranger(Posted 2012) [#1]
I am currently trying to making a 2D platform game. Something like you would see on the classic game consoles. I have a USB joystick for my computer that looks like a SNES/XBOX controller so I am programing with that in mind.

I have read several tutorials and am having a blast. But there are a few things I do not grasp at the moment. The whole OOP is new to me.

One of the things I read is that you must be careful to only poll user input once a game cycle because it can cause some strange things to happen in your game if you don't because the input may change between checks.

Of course I did not account for that when I started... :-)

So I started from over from the ground up and this is what I have:



Type TGameInput

'Set Varibles for SNES/XBox Type Controller.
Global DPad_Up_Pressed:Int=False
Global DPad_Down_Pressed:Int=False
Global DPad_Left_Pressed:Int=False
Global DPad_Right_Pressed:Int=False
Global Button_Start_Pressed:Int=False
Global Button_Select_Pressed:Int=False
Global Button_A_Pressed:Int=False
Global Button_B_Pressed:Int=False
Global Button_X_Pressed:Int=False
Global Button_Y_Pressed:Int=False
Global Button_L_Pressed:Int=False
Global Button_R_Pressed:Int=False

'Set Keys for Varibles.
Global DPad_Up:Int=KEY_UP
Global DPad_Down:Int=KEY_DOWN
Global DPad_Left:Int=KEY_LEFT
Global DPad_Right:Int=KEY_RIGHT
Global Button_Start:Int=KEY_ENTER
Global Button_Select:Int=KEY_TAB
Global Button_A:Int=KEY_X
Global Button_B:Int=KEY_Z
Global Button_X:Int=KEY_S
Global Button_Y:Int=KEY_A
Global Button_L:Int=KEY_Q
Global Button_R:Int=KEY_W

'Set Varibles for echo.
Global EchoControllerInput:Int=False

Function CheckInput()

If KeyDown(DPad_Up) And Not KeyDown(DPad_Down)
If EchoControllerInput Print "Up."
DPad_Up_Pressed=True
Else
DPad_Up_Pressed=False
End If

If KeyDown(DPad_Down) And Not KeyDown(DPad_Up)
If EchoControllerInput Print "Down."
DPad_Down_Pressed=True
Else
DPad_Down_Pressed=False
End If

If KeyDown(DPad_Left) And Not KeyDown(DPad_Right)
If EchoControllerInput Print "Left."
DPad_Left_Pressed=True
Else
DPad_Left_Pressed=False
End If

If KeyDown(DPad_Right) And Not KeyDown(DPad_Left)
If EchoControllerInput Print "Right."
DPad_Right_Pressed=True
Else
DPad_Right_Pressed=False
End If

If KeyDown(Button_Start)
If EchoControllerInput Print "Start."
Button_Start_Pressed=True
Else
Button_Start_Pressed=False
End If

If KeyDown(Button_Select)
If EchoControllerInput Print "Select."
Button_Select_Pressed=True
Else
Button_Select_Pressed=False
End If

If KeyDown(Button_A)
If EchoControllerInput Print "Button A."
Button_A_Pressed=True
Else
Button_A_Pressed=False
End If

If KeyDown(Button_B)
If EchoControllerInput Print "Button B."
Button_B_Pressed=True
Else
Button_B_Pressed=False
End If

If KeyDown(Button_X)
If EchoControllerInput Print "Button X."
Button_X_Pressed=True
Else
Button_X_Pressed=False
End If

If KeyDown(Button_Y)
If EchoControllerInput Print "Button Y."
Button_Y_Pressed=True
Else
Button_Y_Pressed=False
End If

If KeyDown(Button_L)
If EchoControllerInput Print "Button L."
Button_L_Pressed=True
Else
Button_L_Pressed=False
End If

If KeyDown(Button_R)
If EchoControllerInput Print "Button R."
Button_R_Pressed=True
Else
Button_R_Pressed=False
End If

End Function

End Type


Then, in may main game loop I run: TGameInput.CheckInput()

I added EchoControllerInput for debugging. If it is set to true and you are in a windowed screen it will print out what buttons you are pressing.

Also, the first time around I was checking to make sure that the user did not have both the left and right or up and down directions pressed for the update part of my loop. Instead of doing that every time I have now moved it down into the CheckInput Function.

I plan to add an option so it will read a controller configuration file so you can save the keyboard/joystick options but I have not made it that far yet.

Does anybody have any input on the code? I think I have accounted for most situations.

Thanks.


matibee(Posted 2012) [#2]
Looks ok, if a little lengthy IMHO.

If KeyDown(Button_L)
If EchoControllerInput Print "Button L."
Button_L_Pressed=True
Else
Button_L_Pressed=False
End If


to me, is..
Button_L_Pressed=KeyDown(Button_L)


because I don't prevent multiple buttons down in the input code, I deal with it in the game logic. Multiple buttons are often used for cheats or combos.

If you have player run logic tied to left and right inputs my input would look something like..

If ( KeyDown( KEY_LEFT ) )
  'go left
else if ( KeyDown( KEY_RIGHT ) )
  ' go right
end if 


A more flexible system would simply allow you to register the inputs you're interested in with the input action tied to it.

Most importantly your code will work for your needs right now and you can tweak it later - move forward and get some more game written :)


matibee(Posted 2012) [#3]
Just remembered this: http://www.blitzbasic.com/Community/posts.php?topic=85744