Added Stage class for tying together Sprites, Actors, Collages, and
SurfaceSets into a single unit.
class Collage:
tiles = None
- position = (0,0)
displaylist = None
def __init__(self, file):
glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
glDrawArrays(GL_QUADS, 0, nvertices)
glEndList()
-
+
def draw(self):
- glPushMatrix()
- glTranslatef(self.position[0], self.position[1], 0.0)
glCallList(self.displaylist)
- glPopMatrix()
+from Collage import *
+from Surface import *
+
+class Layer:
+ """Container for layer data.
+
+ collage - A Collage object defining the visuals of the layer
+ scale - A float value for the size of the background layer
+ parallax - A float value specifying the relative scroll speed (where
+ 1.0 is typically the playfield scroll speed, >1.0 is faster,
+ and <1.0 is slower)"""
+
+ def __init__(self, collage, scale=1.0, parallax=1.0):
+ if collage:
+ self.collage = Collage(collage)
+ self.scale = scale
+ self.parallax = parallax
+ self.position = (0, 0)
+ self.things = []
+
+
+ def add(self, thing):
+ self.things.append(thing)
+
+
+ def moveTo(self, x, y):
+ self.position = (x * self.parallax, y * self.parallax)
+
+
+ def draw(self):
+ glPushMatrix()
+ if self.scale != 1.0:
+ glScalef(self.scale, self.scale, self.scale)
+ glTranslatef(-self.position[0], -self.position[1], 0)
+ self.collage.draw()
+ for t in self.things:
+ t.draw()
+ glPopMatrix()
+
+
+class Stage:
+ layers = None
+ surfaces = None
+ drawsurfaces = False
+ position = None
+
+ def __init__(self):
+ self.layers = []
+ self.position = (0, 0)
+
+
+ def addLayerBack(self, layer):
+ self.layers.insert(0, layer)
+
+
+ def addLayerFront(self, layer):
+ self.layers.append(layer)
+
+
+ def setSurfaces(self, datafile):
+ self.surfaces = SurfaceSet(datafile)
+
+
+ def drawSurfaces(self):
+ self.drawsurfaces = True
+
+
+ def hideSurfaces(self):
+ self.drawsurfaces = False
+
+
+ def toggleDrawSurfaces(self):
+ self.drawsurfaces = not self.drawsurfaces
+
+
+ def moveTo(self, x, y):
+ self.position = [x,y]
+ for l in self.layers:
+ l.moveTo(x, y)
+
+
+ def draw(self):
+ for l in self.layers:
+ l.draw()
+ if self.drawsurfaces:
+ glPushMatrix()
+ glTranslate(-self.position[0], -self.position[1], 0)
+ self.surfaces.draw()
+ glPopMatrix()
solid 4 1 5 5
solid 6 1 7 3
+tile 10 0
+tile 12 0
+tile 13 0
+tile 14 0
+tile 15 0
+tile 16 0
+tile 17 0
+tile 18 0
+
+tile 12 1
+tile 13 1
+tile 14 1
+tile 14 2
+
+solid 10 0 18 1
+surface 12 1 13 2
+solid 13 1 15 2
+solid 14 2 15 3
+
tilesize 1 1
texture img/ByteIco.png
tile 263 263
from Collage import *
from Surface import *
from Actor import *
+from Stage import *
import Engine
#resolution = (1280,1024)
pygame.display.set_caption("platformtest")
-ground = Collage('data/example1')
-surfaces = SurfaceSet('data/example1')
-stars = Collage('data/stars')
+stage = Stage()
+main = Layer('data/example1')
+stage.addLayerFront(main)
+stage.addLayerBack(Layer('data/stars', 1.0, 0.25))
+stage.setSurfaces('data/example1')
+
candelabra = Sprite(['Sprites/candelabra_short/cand_s_1.png','Sprites/candelabra_short/cand_s_2.png','Sprites/candelabra_short/cand_s_3.png','Sprites/candelabra_short/cand_s_4.png'])
candelabra.setPosition(64, 64)
candelabra.setScale(2.0)
candelabra.setFramerate(10.0)
candelabra.play()
+main.add(candelabra)
+
class Richter(Actor):
def __init__(self, surfaces=None):
Actor.__init__(self, surfaces)
self.grounded = self.collideDelta((0,-1))
-richter = Richter(surfaces)
+richter = Richter(stage.surfaces)
richter.setPosition(32, 64)
-
-
-class gamestate:
- drawsurface = False
+main.add(richter)
def input(e):
elif e.key == pygame.K_UP:
richter.jump()
elif e.key == pygame.K_1:
- gamestate.drawsurface = not gamestate.drawsurface
+ stage.toggleDrawSurfaces()
elif e.type == pygame.KEYUP:
if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
richter.idle()
richter.update()
if richter.position[1] < 0:
richter.position = [richter.position[0],480]
+ stage.moveTo(richter.position[0] - resolution[0]/2, richter.position[1] - resolution[1]/3)
-def draw():
- stars.draw()
- ground.draw()
- candelabra.draw()
- richter.draw()
- if gamestate.drawsurface:
- surfaces.draw()
n = 0
def fpsthing():
print fps
n = 0
-Engine.engine([input],[update,fpsthing],[draw])
+Engine.engine(
+ [input],
+ [update, fpsthing],
+ [stage.draw]
+)