Page 4

ISSUE 5: A New Hope

It's time for some housekeeping before the New Year!  We're going to revamp our code base with a new design, explain the water from Issue 4, add some sound effects and finish with a basic Heads Up Display (HUD).
  • Delicious
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

newgamefolder/volume1/game.py

from panda3d.core import AmbientLight, DirectionalLight, Vec4, Fog
from panda3d.core import Texture, TextureStage
from pandac.PandaModules import CompassEffect
from pandac.PandaModules import VBase4, TransparencyAttrib
from direct.interval.LerpInterval import LerpTexOffsetInterval, LerpPosInterval
 
class GameWorld():
    def __init__(self,size,loader,scenegraph,camera):
        self.worldsize = size
        self.loader = loader
        self.render = scenegraph
        self.camera = camera
        self.world = self.loader.loadModel("volume1/models/world.bam")
        # the model is 1024 already, so we scale accordingly:
        self.world.setScale(self.worldsize/1024)
        self.world.setPos(0,0,0)
        self.world.reparentTo(self.render)
        self.__createEnvironment()
 
    # private method
    def __createEnvironment(self):
        # Fog
        expfog = Fog("scene-wide-fog")
        expfog.setColor(0.5,0.5,0.5)
        expfog.setExpDensity(0.002)
        self.render.setFog(expfog)
 
        # Our sky
        skysphere = self.loader.loadModel('volume1/models/blue-sky-sphere')
        skysphere.setEffect(CompassEffect.make(self.render))
        skysphere.setScale(0.08)
        # NOT render or you'll fly through the sky!:
        skysphere.reparentTo(self.camera) 
 
        # Our lighting
        ambientLight = AmbientLight("ambientLight")
        ambientLight.setColor(Vec4(.6, .6, .6, 1))
        self.render.setLight(self.render.attachNewNode(ambientLight))
 
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setColor(VBase4(0.8, 0.8, 0.5, 1))
        dlnp = self.render.attachNewNode(directionalLight)
        dlnp.setPos(0,0,260)
        dlnp.setHpr(225,-60,0)#lookAt(self.player)
        self.render.setLight(dlnp)
 
        # water
        self.water = self.loader.loadModel('volume1/models/square.egg')
        self.water.setSx(self.worldsize* 2)
        self.water.setSy(self.worldsize*2)
        self.water.setPos(self.worldsize/2,self.worldsize/2,18) # sea level
        self.water.setTransparency(TransparencyAttrib.MAlpha) 
        nTS = TextureStage('1')
        self.water.setTexture(nTS,self.loader.loadTexture('volume1/models/water.png'))
        self.water.setTexScale(nTS,4)
        self.water.reparentTo(self.render)
        LerpTexOffsetInterval(self.water,200,(1,0),(0,0),textureStage=nTS).loop()
 
    def setGroundMask(self,mask):
        self.world.setCollideMask(mask)
 
    def setWaterMask(self,mask):
        self.water.setCollideMask(mask)
 
    def getSize(self):
        return self.worldsize

This class is pretty much the replacement to our old MyApp class. It contains all of the game logic and makes use of the new classes we just discussed. Hopefully, it makes sense and you can see how the other classes (and their methods) are now used to fulfill the design. The second piece of our ‘rapid object collision’ solution is in this code too. As well as the setFluid methods we used in player.py, we also here make a call to ‘setRespectPrevTransform(True)’ on our collision traverser. This tells Panda3D to record previous frame positions. You only need to call this once in your code and then use the setFluid methods thereafter.

newgamefolder/main.py

someVar = GameWorld()
someVar.__createEnvironment() # would NOT be allowed

Think of main.py as your old mygame.py. It is the start of the game, what gets the ball rolling. Instead of now running ‘python mygame.py’ (or ‘ppython mygame.py’) you execute main.py instead. The game should now run if you try! If not, you may have missed something! When you get python errors at the command prompt – read them backwards. Python presents you with what is called a stack trace, the most meaningful error is usually the one spat out last!

Quick Nav: Previous page | Next page |

Index: page 1 | page 2 | page 3 | page 4 | page 5 |
All: View full Issue on one Page