In this series of PDF tutorials I will show you, from start to finish how to make a fairly complex game in the Blender game engine. You will need to have a few Blender skills such as basic modelling, but I do give lots of info about every other aspect of making the game.
At first your will be “plugging in” pre-written scripts to get your objects to perform correctly, but later I will be showing how to write your own Python scritps for the BGE.
I will be covering many aspects including basic A.I. game objectives, multiple missions, switching meshes and using dictionaries to store information. You will learn how to save and load games, as well as how to add a HUD and mini map to your game.
The tutorials are as follows:
- Making a game model using projected texture baking.
- Basic player movement for a helicopter.
- Weapons management, including enemy damage and player ammo.
- Coming soon.
Here’s the first tutorial:
BGE Tutorial Part One.pdf (1010 KB)
and here is the second:
BGE tutorial 2.pdf (880 KB)
Here’s the Blend file to go with Tutorial 2.
And the python script. Copy and paste it to your blend file and name it cobra.py
####################################################
### imports
import bge
####################################################
### get window info
def window_info():
y= bge.render.getWindowHeight() *0.5
x= bge.render.getWindowWidth() *0.5
return (y,x)
####################################################
### stop roll and pitch exceeding maximum
def max_value(own):
max_setting = 80
if own['pitch'] > max_setting:
own['pitch'] = max_setting
if own['pitch'] < -max_setting:
own['pitch'] = -max_setting
if own['roll'] > max_setting:
own['roll'] = max_setting
if own['roll'] < -max_setting:
own['roll'] = -max_setting
####################################################
### apply movement and rotation
def movement(own,cont,cobra_motion,cobra_rotation):
move_sense = 0.2
rotation_sense = 0.00002
location = own['pitch'] * move_sense
roll = own['roll'] * -move_sense
rotation = own['yaw'] * rotation_sense
cobra_rotation.dRot = [0.0, 0.0 , rotation]
cont.activate(cobra_rotation)
cobra_motion.linV= [roll, location , 0.0]
cont.activate(cobra_motion)
####################################################
### main control script
def cobra_controls(cont):
################################################
### set own object, sensors and actuators
own = cont.owner
m_move = cont.sensors['m_move'] # mouse pointer movement
middle_m = cont.sensors['middle_m'] # middle button, used to toggle strafe
cobra_motion = cont.actuators['cobra_motion'] # moves the chopper
cobra_rotation = cont.actuators['cobra_rotation'] # rotates the chopper
tilting = cont.actuators['tilting'] # visual tilt of the chopper
rolling = cont.actuators['rolling'] # visual roll of the chopper
################################################
### get window info and mouse position
### return mouse pointer to the center
window = window_info() ### this calls up info from our first function
y = window[0]
x = window[1]
pos = m_move.position ### this is the position of the mouse
bge.render.setMousePosition(int(x),int(y)) ### we need to reset the mouse pointer now
################################################
### here we set some local properties
### you could set these on the object
### properties panel too for more control
min_sens = 2 # dead zone of mouse movement
p_adjust = y-pos[1] # how far the mouse pointer has shifted from 0 on the y axis
r_adjust = x-pos[0] # how far the mouse pointer has shifted from 0 on the y axis
### how quickly the mouse moves the player object
pitch_rate = 0.2
roll_rate = 0.2
yaw_rate = 1.2
### how quickly it returns to a neutral orientation
pitch_reset = 0.001
roll_reset = 0.02
yaw_reset = 0.02
### a misc value
twitch = 0.05
################################################
### here we use the mouse input to set properties
### which will drive motion and rotation
### as well as showing in game as aircraft
### tilt and roll
if middle_m.positive: # if we want to strafe from side to side and reset other movement
if abs(r_adjust) < min_sens: # if no x axis movement of mouse
own['roll'] -= own['roll'] * roll_reset # -= means reduce property by this much
else:
own['roll'] += r_adjust * roll_rate # += means increase by this amount
own['yaw'] -= own['yaw'] * twitch
own['pitch'] -= own['pitch'] * twitch
else: # normal movement profile
if abs(r_adjust) < min_sens: # if no x axis movement of mouse
own['yaw'] -= own['yaw'] * yaw_reset
own['roll'] -= own['roll'] * roll_reset
else:
own['yaw'] += r_adjust * yaw_rate
own['roll'] += r_adjust * twitch # if not strafing, we want to turn but also roll a little
if abs(p_adjust) < min_sens: # if the mouse is not moving on the y axis
own['pitch'] -= own['pitch'] * pitch_reset
else:
own['pitch'] += p_adjust *pitch_rate
################################################
### here we use a function to stop pitch and roll from exceeding maximum value
max_value(own)
################################################
### this is the function to apply movement
movement(own,cont,cobra_motion,cobra_rotation)
################################################
### these are functions to adjust the pitch and roll of the model using empties
tilter = tilting.owner
tilter['tilt'] = own['pitch']
cont.activate(tilting)
roller = rolling.owner
roller['roll'] = -own['roll']
cont.activate(rolling)
################################################
### This is a short script for making the camera follow the player
def cam_loc(cont):
own = cont.owner
if own['player_ob']:
### make the camera parent locked on to the player
own.worldPosition= own['player_ob'].worldPosition
else:
scene = bge.logic.getCurrentScene()
### from a list of objects in the scene find the player and make a link to it using a property
player = [ob for ob in scene.objects if ob.get("player",False) ==True][0]
if player:
own['player_ob'] = player