In this lesson we'll be capturing mouse input with python inside the game engine.
To check if the mouse input is being captured we'll make a button and capture the mouse
location.
Create two properties in the default cube named as
mouseX and mouseY
leave it as floating point integers and to the coding part...
Import the game engine module, make a function that runs in a loop,
define the controller and owner object.
We now gotta tell what our previously created properties are going to do,
so now we'll assign both of them to bge.logic.mouse.position
and these properties lets us know the position of our mouse.
try:
import bge # Blender Game Engine
except:
import Range as bge
cont = bge.logic.getCurrentController() # the script
def main(cont): # the start
own = cont.owner # scripts object
mouse = bge.logic.mouse # mouse list
# Tracking mouse position
own["mouseX"], own["mouseY"] = mouse.position
And the logic bricks setup looks like this.
To check the mouse input we check the mouse input in the .activeInputs just like we did in last lesson. So we do something like this.
if 116 in mouse.activeInputs:
print("Less Gooo")
# or an alternative way of doing it is like below
if bge.events.LEFTMOUSE in mouse.activeInputs:
print("Less Gooo")
So for left and right clicks we do the following.
if 116 in mouse.activeInputs:
print("Less Go Left!")
if 118 in mouse.activeInputs:
print("Less Go Right!")
We have our setup working, if necessary feel free to check the Range Engine documentation to check the keycodes for the mouse input and download the project file .
Pending... Please wait I'll finish this as soon as possible.