Python в игростроении/Blender/GameLogic: различия между версиями

Материал из Викиучебника — открытых книг для открытого мира
Содержимое удалено Содержимое добавлено
Строка 258: Строка 258:


===Game_actuator===
===Game_actuator===
http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_GameActuator.KX_GameActuator-class.html

The game actuator loads a new .blend file, restarts the current .blend file or quits the game.

===Visibility_actuator===
===Visibility_actuator===

Версия от 02:19, 25 января 2006

В учебнике будут представлены примеры контроля LogicBricks с помощью написанных на Питоне скриптов

Основано на документации с оффициального сайта разработчиков

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/index.html


Введение

Принцип работы GameLogic

Сенсоры/Sensors

Keyboard_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_KeyboardSensor.SCA_KeyboardSensor-class.html

import GameLogic

import GameKeys


co = GameLogic.getCurrentController() #Привязываем скрипт к контроллеру "со"

klavishi = co.getSensor('klavishi') #Привязываем сенсор "klavishi" к контроллеру "со"


# status: these should be added to a module somewhere
#Переменные для модуля GameKeys

KX_NO_INPUTSTATUS = 0

KX_JUSTACTIVATED = 1

KX_ACTIVE = 2

KX_JUSTRELEASED = 3


#Получаем список  нажатых клавиш

keylist = klavishi.getPressedKeys()

for key in keylist:

# key[0] == GameKeys.keycode, key[1] = status
#Процедура проверки только что нажатых клавиш

if key[1] == KX_JUSTACTIVATED:

if key[0] == GameKeys.WKEY:
   #Здесь должна быть функция ,выполняемая при нажатии клавиши "W"
if key[0] == GameKeys.SKEY:
   #Здесь должна быть функция ,выполняемая при нажатии клавиши "S"
if key[0] == GameKeys.AKEY:
   #Здесь должна быть функция ,выполняемая при нажатии клавиши "A"
if key[0] == GameKeys.DKEY:
   #Здесь должна быть функция ,выполняемая при нажатии клавиши "D"

Keyboard_sensor_Blender3dGameLogicПример использования

Mouse_sensor

С применением только модуля GameLogic

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_MouseSensor.SCA_MouseSensor-class.html

import GameLogic


posiziya_X = 1 # Переменная для работы с координатой Х -целое число

posizya_Y = 1 # Переменная для работы с координатой Y -целое число


co = GameLogic.getCurrentController() #Привязываем GameLogic к контроллеру "со"

obj_name = co.getOwner() #Привязываем контроллер к объекту с именем "obj_name"

grizun = co.getSensor("grizun") #Привязываем сенсор "grizun" к контроллеру "со"


posizya_X = grizun.getXPosition() #присваиваем переменной значение координаты Х

posizya_Y = grizun.getYPosition() #присваиваем переменной значение координаты Y

С применением модуля Rasterizer

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/Rasterizer-module.html

# To use a mouse movement sensor "Mouse" and a 
# motion actuator to mouse look:

import Rasterizer

import GameLogic

# SCALE-чуствительность мыши

SCALE=[1, 0.5]

co = GameLogic.getCurrentController()

obj = co.getOwner()#связывание контроллера с объектом по имени "obj"

grizun = co.getSensor("grizun")#привязывание сенсора мыши к контроллеру

lmotion = co.getActuator("LMove")# привязывание активатора "lmotion" к контроллеру "co"

wmotion = co.getActuator("WMove")# привязывание активатора "wmotion" к контроллеру "co"

#Запись координат x , y  как только мышь будет перемещена

def mousePos():

 x = (Rasterizer.getWindowWidth()/2 - mouse.getXPosition())*SCALE[0]
 y = (Rasterizer.getWindowHeight()/2 - mouse.getYPosition())*SCALE[1]
     return (x, y)
      

pos = mousePos()

# скорость перемещения для  мыши :X в общих координатах...

lmotion.setTorque(0.0, 0.0, pos[0], False)

# ...Y в общих координатах

wmotion.setTorque(-pos[1], 0.0, 0.0, True)

# запускаем активаторы в работу
GameLogic.addActiveActuator(lmotion, True)
GameLogic.addActiveActuator(wmotion, True)
      
#устанавливаем мышь по центру окна(экрана)

Rasterizer.setMousePosition(Rasterizer.getWindowWidth()/2, Rasterizer.getWindowHeight()/2)

MouseFocus_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_MouseFocusSensor.KX_MouseFocusSensor-class.html

в процессе редактирования...

Определяет события ,когда мышь находится над игровым объектом


#Возвращает стартовую координату луча сенсора в общих координатах

GetRaySource()

(type=list [x, y, z])

#Возвращает конечную координату луча сенсора в общих координатах

GetRayTarget()

(type=list [x, y, z])



#Пример - не готов пока..

import GameLogic

co = GameLogic.getCurrentController()

obj = co.getOwner()

mouse_focus_sens = co.getSensor("mouse_focus_sens")

sens_list=mouse_focus_sens.getRayTarget()

Touch_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_TouchSensor.KX_TouchSensor-class.html#getHitObject

определяет столкновения(коллизии) между объектами

в процессе редактирования...

Collision_sensor

Near_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_NearSensor.KX_NearSensor-class.html

A near sensor is a specialised form of touch sensor.

Radar_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_RadarSensor.KX_RadarSensor-class.html

Radar sensor is a near sensor with a conical sensor object.

Property_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_PropertySensor.SCA_PropertySensor-class.html

Activates when the game object property matches.

Random_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_RandomSensor.SCA_RandomSensor-class.html

This sensor activates randomly.

Ray_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_RaySensor.KX_RaySensor-class.html

A ray sensor detects the first object in a given direction.

Message_sensor

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_NetworkMessageSensor.KX_NetworkMessageSensor-class.html

The Message Sensor logic brick.Currently only loopback (local) networks are supported.

Joystick_sensor

Активаторы/Actuators

Motion_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_ObjectActuator.KX_ObjectActuator-class.html

The object actuator ("Motion Actuator") applies force, torque, displacement, angular displacement, velocity, or angular velocity to an object.

Constraint_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_ConstraintActuator.KX_ConstraintActuator-class.html

A constraint actuator limits the position or orientation of an object.

ipo_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_IpoActuator.KX_IpoActuator-class.html

IPO actuator activates an animation.

Camera_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_CameraActuator.KX_CameraActuator-class.html

Applies changes to a camera.

Sound_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SoundActuator.KX_SoundActuator-class.html

Sound Actuator.

Property_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_PropertyActuator.SCA_PropertyActuator-class.html

Property Actuator

Edit Object_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SCA_AddObjectActuator.KX_SCA_AddObjectActuator-class.html

Edit Object Actuator (in Add Object Mode)

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SCA_EndObjectActuator.KX_SCA_EndObjectActuator-class.html

Edit Object Actuator (in End Object mode)

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SCA_ReplaceMeshActuator.KX_SCA_ReplaceMeshActuator-class.html

Edit Object actuator, in Replace Mesh mode.

Scene_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SceneActuator.KX_SceneActuator-class.html

Scene Actuator logic brick.

Random_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/SCA_RandomActuator.SCA_RandomActuator-class.html

Random Actuator

Message_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_NetworkMessageActuator.KX_NetworkMessageActuator-class.html

NetworkMessageActuator

CD_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_CDActuator.KX_CDActuator-class.html

Game_actuator

http://www.blender3d.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_GameActuator.KX_GameActuator-class.html

The game actuator loads a new .blend file, restarts the current .blend file or quits the game.

Visibility_actuator