This tutorial covers the basics of using the keyboard to get input by using the Brutus2D keyboard object. It also assumes you already know what Brutus2D objects are and that you have read Getting Started with Brutus2D (original).
The Keyboard Object
The keyboard object is your evil minion who records every move the player makes on the keyboard. At any time, you can use the keyboard object to see exactly what keys the player has hit. This is useful for control.
Overview of the Essential Keyboard Object Methods
The keyboard object, like every other object in Brutus2D has methods which you use to tell it what to do.
- key.Initialize - before you can do anything with the keyboard, you have to wake it up with key.Initialize.
- key.Terminate - before you end your game, you need to call keyboard.Terminate to put the keyboard object back to sleep.
- key.Pressed - once the keyboard object has been initialized, you use key.Pressed to if the player has pressed a key.
Getting the Keyboard Object Started
Before you can do anything with the keyboard object you have to wake it up by using key.Initialize. key.Initialize requires that the graphics object also be up and running so you have to initialize it too.
' initialize the graphics object
graphics.Initialize
' initialize the keyboard object
key.Initialize
After this code is run, both the graphics object and the keyboard object are ready to go.
Terminate the Keyboard Object
Once you are through with the keyboard object and do not need to use any of it's methods anymore, you should terminate it. This is a part of the cleanup process. Technically, you don't have to do it, but it's considered a practice that good programmers follow.
graphics.Initialize
key.Initialize
' terminate the graphics object
graphics.Terminate
' terminate the keyboard object
key.Terminate
Capturing Input
Now you need to do something useful with the keyboard object, like reading player input through the keyboard. For this you can use key.Pressed which detects whether or not a key is pressed down.
You use it this way:
- tell it which key you want to check
- check the response and then take some action
This isn't as complex as it sounds. This is what it looks like.
if key.Pressed(vk_escape) then do something
Well, that's not exactly valid code, but you get the idea: If the player presses the ESCAPE KEY, something happens.
Here's a more complex example :
graphics.Initialize
key.Initialize
' this is the game loop
' hit the excape key or the X to exit
do while not key.Pressed(vk_escape) and not key.Pressed(vk_windowx)
graphics.Clear
'draw your stuff here
graphics.Display
loop
graphics.Terminate
key.Terminate
What the code above does is check to see if the player has pressed the ESCAPE KEY or the X at the top/right of the window. If he has, the game loop does not repeat and the game is over.
This is the essential code to get a game working in Brutus2D. However, it doesn't explain how to use key.Pressed to capture game input while the game is running. Here's a last example :
graphics.Initialize
key.Initialize
do while not key.Pressed(vk_escape) and not key.Pressed(vk_windowx)
' hit space and the window title changes
if key.Pressed(vk_space) then graphics.SetTitle "You hit SPACE!"
graphics.Clear
'draw your stuff here
graphics.Display
loop
If you hit SPACE while the game is running, the window caption will change to You hit SPACE!. You know have been introduced to the Brutus2D keyboard object. With this knowledge, you can move on more complex methods of using the keyboard for player input.