[TUTORIAL] Your First Python Script In Blender.

Hello, this is a very, very simple script. Instead of using logic bricks to do this simple task of moving the player object we will be using python.
There are pro’s and con’s to using python and logic bricks.
but as a novice user of the BGE I am afriad I cannot give a good opinion on what is best.
For now, we will say personal preference.

This tutorial assumes you have little or no programming knowledge.
Don’t be worried, programming is just another way to make your dreams come true.:wink:

I shall put comments in my code, so you can see what the code does.
A comment looks like this.

this is a comment, defined by the hash key.


You need to create an object in blender, let us use a cube.
name your cube player, in the object’s panel.

Create a plane under the cube, make the plane as big as you wish, think of it as EARTH.

Change your view from blender render to Blender Game Engine.
Change your screen layout from Default to `Game Logic,’

Select your cube, and go to your logic panel (located under your 3d screen).
You need to assign this cube(called player) a SENSOR. (A sensor detects certain conditions in the BGE)

In our logic bricks, choose the Always,' sensor. In the always logic brick, in the bottom left corner, there is a box with three dots in it. Click it. You have just actived theTRUE,’ state on this sensor, so it will ALWAYS run.

While still in the logic bricks, move over to CONTROLLERS.
Set this to be Python, as we will be using a python script.

Let us quickly go over that, think of the logic bricks as a T.V remote.
A SENSOR checks the buttons, to see what is pressed.
A CONTROLLER sends that infomation to the T.V
A PYTHON script does that action on the T.V

Lets do some coding!.

I hope this helps, please exscuse my bad typing, Ive got to watch t.v with the other half, might fix it later. :)

import bge
# import bge tells our script we want to use information from the bge - the Blender Game Engine.

def main():

    cont = bge.logic.getCurrentController()
    # we use our controler we made before, remember the logic bricks? we call is cont.
    player = cont.owner
    # since cont is our controler, when we say cont.owner - we say `who is the controllers owner?' python asks the BGE and the BGE will reply with the cubes information as the owner.
    keyboard = bge.logic.keyboard
    # this is pretty self explanatory - we want to usethe same keyboard input as the BGE.

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
   # a simple if statment: if the BGE keyboard input is on than check to see if the event
   # is the UP arrow key, if it is, then we want to do the following
   # we want our player object ( a cube) to move (applyMovement) on the (x,y,z) 
   # and the true says `hey, we want to use the objects local x,y&z info,'

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:

# to exit the if statment in python, we simply stop indenting. This promotes clean code.

Thanks for that ! :smiley:

Glad I could help. :slight_smile:

hey allan sorry I didnt saw it, usually people here are like that, I enter on the forum less oftem now, aniway, your way to explain is very good thanks about sharing this with us! I hope you make more tutorials like that, I really dont like python tutorials on video, I have to pause the video everythime that I dont understand something.
Aniway its nice! thanks!