Advertisement
Guest User

blender-game-engine-input-controls

a guest
Sep 2nd, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. # Player Object Ref
  2. # http://www.blender.org/api/blender_python_api_2_65_release/bge.types.html#bge.types.KX_GameObject
  3. # More info
  4. # http://www.cgmasters.net/free-tutorials/python-scripting/
  5.  
  6. import bge
  7. from mathutils import Vector
  8. import mytools
  9. import aud
  10.  
  11. device = aud.device()
  12. sndPlrJump = aud.Factory.file(
  13.                 bge.logic.expandPath("//assets/music/smb_jump-small.wav"))
  14.  
  15. p = bge.constraints.getCharacter('player_avatar')
  16.  
  17. cont = bge.logic.getCurrentController()
  18. obj = cont.owner
  19.  
  20. keyboard = bge.logic.keyboard
  21. INPUT_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
  22. INPUT_ACTIVE = bge.logic.KX_INPUT_ACTIVE
  23. INPUT_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
  24. INPUT_NONE = bge.logic.KX_INPUT_NONE
  25.      
  26. # input status ref http://www.blender.org/api/blender_python_api_2_60_6/bge.logic.html
  27. if keyboard.events[bge.events.IKEY] == INPUT_ACTIVATED:
  28.     print('Object', dir(p))
  29.     print('onGround?', p.onGround)
  30.     print('Gravity', p.gravity)
  31.     print('health:', obj['health']) # works
  32.     print('speed:', obj['speed'])
  33.  
  34. if keyboard.events[bge.events.HKEY] == INPUT_ACTIVATED:
  35.     # NOTE TO SELF
  36.     # It may be more efficient to enable/disable the overlay scene
  37.     # rather than each object in it. However, this is a good
  38.     # learnign example for interacting with other scene objects.
  39.    
  40.     scene = mytools.getScene(bge.logic.getSceneList(), 'game_world_level_1')
  41.     player_hud = mytools.getScene(bge.logic.getSceneList(), 'player_hud')
  42.    
  43.     # Example of getting a single objects from a scene
  44.     # player_hud.objects['health_bar_1'].setVisible(toggleVisable())
  45.  
  46.     for obj in player_hud.objects:
  47.         print(obj)
  48.         obj.setVisible(mytools.toggleVisable(obj))
  49.  
  50. if keyboard.events[bge.events.JKEY] == INPUT_ACTIVATED:
  51.     scene = mytools.getScene(bge.logic.getSceneList(), 'game_world_level_1')
  52.     player_hud = mytools.getScene(bge.logic.getSceneList(), 'player_hud')
  53.    
  54.     for obj in player_hud.objects:
  55.         print(obj)
  56.         obj.setVisible(mytools.toggleVisable(obj))
  57.    
  58. sensor = cont.sensors["key_input"]
  59.  
  60. for key,status in sensor.events:  
  61.     if status == bge.logic.KX_INPUT_JUST_ACTIVATED:
  62.        
  63.         if key == bge.events.DKEY:
  64.             if p.walkDirection[0] < 0:
  65.                 p.walkDirection = obj.orientation*Vector((0.0, 0.0, 0.0))
  66.                
  67.             if p.walkDirection[0] < obj['maxSpeed']:
  68.                 p.walkDirection += obj.orientation*Vector((obj['speed'] , 0.0, 0.0))
  69.                 print('moving right', p.walkDirection[0])
  70.            
  71.                                          
  72.         if key == bge.events.AKEY:
  73.             if p.walkDirection[0] > 0:
  74.                 p.walkDirection = obj.orientation*Vector((0.0, 0.0, 0.0))
  75.                
  76.             if p.walkDirection[0] > -obj['maxSpeed']:
  77.                 p.walkDirection += obj.orientation*Vector((-obj['speed'] , 0.0, 0.0))
  78.                 print('moving left', p.walkDirection[0])
  79.                
  80.         if key == bge.events.SPACEKEY:
  81.             print('moving jump')      
  82.             p.jump()
  83.             sndPlrJump = sndPlrJump.pitch(1)
  84.             device.play(sndPlrJump)
  85.             #obj['health'] += -1
  86.  
  87.     if status == bge.logic.KX_INPUT_JUST_RELEASED:
  88.         if key == bge.events.AKEY:
  89.             if p.walkDirection[0] < 0:
  90.                 p.walkDirection = obj.orientation*Vector((0.0, 0.0, 0.0))
  91.  
  92.         if key == bge.events.DKEY:
  93.             if p.walkDirection[0] > 0:
  94.                 p.walkDirection = obj.orientation*Vector((0.0, 0.0, 0.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement