|The debug console is an invaluable tool in using the Blender Game Engine.|
Last tutorial in the series, we had something like this to the left. In this screenshot, we see that the Cube is running a script that prints out a string to the console, which is the black window to the left of the main Blender window. This window is exceptionally useful in debugging as it allows us the opportunity to view values of variables and the general state of the game, as well as see if there are any errors that the game is encountering during execution. Now, then, how do we take this further? Well, we now can begin to implement user interaction with this, allowing us to control the Player.
The first thing you'll need to do is add a Motion actuator to our Player object. Make sure that it's named 'Motion', otherwise the script won't pick the actuator up from our list of actuators in the script below. Attach the newly created actuator to the Python controller running the script.
The Blender Game Engine (as of 2.56) has built-in modules that we can use to access the keyboard and mouse. Here's the source code for input recognition:
from bge import logicSo this isn't too much different from Part 1's source code - just a few extra lines. Let's check out the differences, shall we?
from bge import events
cont = logic.getCurrentController()obj = cont.ownermotion = cont.actuators['Motion']key = logic.keyboard.eventskbleft = key[events.LEFTARROWKEY]kbright = key[events.RIGHTARROWKEY]mx = 0.0my = 0.0if kbleft > 0:mx = -0.1elif kbright > 0:mx = 0.1motion.dLoc = [mx, my, 0.0]cont.activate(motion)
from bge import eventsSo, first, we have a new import function that imports a new portion of the Blender Game Engine's programming library that we haven't used yet - events. The events module contains keybinding constant values that we can use to check for input from specific keys. For example, rather than trying to find the exact key code value for the left arrow key, we can just use events.LEFTARROWKEY. Simple, right?
motion = cont.actuators['Motion']This line creates a simple local variable, motion, that corresponds to the Python controller's actuators and finds a specific one, a Motion actuator named... well, Motion. The Motion actuator, as you probably well know, controls motion in the Blender Game Engine. While we could just alter the object's position values using Python very easily, this is a bit more useful as we get a bit more ease of control.
key = logic.keyboard.eventsAs you can see, the logic submodule (I throw 'submodule' around a lot - there are really just a 'submodule' because it's a 'child' of the bge top module) of the bge module is quite useful - it even gives us a pre-made keyboard and mouse to access. Here in the first line, we access the keyboard variable of the logic module and it's input events by creating a variable (key) that references logic.keyboard.events, which is a dictionary of each key on the keyboard and its input status. Using that key variable, we can find specific keys and whether they are being held down or not. Useful, right? In the other two lines, we create two other variables, kbleft and kbright that point to the Left Arrow and Right Arrow keyboard keys, respectively. How? Well, thanks to our previously imported module, the events module, we can now refer to specific keys using simple constants, like XKEY or RIGHTARROWKEY, like above.
kbleft = key[events.LEFTARROWKEY]
kbright = key[events.RIGHTARROWKEY]
mx = 0.0In these two lines, we create two variables, both for moving our object on the X and Y axes. We will use these to apply a simple speed to the Player object to get it to move on our command.
my = 0.0
if kbleft > 0:EDIT: Note that these keyboard variables will return 3 when just released, but this is so minute a time frame that for simplicity, I used ' > 0' rather than '... == 1 or ... == 2'. Also, note that I use an elif statement for the right key check, meaning that either the left key or the right key can be pressed at a given time - if the left key is pressed, then the right key doesn't register a response. Whether or not you wish to pursue this method of input-checking as opposed to checking for either is up to you.
mx = -0.1elif kbright > 0:
mx = 0.1
These lines are very simple. We just evaluate to see if the left arrow key is being pressed (greater than 0); if so, then we move -0.1 Blender units to the left. Otherwise, if the right arrow key is pressed, then we move 0.1 Blender units to the right. The keyboard events dictionary works very simply - if the key binding's value is 0, that means that the key isn't being pressed. 1 means that the key has just been pressed, 2 means that the key is being held down, and 3 means that the key is being released. The actual correct way of doing the lines above would be if kbleft == 1 or kbleft == 2 and elif kbright == 1 or kbright == 2, but I'm not trying to split hairs.
motion.dLoc = [mx, my, 0.0]The first lines are referencing our motion actuator's dLoc variable - this is a small Python list, or array, of how much to move the object. As can be seen above, we assign the mx and my movement variables to dLoc. If you follow the line of logic, you can see that if you press the left or right arrow keys, then the mx variable gets assigned a small value, which gets put into the motion actuator at the end. Because of this, the Player cube will move 0.1 Blender units to the right or left depending on which key you press. The last line makes the Python controller running this script activate the motion actuator, which finally affects the object, moving our Player cube if you press the keyboard keys. Play the game, and you'll be able to press the Left or Right arrow keys to move the cube. Great! Now, then, Get the source blend / code here for Part 2 of the Blender Game Engine with Python Tutorial.
If you notice, there's no use of the my variable - this would be used for moving the Player on the Y-Axis as well. However, I'm not going to show you how to do that - it will be an exercise.
EXERCISE: Implement Y-axis movement by using the my movement variable to make the Player move using all keyboard arrow keys.
That's all for now - if you have questions, don't hesitate to contact me either via E-Mail, on the comments section here or on any of the other posts on my blog, or right on the BlenderArtists forum - I and the others there will be glad to help you out.