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.

Intro to Scripting in BGE>

You must be logged in to upload images. Register

Discussion

141 Responses to “Introduction to Scripting in the Blender Game Engine – Part 01”
  1. SpuriousGrader
    Posts: 3

    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

    #
    51
    Feb 20, 2012 at 11:20 am
    • 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 …

      #
      51.1
      May 4, 2013 at 6:51 am
    • Posts: 3

      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.

      #
      51.2
      May 8, 2013 at 11:54 am
  2. SpuriousGrader
    Posts: 3

    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.

    #
    52
    Feb 20, 2012 at 11:33 am
  3. SpuriousGrader
    Posts: 3

    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.

    #
    53
    Feb 20, 2012 at 12:42 pm
  4. yd
    Posts: 1

    hey, super helpful tutorial!!

    while I was trying to use the math.radians, the rotation looked weird. please help me!!

    #
    54
    Feb 26, 2012 at 7:16 pm
  5. Posts: 2

    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?

    #
    55
    Mar 8, 2012 at 10:14 pm
    • Posts: 2

      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.

      #
      55.1
      Mar 8, 2012 at 11:51 pm
  6. Zadboy
    Posts: 1

    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??????

    #
    56
    Mar 9, 2012 at 8:37 pm
  7. robin
    Posts: 2

    to add jump for arrow :

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
    #Move Forward
    player.applyMovement((0, 0, MoveSpeed), True)

    #
    57
    Mar 21, 2012 at 1:15 am
  8. Posts: 1

    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

    #
    58
    Mar 31, 2012 at 6:37 pm
  9. Posts: 1

    Hi,Truly awsome work :)
    Would be really great if anyone know how to open the terminal in Ubuntu?

    #
    59
    Apr 21, 2012 at 10:15 am
    • SIboomer
      Posts: 2

      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.

      #
      59.1
      Apr 22, 2012 at 4:30 pm
  10. SIboomer
    Posts: 2

    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()

    #
    60
    Apr 22, 2012 at 4:18 pm
  11. jonesmat
    Posts: 2

    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.

    #
    61
    Apr 23, 2012 at 8:38 pm
  12. rAINBOW rOAD
    Posts: 5

    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.

    #
    62
    May 13, 2012 at 10:03 pm
    • rAINBOW rOAD
      Posts: 5

      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

      #
      62.1
      May 13, 2012 at 10:04 pm
  13. Gauri
    Posts: 4

    I cant view this video…Please help

    #
    63
    May 20, 2012 at 12:10 pm
    • Posts: 2975

      Hi Gauri,

      What is not working with the video?

      #
      63.1
      May 20, 2012 at 1:08 pm
      • Gauri
        Posts: 4

        Video is not getting played…on that part of video I can see only a black screen….

        #
        63.1.1
        May 21, 2012 at 12:30 am
    • Scott Zupek
      Posts: 1

      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.

      #
      63.2
      Feb 24, 2013 at 10:47 pm
  14. Shawn
    Posts: 1

    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.

    #
    64
    May 20, 2012 at 3:17 pm
  15. rAINBOW rOAD
    Posts: 5

    Should, after adding bulletspeed, there be bullets constantly firing without any direction? I am confused.

    #
    65
    May 29, 2012 at 11:39 am
  16. Tadd Mencer
    Posts: 14

    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?

    #
    66
    May 29, 2012 at 12:49 pm
    • jonesmat
      Posts: 2

      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 !!!!!

      #
      66.1
      May 29, 2012 at 3:34 pm
      • Posts: 5

        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! :)

        #
        66.1.1
        Jan 2, 2013 at 11:34 am
      • Posts: 5

        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! :)

        #
        66.1.2
        Jan 2, 2013 at 11:37 am
  17. Braden
    Posts: 1

    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

    #
    67
    Jun 30, 2012 at 3:17 pm
  18. BlenderRender
    Posts: 1

    Great !! I understood very well your tutorial altought I’m bad in english/american :D
    This tutorial is going to help me.
    Ty !

    #
    68
    Jul 25, 2012 at 7:01 am
  19. Posts: 35

    Can you do a tutorial that is more advanced? I haven’t seen to many python game tutorials for blender.

    #
    69
    Jul 27, 2012 at 8:37 pm
  20. Murtaza
    Posts: 1

    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…

    #
    70
    Sep 4, 2012 at 2:29 am
    • Zrigs
      Posts: 1

      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.

      #
      70.1
      Feb 20, 2013 at 10:57 am
  21. Posts: 17

    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()

    #
    71
    Oct 30, 2012 at 11:51 am
  22. Posts: 17

    I’m using 2.64a and I can’t get the script to work. Somebody help me please

    #
    72
    Oct 30, 2012 at 2:31 pm
  23. Posts: 17

    Blender won’t import ‘bge’ because it says there is no module called bge. Please someone help me!

    #
    73
    Jan 30, 2013 at 1:22 pm
  24. Posts: 2

    Hi,

    Same problem with me, when i’m typing “import bge”, it gives me import error: no module named ‘BGE’.. Please help…

    #
    74
    Feb 11, 2013 at 6:52 am
  25. Posts: 7

    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!

    #
    75
    Feb 22, 2013 at 4:40 pm
    • Posts: 7

      …Nevermind, I got it. I had some of my normals backwards. :P

      #
      75.1
      Feb 22, 2013 at 4:44 pm
  26. cool

    #
    76
    Mar 25, 2013 at 12:37 pm

Leave a Comment

You must be logged in to post a comment.