[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pygame-Fenix - Using generators as game objects



On Mon, Dec 1, 2008 at 6:05 PM, Fiona Burrows <fiona@xxxxxxxxxxxxxxxxxxx> wrote:
>
>
> Noah Kantrowitz wrote:
>
> If you want to look up information about this, the general term is
> "actor-based" programming. Existing libraries (neither of which ever seemed
> that great) include PARLEY and Dramatis. Stackless tasklets are also a very
> nice way to handle this.

Panda3d is a lot like this also. This sample code is from their intro
in the manual. They have 2 systems that work together. One is event
driven and the other is task driven (tasks get called after each
screen refresh or so). You then just make little defs that say what to
do at each event or task slice and add them to the caller. Collisions
can be set to trigger events or you can use a task to check the event
que each turn.

This code loads a 3d landscape and a panda bear actor and then makes
the panda walk from side to side and turn around. Then the camera
spins round and round also.

import direct.directbase.DirectStart
from pandac.PandaModules import *

from direct.task import Task
from direct.actor import Actor
from direct.interval.IntervalGlobal import *
import math

#Load the first environment model
environ = loader.loadModel("models/environment")
environ.reparentTo(render)
environ.setScale(0.25,0.25,0.25)
environ.setPos(-8,42,0)

#Task to move the camera
def SpinCameraTask(task):
  angledegrees = task.time * 6.0
  angleradians = angledegrees * (math.pi / 180.0)
  base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),3)
  base.camera.setHpr(angledegrees, 0, 0)
  return Task.cont

taskMgr.add(SpinCameraTask, "SpinCameraTask")

#Load the panda actor, and loop its animation
pandaActor = Actor.Actor("models/panda-model",{"walk":"models/panda-walk4"})
pandaActor.setScale(0.005,0.005,0.005)
pandaActor.reparentTo(render)
pandaActor.loop("walk")

#Create the four lerp intervals needed to walk back and forth
pandaPosInterval1= pandaActor.posInterval(13,Point3(0,-10,0),
startPos=Point3(0,10,0))
pandaPosInterval2= pandaActor.posInterval(13,Point3(0,10,0),
startPos=Point3(0,-10,0))
pandaHprInterval1= pandaActor.hprInterval(3,Point3(180,0,0),
startHpr=Point3(0,0,0))
pandaHprInterval2= pandaActor.hprInterval(3,Point3(0,0,0),
startHpr=Point3(180,0,0))

#Create and play the sequence that coordinates the intervals
pandaPace = Sequence(pandaPosInterval1, pandaHprInterval1,
  pandaPosInterval2, pandaHprInterval2, name = "pandaPace")
pandaPace.loop()

run()


-- 
Douglas E Knapp

http://sf-journey-creations.wikispot.org/Front_Page