In this Blender 2.5 video tutorial series we will be going over the basics of how to use Python in the Blender Game Engine.
In this first part we’ll briefly go over the BGE python module, as well as taking a quick look at Logic Bricks and Game Physics. After that we’ll get into the real meat of the tutorial and start writing our playerscript.py script to handle simple keyboard input and instantiate new objects at runtime.
What is covered in this tutorial:
- Basic introduction to the BGE module.
- Brief explanation of Logic Bricks.
- Brief explanation of Game Physics.
- Catching and handling keyboard input.
- Instantiating new objects at runtime.











Very Informative,
Any reason why I can’t use
keyboard.events[bge.events.LEFTMOUSE]
to fire the bullet??
When I change LEFTCTLRKEY event to this it fails.
Console reports
object ‘player controller ‘python’ error on the line I assign LEFTMOUSE
hi ther .i have a problem… and i need help plz..
when i open my game that i allready save .i finde that all the animations that i made .like walking and jumping … desiper .i find only the last one but the other animation gon .i think that the prublem is when i save animations .meby i do some thin that i dont have to do …
you are applying LEFTMOUSE under a keyboard events object. LEFTMOUSE is part of mouse events. Try again but use mouse events. Also, make sure you create a mouse object.
Oh and Leito….
you have the letter ‘o’ in your applyForce function for the z value instead of the number ’0′ .. thats probably why its failing to move.
Solved for the mouse..
You have to use mouse.events[bge.events.LEFTMOUSE]
This was a great tutorial.
I have learned more in the past few hours with this than I would have learned in months on my own.
Can’t wait to watch the next part.
Thanks a lot.
hey, super helpful tutorial!!
while I was trying to use the math.radians, the rotation looked weird. please help me!!
Hi, if I set the arrow as Rigid Body, and bounce it off a sphere, the Empty moves away as the arrow is up-ended (seen in physics visualization mode), and then slowly returns to the arrow as the arrow stop bouncing.. why is that? How to keep the Empty from moving off during the collision?
Ok, I didn’t parent the Empty to the arrow, and instead added a Rigid Body Constraint (Hinge) to it the empty, targeted to the arrow. Limited the angle to 0 .. .this kept the empty just in front of the arrow even as the arrow is tumbling around as a Rigid body colliding with stuff. Curious if this can be done using the Compound function but dunno.
Hey Can Anyone Plz Help Me I Get The Error Shaders Not Supported?
My Graphics Card Driver Wont Work Since I Updated My Comp From Xp To Win7 After It Crashed. Is This The Problem? I Used to Be Running On Nvidia Geforce 8600 But Now My Driver Only Reads It As An AVG Standard Card??????
to add jump for arrow :
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
#Move Forward
player.applyMovement((0, 0, MoveSpeed), True)
hey, i’m a begginner blender artist. loved this tutorial. but my scripting isn’t working. i copied it word for word with yours but it isn’t working. havn’t done anything to use python scripting before. is there anythign i need to download to get it to work?
thanks
Hi,Truly awsome work
Would be really great if anyone know how to open the terminal in Ubuntu?
Hi, to open Terminal in Ubuntu(just plainly) go to your search and then type in Terminal and then hit enter and it should open terminal.
I did everything you said up to the bullet and then the movement didnt work.
this is what i did(please help):
import bge
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.GetCurrentScene()
movSpeed = 0.15
rotSpeed = 0.5
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY] :
#move forward
player.applyMovement((0, movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY] :
#move forward
player.applyMovement((0, -movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY] :
#rotate left
player.applyRotation((0, 0, rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY] :
#rotate right
player.applyRotation((0, 0, -rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTCTRLKEY] :
#fire bullet
scene.addObject(“Bullet”, “BulletSpawn”, 150)
main()
Patrick, this was incredibly helpful to me. I’m trying to learn Python in Blender so I can program physical simulations of rigid body-like interactions in protein domains. Thank you so much.
I did all the movement just fine. Fully working happy as can be, with an arrow pushing a block around, until I went to add the bullet firing and then nothing,no movement works and adding the sphere doesn’t work. for some reason the console isn’t open either.
import bge
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.getCurrentScene()
movSpeed = 0.15
rotSpeed = 0.1
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
#Move forward
player.applyMovement((0, movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
#Move backward
player.applyMovement((0, -movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
#Rotate left
player.applyRotation((0, 0, rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
#Rotate right
player.applyRotation((0, 0, -rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTCTRLKEY]:
#Fire bullet
scene.addObject(“Bullet”, “BulletSpawn”, 150)
main()
is what I have so far
I cant view this video…Please help
Hi Gauri,
What is not working with the video?
Video is not getting played…on that part of video I can see only a black screen….
The reason the video doesnt work is because your device, like many, doesnt support Adobe Flash. Android Jelly Bean is also not flash compatible ( way to get rid of about 300 million + viewers on this site ). This entire site needs to be written for HTML5, which will take forever.
Just a small inconsequential bit; but you couldn’t modify the player position by adding a vector because, in Python, tuples are immutable. You could do this in your game logic (without calling the API) by creating a new tuple which adds the component values of the original player position and the new player position.
For instance:
playerPosition = (playerPosition.X + newX, playerPosition.Y + newY, playerPosition.Z + newZ)
It should work (unless playerPosition is a getter), but it is a pain in the butt to type.
Should, after adding bulletspeed, there be bullets constantly firing without any direction? I am confused.
So, this works great. One problem. The rotation .. for some reason whenever I do a rotation it makes the character, may it be a cube or sphere or arrow, go nuts. It’s really annoying when I’m trying to use the camera to look behind whatever object and follow it.
I’m using the latest stable release (2.63).
the object is a dynamic object, radius of 0.01 and using a box collision bound. The camera is parented to the box.
The code is:
# This script must be assigned to a python controller
# where it can access the object that owns it and the sensors/actuators that it connects to.
import bge
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.getCurrentScene()
movSpeed = 0.15
rotSpeed = 0.1
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]:
#move forward
player.applyMovement((0, movSpeed,0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SKEY]:
#move back
player.applyMovement((0, -movSpeed,0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]:
#move forward
player.applyRotation((0,0,rotSpeed), True)
main()
I know I must be missing something. As anyone had this problem? It can’t just be me … could it?
Tadd,
I had the same problem. After some googling, I found that this is a known bug in Blender 2.63 that causes apply_Rotation to act screwy. One workaround is to use apply_Torque instead of apply_Rotation. You’ll also probably need a different value for rot_Speed than the one you would use for apply_Rotation.
Torque gives slightly different physical behavior (i.e., it causes angular acceleration, so once you apply torque the object will continue to rotate even after you stop pressing the key, and you have to press the opposite key to “de-acellerate” – like the behavior of a spacecraft rather than a vehicle on tank treads). So this isn’t a perfect workaround. But until the bug fix, it’ll at least allow you to proceed with your scripting.
Cheers,
Matt
PS – A BIG THANKS to Patrick Boelens for his FANTASTIC tutorials !!!!!
I was trying to show this to a group of people the other day & rotation was being weird & I had no idea why.
Now I know. Thank you for posting this!
I was showing a simplified version of the game I learned here to a group of people the other day & the rotation was acting all weird!
Now I know why! Thank you for posting this – top tip!
I put this Script in: import bge
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.getCurrentScene()
movSpeed = 0.15
rotSpeed = 0.2
bulletSpeed = 1000
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
#move forward
player.applyMovement ((0, movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
#move Back
player.applyMovement ((0, -movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
#rotate left
player.applyRotation ((0, 0, rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
#rotate right
player.applyRotation ((0, 0,-rotSpeed), True)
if bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events[bge.events.LEFTCTRLKEY]:
#fire bullet
bullet = scene.addObject(
Bullet,BulletSpawn, 150)bullet.applyForce((0, bulletSpeed), True)
bullet.colour = (1, 0, 0, 1)
main()
but nothing is moving or shooting or anything. Do i need to download a client or make an Alteration to the code or what?
please note that i have only downloaded the basic Blender 2.60 Module
if you know what i need to Change it would be Very Appreciated. Thank you
Great !! I understood very well your tutorial altought I’m bad in english/american
This tutorial is going to help me.
Ty !
Can you do a tutorial that is more advanced? I haven’t seen to many python game tutorials for blender.
Seconded.
(Please)
Hey!
well i’ve checked the code everything’s fine but the object does not move when i press the up arrow key..the code is as follows
import bge
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
#move forward
player.localPosition.y += 0.5
main()
what must be the problem?
please reply asap…
Hi Murtaza,
To fix the error, you must check the indentation. Space out each phrase with tab, so that it is correct.
… excuse for my english. I hope you understand what I mean.
I’m using blender 2.64a, and my scripting will not work. I did exactly what you did, so what is wrong
def main():
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.GetCurrentScene()
movSpeed = 0.15
rotSpeed = 0.5
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY] :
#move forward
player.applyMovement((0, movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY] :
#move forward
player.applyMovement((0, -movSpeed, 0), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY] :
#rotate left
player.applyRotation((0, 0, rotSpeed), True)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY] :
#rotate right
player.applyRotation((0, 0, -rotSpeed), True)
main()
I’m using 2.64a and I can’t get the script to work. Somebody help me please
Blender won’t import ‘bge’ because it says there is no module called bge. Please someone help me!
I’m Using 2.65a
Hi,
Same problem with me, when i’m typing “import bge”, it gives me import error: no module named ‘BGE’.. Please help…
For anyone having trouble with ‘import bge,’ I’m using Blender 2.6 and instead of pressing ‘run script’ you have to press ‘p’ (to ‘play’). To go back to editing you can hit ‘Escape.’ Otherwise, the console will pop up and tell you it can’t find Module BGE. It’s a difference between the Blender versions, I guess.
What I can’t figure out is why faces on my arrow turn invisible in the game engine!
…Nevermind, I got it. I had some of my normals backwards.
cool