|
PyGame Sound Looping in Blender
Difficulty: Intermediate
Blender Version: Ultimate Blender
Overview
This example would show you how to randomize starting positions of objects
and also create a simple efficient AI "PredatorPrey" model, where
the predator will seek out all the prey.
Download the File here
->>PredatorPrey1.0.blend<<-
Basically when you start it up, it'll look like this. Sorry guys I'm not an
artist by trade or nature, lol so it's bound to look a little less
appealing. =) Anyone's free to give me a message and help me with the art
to make this tutorial at least look better artwise.
You can see below that the prey (yellow cubes) don't always spawn at the
same place. Hehe, that's the beauty of randomization (very useful for many
things actually).
 
Tutorial
Initialize

To not deal with execution priorities, I chose to have another scene
declare the global variables that were needed. Take a look at the script
"Positions"
Positions
---------------------------------------------------------------------------------
import GameLogic as g
import random
#All opening positions
##Note: [0,0] is taken out because that is the starting position of the Predator
g.pos = [[-4,3],[-3,3],[-2,3],[-1,3],[0,3],[1,3],[2,3],[3,3],[4,3],\
[-4,2],[-3,2],[-2,2],[-1,2],[0,2],[1,2],[2,2],[3,2],[4,2],\
[-4,1],[-3,1],[-2,1],[-1,1],[0,1],[1,1],[2,1],[3,1],[4,1],\
[-4,0],[-3,0],[-2,0],[-1,0],[1,0],[2,0],[3,0],[4,0],\
[-4,-1],[-3,-1],[-2,-1],[-1,-1],[0,-1],[1,-1],[2,-1],[3,-1],[4,-1],\
[-4,-2],[-3,-2],[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2],[3,-2],[4,-2],\
[-4,-3],[-3,-3],[-2,-3],[-1,-3],[0,-3],[1,-3],[2,-3],[3,-3],[4,-3]]
#This shuffles the position array
random.shuffle(g.pos)
g.num = 0
---------------------------------------------------------------------------------
Here g.pos declares all the given coordinates that are within the camera.
These are the avaliable places that the prey (yellow cubes) can be placed
at. random.shuffle(g.pos) shuffles the entire g.pos array. g.num is
basically used to increment the array g.pos later on.
The scene Initialize only lasts for .1 seconds and then it removes itself
and adds "Scene".
Initialize
PreySetPos
---------------------------------------------------------------------------------
import GameLogic as g
c = g.getCurrentController()
o = c.getOwner()
#set's the positon of the cube (prey)
o.setPosition([g.pos[g.num][0],g.pos[g.num][1],0])
#increments g.num so that the next object that calls setPosition will
#have its position set to the next value in the g.pos array
g.num += 1
---------------------------------------------------------------------------------

First up is the random placement of the prey. The prey are all located on a
place on top of the camera, conviently out of view. Since there's a blender
bug in this current version where you can't detect added objects, all of
the prey that's used will be in the 1st layer. Each of them calls a the
"PreySetPos" script which automatically set's the prey calling
the script to a position in g.pos. None of the preys will occupy the same
space because after each time a prey is placed, g.num is incremented and
the next spot on the g.pos array will be used for the next prey that calls
the script.
Now for the predator script
Predator
---------------------------------------------------------------------------------
import GameLogic as g
from math import*
c = g.getCurrentController()
o = c.getOwner()
ate = c.getSensor("ateIsTrue")
detect = c.getSensor("detectPrey")
track = c.getActuator("TrackTo")
#Only run when ate is positive
##Because we don't want the predator to keep changing targets before he
##eats his prey
if ate.isPositive():
#get's the whole list of preys
preys = detect.getHitObjectList()
#get's number of preys there are
preyAlive = len(preys)
#get's current position of predator
oPos = o.getPosition()
#set's the preyDist, make a number higher than your furthest distance
preyDist = 100.0
targetPrey = "None"
#this for loop will allow you to run through the total number of preys
for i in range(preyAlive):
##get's prey's position
pPos = preys[i].getPosition()
##Apply the distance formula between prey and predator
xDiff = oPos[0]-pPos[0]
yDiff = oPos[1]-pPos[1]
zDiff = oPos[2]-pPos[2]
dist = sqrt(xDiff*xDiff+yDiff*yDiff+zDiff*zDiff)
##set's preyDist to the closest prey and set's that prey as target
if dist < preyDist:
preyDist = dist
targetPrey = preys[i]
track.setObject(targetPrey)
g.addActiveActuator(track,1)
##set o.ate to 0 since predator is now tracking new target but hasn't
##eaten it yet
o.ate = 0
---------------------------------------------------------------------------------
Well the comments within the code basically explains everything fairly
well. I'll clarify the thinking behind it a little more here.

On the logic bricks shown there is a bool property named "ate".
This basically will see if the prey that we have targeted is eaten or not.
When a prey is eaten the Predator script is allowed to run through all its
code again to find the next avaliable target (within the script, when it is
run "ate" would be set back to false again). So basically if the
creature hasn't eaten the prey it will continue to track it and when it eats
it, "ate" becomes true causing the predator script to run and
starts to track new prey and sets "ate" back to false.
The 2 key things that makes this simple AI work is blender's built in near
sensor and the distance formula. getHitObjectList() returns an array of
objects that are detected within range. In the script I cycle through all
the objects detected and compare their distances with the Predator. Using
this simple if statement in the loop
---------------------------------------------------------------------------------
if dist < preyDist:
preyDist = dist
targetPrey = preys[i]
---------------------------------------------------------------------------------
The predator is set to track the closest prey possible. =) everyone's lazy
even animals, if you can kill faster or closer it's the desired thing. Lol
unless that animal's out for revenge.
So this basically wraps the PredatorPrey model up. Hope you got something
valuable out of all this.
Jason Lin
|