=============== Object Oriented =============== .. _object_oriented: .. index:: BGE: object oriented .. |a| image:: /images/all/blender-game-engine/arrows.jpg :width: 100px Objectives ========== .. tabularcolumns:: | c | l | :border: none +-------+-------------------------------------------------------------------------+ | |a| |In this part you learn how to write a little game for the game engine | | |which is splitted in many scripts, which are executed every frame. | +-------+-------------------------------------------------------------------------+ Instructions ============ :Tasks: 1. Download the .blend file from :download:`here ` 2. Add functions Preparations ============ 1. Open the the .blend file in Blender 2. Change the *Scene layout* to »Game Logic« .. image:: /images/all/blender-game-engine/change_scene_layout.png Running scripts for an object ============================= If you want to run a script for a Blender-Object, you can use the *logic bricks* to do that. First you have to add an »Always« sensor. Click to the left ''', for executing the script every frame. .. image:: /images/all/blender-game-engine/add_sensor.png Then add an »Python« controller and set the *Script* Property to your script. .. image:: /images/all/blender-game-engine/add_controller.png Then connect them. .. image:: /images/all/blender-game-engine/connect_logic_bricks.png The scripts =========== The script for our Player (The monkey named »Player«). .. literalinclude:: files/player.py :language: python And the Script for our Enemys: .. literalinclude:: files/enemy.py :language: python The camera ========== Connecting the camera to the player: 1. Deselect all (»A« toggles selection) 2. Select the camera (with a right click) 3. Press shift while selecting the player, so you selected both and the player is active 4. Press »Ctrl« + »P« and click on »Object« .. image:: /images/all/blender-game-engine/set_parent.png 5. Select the camera again and enable »Slow Parent« in the *Relations Extras* 6. Set the *Offset* to »11.5« .. image:: /images/all/blender-game-engine/set_slow_parent.png More about Blender and the Game Enigne under `Blender Game Engine -- API `_