Basic Movement Code, need help

I am just taking my first steps into the blender game engine and coding.
I’ve created this simple script which should, set the keyboard sensor to accept the uparrow as it’s trigger key, and when it is pushed my cube should move 0.2 in the y-direction.

My logic brick setup is like these:

Keyboard sensor (named Keyboard) - python controller - motion actuator which is set to .2 in y-direction.

But things won’t work, anyone that might help??

Thanks

import bge

def main():
    cont = bge.logic.getCurrentController()
    
    sens = cont.sensors["Keyboard"]
    actu = cont.actuators["Motion"]
    
    sens.key = bge.events.UPARROWKEY
    
    if sens.positive:
        cont.activate(actu)
    else:
        cont.deactivate(actu)

        
main()

Well there are actually many things wrong with this script - and I’m willing to go through and teach you what you are missing.

First of all - the script doesn’t know what ‘events’ is inless you do:

import events

And you don’t need a keyboard sensor. Just delete that and put an always sensor attached to it with it’s pulse on ‘always’.

define you’re up uparrowkey as:

UPARROWKEY = logic.keyboard.events[events.UPARROWKEY]

Now keys are either pressed, held, or let go.
This is either when UPARROWKEY = 1, is just pressed, UPARROWKEY = 2, is held, and finally UPARROWKEY = 3 is let go.

But hey - why go through all this when I can just share with you my script - and you can make it what you want!

import bge
from bge import logic
from bge import events

cont = logic.getCurrentController()
obj = cont.owner

collision = cont.sensors["Collision"]

key = logic.keyboard.events

WKEY = key[events.WKEY]
AKEY = key[events.AKEY]
SKEY = key[events.SKEY]
DKEY = key[events.DKEY]
SPACEKEY = key[events.SPACEKEY]
SHIFTKEY = key[events.LEFTSHIFTKEY]

yMove = 0.0
xMove = 0.0
xForce = 0.0
yForce = 0.0

if AKEY == 2:
    xMove = -0.1
    xForce = -300
    if SHIFTKEY == 2:
        xMove = -0.05
        xForce = -150
if DKEY == 2:
    xMove = 0.1
    xForce = 300
    if SHIFTKEY == 2:
        xMove = 0.05
        xForce = 150

if WKEY == 2:
    yMove = 0.1
    yForce = 300
    if SHIFTKEY == 2:
        yMove = 0.05
        yForce = 150
if SKEY == 2:
    yMove = -0.1
    yForce = -300
    if SHIFTKEY == 2:
        yMove = -0.05
        yForce = -150

obj.applyMovement([xMove, yMove, 0.0], 1)

if SPACEKEY == 1 and collision.positive:
    obj.applyForce([xForce, yForce, 300], 1)

yMove = 0.0
xMove = 0.0
xForce = 0.0
yForce = 0.0

WASD move around, space jump, left shift slow down…
All you need is a keyboard sensor set to ‘anykey’ and also put pulse mode on ‘always’, and have a collision sensor. No actuators needed.

this is amazing lol, sorry to intrude persay =p would it be as easy to script to rotate? my character is stuck facing forever forwards hehe, ive been experimenting trying to rotate him, i just suck at coding lol, well thanks and it works very well :stuck_out_tongue:

Similar, just use the same structure but use applyRotation
The best resource for Blender Game Python in the world is the API, a list of every possible function in the game engine.
Go have a look at the game engine modules

thank you so much, and thats exactly what ive been trying i added applyRotation on x and y and added it do i switch out force or move or just add the line? lol idk thanks again for the very handy link!