Moved Richter engine bits into their own directory
-from Sprite import *
-
-class Actor:
- def __init__(self, surfaces=None):
- self.states = {}
- self.currentstate = None
- self.surfaces = surfaces
- self.position = [0,0]
- self.vgravity = 0
-
-
- def defineState(self, statename, sprite):
- self.states[statename] = sprite
-
-
- def setState(self, statename):
- self.currentstate = statename
- self.states[self.currentstate].reset()
- self.states[self.currentstate].play()
-
-
- def setPosition(self, x, y):
- self.position = [x,y]
-
-
- def collideDelta(self, delta):
- if not self.surfaces:
- return False
- s = self.states[self.currentstate]
- # Don't panic. We don't care about the sprite's position since
- # we keep track of it ourself.
- s.setPosition(self.position[0], self.position[1])
- s.hitbox.move_ip(delta[0], delta[1])
- if self.surfaces.collideRect(s.hitbox):
- return True
- else:
- return False
-
-
- def moveDelta(self, delta):
- collided = False
- s = self.states[self.currentstate]
- s.setPosition(self.position[0], self.position[1])
- d = [0,0]
- if self.surfaces.collideRect(s.hitbox) or (delta[0] == 0 and delta[1] == 0):
- return (True, (0,0))
- if abs(delta[0]) > abs(delta[1]):
- dx = delta[0] / abs(delta[0])
- dy = float(delta[1]) / float(abs(delta[0]))
- xrange = abs(int(delta[0]))
- while xrange > 0:
- d[0] += dx
- d[1] += dy
- nrect = s.hitbox.move(d[0],d[1])
- if self.surfaces.collideRect(nrect):
- # Slide along surfaces if possible
- nrect = s.hitbox.move(d[0],d[1]-dy)
- if not self.surfaces.collideRect(nrect):
- d[1] -= dy
- xrange -= 1
- continue
- nrect = s.hitbox.move(d[0]-dx,d[1])
- if dy != 0 and not self.surfaces.collideRect(nrect):
- d[0] -= dx
- xrange -= 1
- continue
- d[0] -= dx
- d[1] -= dy
- collided = True
- break
- xrange -= 1
- d[1] = int(d[1])
- else:
- dy = delta[1] / abs(delta[1])
- dx = float(delta[0]) / float(abs(delta[1]))
- yrange = abs(int(delta[1]))
- while yrange > 0:
- d[1] += dy
- d[0] += dx
- nrect = s.hitbox.move(d[0],d[1])
- if self.surfaces.collideRect(nrect):
- # Slide along surfaces if possible
- nrect = s.hitbox.move(d[0]-dx,d[1])
- if not self.surfaces.collideRect(nrect):
- d[0] -= dx
- yrange -= 1
- continue
- nrect = s.hitbox.move(d[0],d[1]-dy)
- if dx != 0 and not self.surfaces.collideRect(nrect):
- d[1] -= dy
- yrange -= 1
- continue
- d[1] -= dy
- d[0] -= dx
- collided = True
- break
- yrange -= 1
- d[0] = int(d[0])
- return (collided, d)
-
-
- def move(self, delta):
- (collided, delta) = self.moveDelta(delta)
- self.position[0] += delta[0]
- self.position[1] += delta[1]
- return collided
-
-
- def update(self):
- "Override this function. Called once per frame."
- pass
-
-
- def draw(self):
- self.states[self.currentstate].drawAt(self.position)
-import pygame
-
-class Camera:
- stage = None
- resolution = None
- focus = None
- camerapos = None
- bbox = None
-
- def __init__(self, stage):
- self.stage = stage
- self.resolution = pygame.display.get_surface().get_size()
- self.camerapos = [0,0]
-
-
- def setFocus(self, obj):
- self.focus = obj
-
-
- def clearFocus(self):
- self.focus = None
-
-
- def setPosition(self, x, y):
- self.camerapos = [x, y]
-
-
- def setBbox(self, bbox):
- self.bbox = bbox
-
-
- def clearBbox(self):
- self.bbox = None
-
-
- def update(self):
- if self.focus:
- self.camerapos = [self.focus.position[0] - self.resolution[0]/2,
- self.focus.position[1] - self.resolution[1]/4]
-
- if self.bbox:
- # This is a wee bit confusing since Rects assume (0,0) in the
- # upper left, but we want it in the lower left. Tops and
- # bottoms are reversed.
- if self.bbox.height < self.resolution[1] or self.camerapos[1] < self.bbox.top:
- self.camerapos[1] = self.bbox.top
- elif self.camerapos[1] + self.resolution[1] > self.bbox.bottom:
- self.camerapos[1] = self.bbox.bottom - self.resolution[1]
-
- if self.bbox.width < self.resolution[0] or self.camerapos[0] < self.bbox.left:
- self.camerapos[0] = self.bbox.left
- elif self.camerapos[0] + self.resolution[0] > self.bbox.right:
- self.camerapos[0] = self.bbox.right - self.resolution[0]
-
- self.stage.moveTo(self.camerapos[0], self.camerapos[1])
-from Sprite import Sprite
-from OpenGL.GL import *
-from Numeric import array
-import pygame
-
-import TexMan
-
-class Collage:
- tiles = None
- displaylist = None
- bbox = None
-
- def __init__(self, file):
- self.parse(file)
-
-
- def parse(self, file):
- if self.displaylist == None:
- self.displaylist = glGenLists(1)
- glNewList(self.displaylist, GL_COMPILE)
- glEnable(GL_TEXTURE_2D)
- glColor3f(1.0,1.0,1.0)
- ctexture = None
- ctexsize = None
- vertexarray = []
- texcoordarray = []
- nvertices = 0
- #maxvertices = glGetIntegerv(GL_MAX_ELEMENTS_VERTICES)
- #maxindexes = glGetIntegerv(GL_MAX_ELEMENTS_INDICES)
-
- self.tiles = []
- mf = (1,1)
- f = open(file,'r')
- for line in f:
- args = line.split()
- if not args:
- continue
- cmd = args[0]
- args = args[1:]
- if cmd == 'tilesize':
- mf = (int(args[0]),int(args[1]))
- elif cmd == 'bbox':
- self.bbox = pygame.Rect(0, 0, mf[0] * int(args[0]), mf[1] * int(args[1]))
- elif cmd == 'texture':
- if nvertices > 0: # dump vertex array
- glVertexPointer(3, GL_FLOAT, 0, array(vertexarray,'f'))
- glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
- glDrawArrays(GL_QUADS, 0, nvertices)
- vertexarray = []
- texcoordarray = []
- nvertices = 0
- d = TexMan.load(args[0])
- ctexture = d['texture']
- ctexsize = d['tsize']
- glBindTexture(GL_TEXTURE_2D, ctexture)
- elif cmd == 'tile':
- x = int(args[0]) * mf[0]
- y = int(args[1]) * mf[1]
- vertexarray += [
- (x,y,0),
- (x+ctexsize[0],y,0),
- (x+ctexsize[0],y+ctexsize[1],0),
- (x,y+ctexsize[1],0)
- ]
- texcoordarray += [
- (1.0,0.0),(1.0,1.0),(0.0,1.0),(0.0,0.0)
- ]
- nvertices += 4
- f.close()
- # Finally,
- glVertexPointer(3, GL_FLOAT, 0, array(vertexarray,'f'))
- glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
- glDrawArrays(GL_QUADS, 0, nvertices)
- glEndList()
-
-
- def draw(self):
- glCallList(self.displaylist)
-import sys
-import imp
-import pygame
-from pygame.locals import *
-from Sprite import Sprite
-from Numeric import array,reshape
-
-try:
- from OpenGL.GL import *
- from OpenGL.GLU import *
-except:
- print 'sprite_engine requires PyOpenGL'
- raise SystemExit
-
-import config
-
-fpsman = pygame.time.Clock()
-
-def init(resolution=(640,480)):
- pygame.init()
- try:
- sres = [int(n * config.scalefactor) for n in resolution]
- except AttributeError:
- sres = resolution
- flags = OPENGL|DOUBLEBUF|HWSURFACE
- try:
- if config.fullscreen:
- flags |= FULLSCREEN
- except AttributeError:
- pass
- pygame.display.set_mode(sres, flags)
-
- # Set up the camera
- glLoadIdentity()
- glMatrixMode(GL_PROJECTION)
- #gluOrtho2D(0,resolution[0],0,resolution[1])
- # calculate Z-depth from average resolution
- zdepth = sum(resolution) / 2
- glFrustum(0,resolution[0],0,resolution[1],zdepth,zdepth * 10)
- glTranslate(0,0,-zdepth)
-
- #glTranslate(resolution[0]/2, resolution[1]/2, 0)
- #glTranslate(0,0,-zdepth)
- #glRotate(30, 0, 1, 0)
- #glTranslate(-resolution[0]/2, -resolution[1]/2, 0)
-
- # And initialize things how we like in GL
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- glEnable(GL_TEXTURE_2D)
- glEnable(GL_BLEND)
- glDisable(GL_DITHER)
- glShadeModel(GL_FLAT)
- glEnable(GL_CULL_FACE)
- #glDepthFunc(GL_LEQUAL)
- #glEnable(GL_DEPTH_TEST)
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST)
- glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST)
- glEnableClientState(GL_VERTEX_ARRAY)
- glEnableClientState(GL_TEXTURE_COORD_ARRAY)
-
-
-running = True
-level = None
-eventhandlers = []
-updaters = []
-drawers = []
-
-
-def loadLevel(name):
- global level
- unloadLevel()
- fp, pathname, description = imp.find_module(name)
- level = imp.load_module(name, fp, pathname, description)
- updaters.insert(0, level.update)
- drawers.insert(0, level.draw)
- return level
-
-
-def unloadLevel():
- global level
- if level:
- updaters.remove(level.update)
- drawers.remove(level.draw)
- level = None
-
-
-def stop():
- global running
- running = False
-
-
-def run():
- global running
- running = True
- while running:
- events = pygame.event.get()
- for e in events:
- for h in eventhandlers:
- h(e)
-
- #clear screen and move camera
- #glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- glClear(GL_COLOR_BUFFER_BIT)
-
- for update in updaters:
- update()
-
- for draw in drawers:
- draw()
-
- pygame.display.flip()
- fpsman.tick(60)
+from Sprite import *
+
+class Actor:
+ def __init__(self, surfaces=None):
+ self.states = {}
+ self.currentstate = None
+ self.surfaces = surfaces
+ self.position = [0,0]
+ self.vgravity = 0
+
+
+ def defineState(self, statename, sprite):
+ self.states[statename] = sprite
+
+
+ def setState(self, statename):
+ self.currentstate = statename
+ self.states[self.currentstate].reset()
+ self.states[self.currentstate].play()
+
+
+ def setPosition(self, x, y):
+ self.position = [x,y]
+
+
+ def collideDelta(self, delta):
+ if not self.surfaces:
+ return False
+ s = self.states[self.currentstate]
+ # Don't panic. We don't care about the sprite's position since
+ # we keep track of it ourself.
+ s.setPosition(self.position[0], self.position[1])
+ s.hitbox.move_ip(delta[0], delta[1])
+ if self.surfaces.collideRect(s.hitbox):
+ return True
+ else:
+ return False
+
+
+ def moveDelta(self, delta):
+ collided = False
+ s = self.states[self.currentstate]
+ s.setPosition(self.position[0], self.position[1])
+ d = [0,0]
+ if self.surfaces.collideRect(s.hitbox) or (delta[0] == 0 and delta[1] == 0):
+ return (True, (0,0))
+ if abs(delta[0]) > abs(delta[1]):
+ dx = delta[0] / abs(delta[0])
+ dy = float(delta[1]) / float(abs(delta[0]))
+ xrange = abs(int(delta[0]))
+ while xrange > 0:
+ d[0] += dx
+ d[1] += dy
+ nrect = s.hitbox.move(d[0],d[1])
+ if self.surfaces.collideRect(nrect):
+ # Slide along surfaces if possible
+ nrect = s.hitbox.move(d[0],d[1]-dy)
+ if not self.surfaces.collideRect(nrect):
+ d[1] -= dy
+ xrange -= 1
+ continue
+ nrect = s.hitbox.move(d[0]-dx,d[1])
+ if dy != 0 and not self.surfaces.collideRect(nrect):
+ d[0] -= dx
+ xrange -= 1
+ continue
+ d[0] -= dx
+ d[1] -= dy
+ collided = True
+ break
+ xrange -= 1
+ d[1] = int(d[1])
+ else:
+ dy = delta[1] / abs(delta[1])
+ dx = float(delta[0]) / float(abs(delta[1]))
+ yrange = abs(int(delta[1]))
+ while yrange > 0:
+ d[1] += dy
+ d[0] += dx
+ nrect = s.hitbox.move(d[0],d[1])
+ if self.surfaces.collideRect(nrect):
+ # Slide along surfaces if possible
+ nrect = s.hitbox.move(d[0]-dx,d[1])
+ if not self.surfaces.collideRect(nrect):
+ d[0] -= dx
+ yrange -= 1
+ continue
+ nrect = s.hitbox.move(d[0],d[1]-dy)
+ if dx != 0 and not self.surfaces.collideRect(nrect):
+ d[1] -= dy
+ yrange -= 1
+ continue
+ d[1] -= dy
+ d[0] -= dx
+ collided = True
+ break
+ yrange -= 1
+ d[0] = int(d[0])
+ return (collided, d)
+
+
+ def move(self, delta):
+ (collided, delta) = self.moveDelta(delta)
+ self.position[0] += delta[0]
+ self.position[1] += delta[1]
+ return collided
+
+
+ def update(self):
+ "Override this function. Called once per frame."
+ pass
+
+
+ def draw(self):
+ self.states[self.currentstate].drawAt(self.position)
+import pygame
+
+class Camera:
+ stage = None
+ resolution = None
+ focus = None
+ camerapos = None
+ bbox = None
+
+ def __init__(self, stage):
+ self.stage = stage
+ self.resolution = pygame.display.get_surface().get_size()
+ self.camerapos = [0,0]
+
+
+ def setFocus(self, obj):
+ self.focus = obj
+
+
+ def clearFocus(self):
+ self.focus = None
+
+
+ def setPosition(self, x, y):
+ self.camerapos = [x, y]
+
+
+ def setBbox(self, bbox):
+ self.bbox = bbox
+
+
+ def clearBbox(self):
+ self.bbox = None
+
+
+ def update(self):
+ if self.focus:
+ self.camerapos = [self.focus.position[0] - self.resolution[0]/2,
+ self.focus.position[1] - self.resolution[1]/4]
+
+ if self.bbox:
+ # This is a wee bit confusing since Rects assume (0,0) in the
+ # upper left, but we want it in the lower left. Tops and
+ # bottoms are reversed.
+ if self.bbox.height < self.resolution[1] or self.camerapos[1] < self.bbox.top:
+ self.camerapos[1] = self.bbox.top
+ elif self.camerapos[1] + self.resolution[1] > self.bbox.bottom:
+ self.camerapos[1] = self.bbox.bottom - self.resolution[1]
+
+ if self.bbox.width < self.resolution[0] or self.camerapos[0] < self.bbox.left:
+ self.camerapos[0] = self.bbox.left
+ elif self.camerapos[0] + self.resolution[0] > self.bbox.right:
+ self.camerapos[0] = self.bbox.right - self.resolution[0]
+
+ self.stage.moveTo(self.camerapos[0], self.camerapos[1])
+from Sprite import Sprite
+from OpenGL.GL import *
+from Numeric import array
+import pygame
+
+import TexMan
+
+class Collage:
+ tiles = None
+ displaylist = None
+ bbox = None
+
+ def __init__(self, file):
+ self.parse(file)
+
+
+ def parse(self, file):
+ if self.displaylist == None:
+ self.displaylist = glGenLists(1)
+ glNewList(self.displaylist, GL_COMPILE)
+ glEnable(GL_TEXTURE_2D)
+ glColor3f(1.0,1.0,1.0)
+ ctexture = None
+ ctexsize = None
+ vertexarray = []
+ texcoordarray = []
+ nvertices = 0
+ #maxvertices = glGetIntegerv(GL_MAX_ELEMENTS_VERTICES)
+ #maxindexes = glGetIntegerv(GL_MAX_ELEMENTS_INDICES)
+
+ self.tiles = []
+ mf = (1,1)
+ f = open(file,'r')
+ for line in f:
+ args = line.split()
+ if not args:
+ continue
+ cmd = args[0]
+ args = args[1:]
+ if cmd == 'tilesize':
+ mf = (int(args[0]),int(args[1]))
+ elif cmd == 'bbox':
+ self.bbox = pygame.Rect(0, 0, mf[0] * int(args[0]), mf[1] * int(args[1]))
+ elif cmd == 'texture':
+ if nvertices > 0: # dump vertex array
+ glVertexPointer(3, GL_FLOAT, 0, array(vertexarray,'f'))
+ glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
+ glDrawArrays(GL_QUADS, 0, nvertices)
+ vertexarray = []
+ texcoordarray = []
+ nvertices = 0
+ d = TexMan.load(args[0])
+ ctexture = d['texture']
+ ctexsize = d['tsize']
+ glBindTexture(GL_TEXTURE_2D, ctexture)
+ elif cmd == 'tile':
+ x = int(args[0]) * mf[0]
+ y = int(args[1]) * mf[1]
+ vertexarray += [
+ (x,y,0),
+ (x+ctexsize[0],y,0),
+ (x+ctexsize[0],y+ctexsize[1],0),
+ (x,y+ctexsize[1],0)
+ ]
+ texcoordarray += [
+ (1.0,0.0),(1.0,1.0),(0.0,1.0),(0.0,0.0)
+ ]
+ nvertices += 4
+ f.close()
+ # Finally,
+ glVertexPointer(3, GL_FLOAT, 0, array(vertexarray,'f'))
+ glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
+ glDrawArrays(GL_QUADS, 0, nvertices)
+ glEndList()
+
+
+ def draw(self):
+ glCallList(self.displaylist)
+import sys
+import imp
+import pygame
+from pygame.locals import *
+from Sprite import Sprite
+from Numeric import array,reshape
+
+try:
+ from OpenGL.GL import *
+ from OpenGL.GLU import *
+except:
+ print 'sprite_engine requires PyOpenGL'
+ raise SystemExit
+
+import config
+
+fpsman = pygame.time.Clock()
+
+def init(resolution=(640,480)):
+ pygame.init()
+ try:
+ sres = [int(n * config.scalefactor) for n in resolution]
+ except AttributeError:
+ sres = resolution
+ flags = OPENGL|DOUBLEBUF|HWSURFACE
+ try:
+ if config.fullscreen:
+ flags |= FULLSCREEN
+ except AttributeError:
+ pass
+ pygame.display.set_mode(sres, flags)
+
+ # Set up the camera
+ glLoadIdentity()
+ glMatrixMode(GL_PROJECTION)
+ #gluOrtho2D(0,resolution[0],0,resolution[1])
+ # calculate Z-depth from average resolution
+ zdepth = sum(resolution) / 2
+ glFrustum(0,resolution[0],0,resolution[1],zdepth,zdepth * 10)
+ glTranslate(0,0,-zdepth)
+
+ #glTranslate(resolution[0]/2, resolution[1]/2, 0)
+ #glTranslate(0,0,-zdepth)
+ #glRotate(30, 0, 1, 0)
+ #glTranslate(-resolution[0]/2, -resolution[1]/2, 0)
+
+ # And initialize things how we like in GL
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
+ glEnable(GL_TEXTURE_2D)
+ glEnable(GL_BLEND)
+ glDisable(GL_DITHER)
+ glShadeModel(GL_FLAT)
+ glEnable(GL_CULL_FACE)
+ #glDepthFunc(GL_LEQUAL)
+ #glEnable(GL_DEPTH_TEST)
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST)
+ glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST)
+ glEnableClientState(GL_VERTEX_ARRAY)
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY)
+
+
+running = True
+level = None
+eventhandlers = []
+updaters = []
+drawers = []
+
+
+def loadLevel(name):
+ global level
+ unloadLevel()
+ fp, pathname, description = imp.find_module(name)
+ level = imp.load_module(name, fp, pathname, description)
+ updaters.insert(0, level.update)
+ drawers.insert(0, level.draw)
+ return level
+
+
+def unloadLevel():
+ global level
+ if level:
+ updaters.remove(level.update)
+ drawers.remove(level.draw)
+ level = None
+
+
+def stop():
+ global running
+ running = False
+
+
+def run():
+ global running
+ running = True
+ while running:
+ events = pygame.event.get()
+ for e in events:
+ for h in eventhandlers:
+ h(e)
+
+ #clear screen and move camera
+ #glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+ glClear(GL_COLOR_BUFFER_BIT)
+
+ for update in updaters:
+ update()
+
+ for draw in drawers:
+ draw()
+
+ pygame.display.flip()
+ fpsman.tick(60)
+from OpenGL.GL import *
+from OpenGL.GLU import *
+import pygame
+from pygame.locals import *
+from Numeric import array
+
+from util import *
+import TexMan
+
+LEFT = -1
+BOTTOM = -1
+CENTER = 0
+RIGHT = 1
+TOP = 1
+
+class Sprite:
+ frames = None
+ frameno = 0
+ animating = False
+ anim_t = 0
+ size = (0,0)
+ displaylists = None
+
+ showup = False
+ showhitbox = False
+
+ def __init__(self, file, position=(0,0), scale=1.0, rotation=0,
+ mirror=False, flip=False, color=(1.0,1.0,1.0,1.0),
+ framerate=0.0, gravity=(LEFT,BOTTOM)):
+
+ self.frames = []
+ d = None
+ if type(file) == type([]):
+ d = TexMan.load(file[0])
+ self.frames.append(d['texture'])
+ for f in file[1:]:
+ self.frames.append(TexMan.load(f)['texture'])
+ else:
+ d = TexMan.load(file)
+ self.frames.append(d['texture'])
+ self.size = d['size']
+ self.tsize = d['tsize']
+
+ self.rotation = rotation
+ self.mirror = mirror
+ self.flip = flip
+ self.color = color
+ self.setFramerate(framerate)
+ self.scale = scale
+ self.position = position
+ self.gravity = gravity
+
+ self.fixHitbox()
+ self.gen_displaylist()
+
+
+ def gen_displaylist(self):
+ if self.displaylists == None:
+ n = glGenLists(len(self.frames))
+ self.displaylists = range(n,n+len(self.frames))
+ c = [0,0]
+ for i in range(0,2):
+ if self.gravity[i] == LEFT:
+ c[i] = 0
+ elif self.gravity[i] == CENTER:
+ c[i] = -self.size[i]/2
+ elif self.gravity[i] == RIGHT:
+ c[i] = -self.size[i]
+ else:
+ print "Invalid gravity:",x
+ c[i] = 0
+ if self.mirror:
+ c[0] -= self.tsize[0] - self.size[0]
+ if self.flip:
+ c[1] -= self.tsize[1] - self.size[1]
+ vertexarray = array(
+ ((c[0],c[1],0),(c[0]+self.tsize[0],c[1],0),
+ (c[0]+self.tsize[0],c[1]+self.tsize[1],0),
+ (c[0],c[1]+self.tsize[1],0))
+ ,'f')
+ if self.flip and self.mirror:
+ texcoordarray = array(
+ ((0.0,0.0),(0.0,1.0),(1.0,1.0),(1.0,0.0))
+ ,'f')
+ elif self.flip:
+ texcoordarray = array(
+ ((0.0,1.0),(0.0,0.0),(1.0,0.0),(1.0,1.0))
+ ,'f')
+ elif self.mirror:
+ texcoordarray = array(
+ ((1.0,1.0),(1.0,0.0),(0.0,0.0),(0.0,1.0))
+ ,'f')
+ else:
+ texcoordarray = array(
+ ((1.0,0.0),(1.0,1.0),(0.0,1.0),(0.0,0.0))
+ ,'f')
+ index = array((0,1,2,3),'i')
+ for i in range(0,len(self.frames)):
+ glNewList(self.displaylists[i], GL_COMPILE)
+ glEnable(GL_TEXTURE_2D)
+ glBindTexture(GL_TEXTURE_2D, self.frames[i])
+ glColor4fv(self.color)
+ glVertexPointer(3, GL_FLOAT, 0, vertexarray)
+ glTexCoordPointer(2, GL_FLOAT, 0, texcoordarray)
+ glDrawArrays(GL_QUADS, 0, 4)
+ glEndList()
+
+
+ def setPosition(self, x, y):
+ self.position = (x,y)
+ self.hitbox.topleft = (x + self.hitbox_offset[0], y + self.hitbox_offset[1])
+
+
+ def setColor(self, r, g, b, a = 1.0):
+ self.color = (r,g,b,a)
+ self.gen_displaylist()
+
+
+ def setScale(self, scale):
+ self.scale = scale
+ self.fixHitbox()
+
+
+ def fixHitbox(self):
+ self.hitbox_offset = [0,0]
+ for i in range(0,2):
+ if self.gravity[i] == -1:
+ self.hitbox_offset[i] = 0
+ elif self.gravity[i] == 0:
+ self.hitbox_offset[i] = -(self.size[i] * self.scale) / 2
+ elif self.gravity[i] == 1:
+ self.hitbox_offset[i] = -(self.size[i] * self.scale)
+ self.hitbox = pygame.Rect((self.position[0] + self.hitbox_offset[0], self.position[1] + self.hitbox_offset[1]), (self.size[0] * self.scale, self.size[1] * self.scale))
+
+
+ def setGravity(self, xg, yg, genlist=True):
+ if self.gravity != (xg,yg):
+ self.gravity = (xg,yg)
+ if genlist:
+ self.gen_displaylist()
+ self.fixHitbox()
+
+
+ def setFramerate(self, rate):
+ self.framerate = rate
+ if self.framerate != 0.0:
+ self.anim_period = int(1000/rate)
+ if self.anim_t == 0:
+ self.reset()
+ else:
+ self.stop()
+
+
+ def reset(self):
+ self.anim_t = pygame.time.get_ticks()
+ self.frameno = 0
+
+
+ def play(self):
+ if len(self.frames) > 1:
+ self.animating = True
+
+
+ def stop(self):
+ self.animating = False
+
+
+ def draw(self):
+ self.drawAt(self.position)
+
+
+ def drawAt(self,pos):
+ if self.animating:
+ now = pygame.time.get_ticks()
+ dt = now - self.anim_t
+ if dt > self.anim_period:
+ self.frameno = (self.frameno + int(dt / self.anim_period)) % len(self.frames)
+ self.anim_t = now
+ glPushMatrix()
+ glTranslatef(pos[0], pos[1], 0.0)
+ if self.rotation != 0:
+ glRotatef(self.rotation, 0.0, 0.0, -1.0)
+ if self.scale != 1.0:
+ glScalef(self.scale, self.scale, 1.0)
+ glCallList(self.displaylists[self.frameno])
+ if self.showup:
+ glDisable(GL_TEXTURE_2D)
+ glColor3f(0.0, 0.0, 1.0)
+ glBegin(GL_TRIANGLES)
+ glVertex(-5, 0, 0)
+ glVertex(0, 20, 0)
+ glVertex(5, 0, 0)
+ glEnd()
+ glPopMatrix()
+ if self.showhitbox:
+ glDisable(GL_TEXTURE_2D)
+ glColor3f(1.0, 1.0, 1.0)
+ glBegin(GL_LINE_LOOP)
+ glVertex(self.hitbox.left, self.hitbox.bottom, 0)
+ glVertex(self.hitbox.left, self.hitbox.top, 0)
+ glVertex(self.hitbox.right, self.hitbox.top, 0)
+ glVertex(self.hitbox.right, self.hitbox.bottom, 0)
+ glEnd()
+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.bbox = self.collage.bbox
+ 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()
+import pygame
+from OpenGL.GL import *
+import math
+
+class Surface:
+ fuzz = 20
+ def __init__(self, p1, p2):
+ self.p1 = p1
+ self.p2 = p2
+ self.dx = p2[0] - p1[0]
+ self.dy = p2[1] - p1[1]
+ try:
+ self.m_x = float(self.dy) / float(self.dx)
+ except ZeroDivisionError:
+ self.m_x = 1e999
+ self.b_x = p1[1] - self.m_x * p1[0]
+ try:
+ self.m_y = float(self.dx) / float(self.dy)
+ except ZeroDivisionError:
+ self.m_y = 1e999
+ self.b_y = p1[0] - self.m_y * p1[1]
+
+ # A collision rect for fast collision culling. Only surfaces
+ # whose initial rect collision check passes will run the
+ # detailed check
+ self.rect = pygame.Rect(p1,(self.dx,self.dy))
+ self.rect.normalize()
+ #self.rect.width += 1
+ #self.rect.height += 1
+
+
+ def lineSideTest(self, point):
+ if self.m_x > -1.0 and self.m_x < 1.0:
+ if self.dx > 0:
+ if point[1] < self.m_x * point[0] + self.b_x:
+ return True
+ else:
+ if point[1] > self.m_x * point[0] + self.b_x:
+ return True
+ else:
+ if self.dy > 0:
+ if point[0] > self.m_y * point[1] + self.b_y:
+ return True
+ else:
+ if point[0] < self.m_y * point[1] + self.b_y:
+ return True
+ return False
+
+
+ def collidePoint(self, point):
+ if self.rect.collidepoint(point[0],point[1]):
+ return self.lineSideTest(point)
+
+
+ def collideRect(self, rect):
+ if not self.rect.colliderect(rect):
+ return False
+ #print self.collidePoint(rect.bottomleft), self.collidePoint(rect.bottomright), self.collidePoint(rect.topright), self.collidePoint(rect.topright)
+ return self.lineSideTest(rect.bottomleft) or \
+ self.lineSideTest(rect.bottomright) or \
+ self.lineSideTest(rect.topright) or \
+ self.lineSideTest(rect.topright)
+
+
+ def draw(self):
+ angle = math.atan(self.m_x)
+ if self.dx < 0 or self.dy < 0:
+ arrow = (angle+3*math.pi/2) % (2*math.pi)
+ else:
+ arrow = (angle+math.pi/2) % (2*math.pi)
+ anglepoint = (math.cos(arrow) * 10, math.sin(arrow) * 10)
+
+ glPushMatrix()
+ glDisable(GL_TEXTURE_2D)
+
+ if self.dx > 0:
+ py = self.rect.top # rects are upside-down in our space
+ else:
+ py = self.rect.bottom - 1
+ if self.dy > 0:
+ px = self.rect.right - 1
+ else:
+ px = self.rect.left
+ glColor4f(1.0,0.0,0.0,0.25)
+ glBegin(GL_TRIANGLES)
+ glVertex3f(self.p2[0], self.p2[1], 0)
+ glVertex3f(self.p1[0], self.p1[1], 0)
+ glVertex3f(px, py, 0)
+ glEnd()
+
+ glColor3f(1.0,1.0,1.0)
+ glBegin(GL_LINES)
+ glVertex3f(self.p1[0],self.p1[1],0)
+ glVertex3f(self.p2[0],self.p2[1],0)
+ glVertex3f(self.rect.centerx, self.rect.centery, 0)
+ glVertex3f(self.rect.centerx + anglepoint[0], self.rect.centery + anglepoint[1], 0)
+ glEnd()
+
+ glPopMatrix()
+
+
+class Solid:
+ def __init__(self, p1, p2):
+ self.rect = pygame.Rect(p1,(p2[0]-p1[0],p2[1]-p1[1]))
+ self.rect.normalize()
+
+
+ def collidePoint(self, point):
+ return self.rect.collidepoint(point[0],point[1])
+
+
+ def collideRect(self, rect):
+ return bool(self.rect.colliderect(rect))
+
+
+ def draw(self):
+ glPushMatrix()
+ glDisable(GL_TEXTURE_2D)
+ glColor4f(1.0,0.0,0.0,0.25)
+ glBegin(GL_QUADS)
+ glVertex(self.rect.left, self.rect.bottom, 0)
+ glVertex(self.rect.left, self.rect.top, 0)
+ glVertex(self.rect.right, self.rect.top, 0)
+ glVertex(self.rect.right, self.rect.bottom, 0)
+ glEnd()
+ glColor3f(1.0,1.0,1.0)
+ glBegin(GL_LINE_LOOP)
+ glVertex(self.rect.left, self.rect.bottom, 0)
+ glVertex(self.rect.left, self.rect.top, 0)
+ glVertex(self.rect.right, self.rect.top, 0)
+ glVertex(self.rect.right, self.rect.bottom, 0)
+ glEnd()
+ glPopMatrix()
+
+
+class SurfaceSet:
+ surfaces = None
+
+ def __init__(self, file = None):
+ self.surfaces = []
+ if file is not None:
+ self.parse(file)
+
+
+ def parse(self, file):
+ self.surfaces = []
+
+ mf = (1,1)
+ f = open(file,'r')
+ for line in f:
+ args = line.split()
+ if not args:
+ continue
+ cmd = args[0]
+ args = args[1:]
+ if cmd == 'tilesize':
+ mf = (int(args[0]),int(args[1]))
+ elif cmd == 'surface':
+ p1 = (int(args[0])*mf[0],int(args[1])*mf[1])
+ p2 = (int(args[2])*mf[0],int(args[3])*mf[1])
+ self.surfaces.append(Surface(p1,p2))
+ elif cmd == 'solid':
+ p1 = (int(args[0])*mf[0],int(args[1])*mf[1])
+ p2 = (int(args[2])*mf[0],int(args[3])*mf[1])
+ self.surfaces.append(Solid(p1,p2))
+ f.close()
+
+
+ def collidePoint(self,point):
+ colliders = filter(lambda x: x.rect.collidepoint(point), self.surfaces)
+ if not colliders:
+ return False
+ for c in colliders:
+ if not c.collidePoint(point):
+ return False
+ return True
+
+
+ def collideRect(self, rect):
+ colliders = filter(lambda x: x.rect.colliderect(rect), self.surfaces)
+ if not colliders:
+ return False
+ #print [(c,c.rect,c.collideRect(rect)) for c in colliders]
+ for c in colliders:
+ if not c.collideRect(rect):
+ return False
+ return True
+
+
+ def draw(self):
+ for s in self.surfaces:
+ s.draw()
+
+
+ def append(self, s):
+ return self.surfaces.append(s)
+
+
+ def remove(self, s):
+ return self.surfaces.remove(s)
+from OpenGL.GL import *
+from OpenGL.GLU import *
+import pygame
+from pygame.locals import *
+from Numeric import array,reshape,concatenate
+
+from util import *
+import config
+
+sprites = {}
+
+def load(file):
+ global sprites
+ if sprites.has_key(file):
+ return sprites[file]
+
+ image = pygame.image.load(file)
+ w = image.get_width()
+ h = image.get_height()
+ tw = n2ceil(w)
+ th = n2ceil(h)
+ if config.square_textures:
+ tw = th = max(tw,th)
+ newImage = pygame.Surface((tw,th), SRCALPHA, 32)
+ newImage.fill((255,255,255,0))
+ newImage.blit(image, (0, th - h)) # We want this in the lower corner
+
+ # Freaking pygame making us do this by hand...
+ colors = pygame.surfarray.pixels3d(newImage)
+ alphas = pygame.surfarray.pixels_alpha(newImage)
+ alphas = reshape(alphas, (tw,th,1))
+ pixels = concatenate((colors,alphas),2)
+
+ # Create Texture
+ texture = glGenTextures(1)
+ glBindTexture(GL_TEXTURE_2D, texture)
+ glTexImage2Dub(GL_TEXTURE_2D, 0, GL_RGBA8, 0, GL_RGBA, pixels)
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
+
+ sprites[file] = {'texture':texture,'size':(w,h),'tsize':(tw,th)}
+ return sprites[file]
+#!/usr/bin/env python
+
+import random
+import sys
+import pygame
+from pygame.locals import *
+from Sprite import Sprite
+import Engine
+from Numeric import array,reshape
+
+try:
+ from OpenGL.GL import *
+ from OpenGL.GLU import *
+except:
+ print 'This requires PyOpenGL'
+ raise SystemExit
+
+
+#resolution = (1280,1024)
+resolution = (640,480)
+#resolution = (320,240)
+
+if sys.argv[1:]:
+ spritefile = sys.argv[1:]
+else:
+ spritefile = 'img/ball.png'
+
+class Bouncer:
+ def __init__(self):
+ self.velocity = [random.uniform(-10,10),random.uniform(-10,10)]
+ self.sprite = Sprite(spritefile)
+ self.sprite.framerate = 9.0
+ self.sprite.play()
+ #self.sprite.color = (random.random(),random.random(),random.random(), 0.5)
+ self.sprite.position = [random.randrange(0,resolution[0]),random.randrange(0,resolution[1])]
+ self.sprite.gen_displaylist()
+ self.draw = self.sprite.draw
+
+ def update(self):
+ if self.sprite.position[0] < 0:
+ self.sprite.position[0] = 0
+ self.velocity[0] = -self.velocity[0]
+ elif self.sprite.position[0] > resolution[0]:
+ self.sprite.position[0] = resolution[0]
+ self.velocity[0] = -self.velocity[0]
+ if self.sprite.position[1] < 0:
+ self.sprite.position[1] = 0
+ self.velocity[1] = -self.velocity[1]
+ elif self.sprite.position[1] > resolution[1]:
+ self.sprite.position[1] = resolution[1]
+ self.velocity[1] = -self.velocity[1]
+
+ #self.velocity[1] -= 1.0
+
+ self.sprite.position[0] += self.velocity[0]
+ self.sprite.position[1] += self.velocity[1]
+
+Engine.init(resolution)
+
+bouncers = []
+for i in range(0,1000):
+ h = Bouncer()
+ bouncers.append(h)
+
+pygame.display.set_caption("bouncetest")
+
+def update():
+ for b in bouncers:
+ b.update()
+
+Engine.updaters.append(update)
+
+def draw():
+ for b in bouncers:
+ b.draw()
+
+Engine.drawers.append(draw)
+
+n = 0
+def fpsthing():
+ global n
+ n += 1
+ if n == 100:
+ fps = "%0.1f FPS" % Engine.fpsman.get_fps()
+ pygame.display.set_caption("bouncetest [%s]" % fps)
+ print fps
+ n = 0
+
+Engine.updaters.append(fpsthing)
+
+Engine.run()
+# If your card has trouble with non-square textures (my TNT seems to), set this
+# to true to make all textures square
+square_textures = True
+#scalefactor = 2
+#fullscreen = True
+tilesize 64 64
+bbox 19 7
+texture img/block.png
+tile 0 0
+tile 1 0
+tile 2 0
+tile 3 0
+tile 4 0
+tile 5 0
+tile 6 0
+tile 7 0
+tile 8 0
+tile 9 0
+
+tile 3 1
+tile 4 1
+tile 4 2
+tile 4 3
+tile 4 4
+
+tile 6 1
+tile 6 2
+
+solid 0 0 10 1
+surface 2 1 4 2
+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 11 1
+solid 12 0 19 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
+tilesize 64 64
+texture img/block.png
+tile 0 0
+tile 0 1
+tile 1 0
+tile 1 1
+texture img/star.png
+tile 232 421
+tile 390 193
+tile 548 66
+tile 311 367
+tile 430 12
+tile 88 64
+tile 90 123
+tile 286 370
+tile 263 100
+tile 338 84
+tile 231 47
+tile 132 415
+tile 399 183
+tile 111 470
+tile 445 67
+tile 427 159
+tile 237 103
+tile 533 223
+tile 26 409
+tile 382 87
+tile 507 82
+tile 329 380
+tile 381 417
+tile 518 317
+tile 378 197
+tile 607 463
+tile 502 274
+tile 406 235
+tile 185 249
+tile 448 412
+tile 418 29
+tile 248 121
+tile 177 85
+tile 327 352
+tile 466 39
+tile 591 306
+tile 106 95
+tile 570 209
+tile 163 466
+tile 100 185
+tile 376 469
+tile 586 33
+tile 383 251
+tile 178 209
+tile 121 211
+tile 408 289
+tile 539 222
+tile 476 245
+tile 282 274
+tile 557 90
+tile 249 80
+tile 563 447
+tile 18 229
+tile 255 244
+tile 358 354
+tile 563 323
+tile 292 28
+tile 497 241
+tile 379 67
+tile 260 51
+tile 159 163
+tile 209 304
+tile 541 28
+tile 635 475
+tile 479 130
+tile 473 45
+tile 127 308
+tile 281 420
+tile 332 28
+tile 177 478
+tile 413 12
+tile 265 403
+tile 216 147
+tile 105 117
+tile 64 417
+tile 164 137
+tile 131 416
+tile 386 67
+tile 511 111
+tile 239 419
+tile 600 445
+tile 356 369
+tile 314 266
+tile 153 473
+tile 239 221
+tile 359 135
+tile 288 133
+tile 511 449
+tile 234 401
+tile 333 71
+tile 40 78
+tile 426 235
+tile 251 195
+tile 324 432
+tile 25 334
+tile 205 37
+tile 219 284
+tile 358 414
+tile 260 395
+tile 202 22
+tile 594 64
+tile 187 39
+tile 71 231
+tile 428 5
+tile 180 302
+tile 435 93
+tile 10 161
+tile 45 113
+tile 224 99
+tile 46 401
+tile 196 465
+tile 432 376
+tile 194 342
+tile 375 76
+tile 437 174
+tile 197 275
+tile 208 92
+tile 195 118
+tile 618 202
+tile 554 406
+#!/usr/bin/env python
+
+import pygame
+from Sprite import *
+from Collage import *
+from Surface import *
+from Actor import *
+from Stage import *
+from Camera import *
+import Engine
+
+#resolution = (1280,1024)
+resolution = (640,480)
+#resolution = (320,240)
+
+Engine.init(resolution)
+
+pygame.display.set_caption("platformtest")
+
+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_%d.png' % x for x in range(1,4)])
+candelabra.setPosition(64, 64)
+candelabra.setScale(2.0)
+candelabra.setGravity(CENTER, BOTTOM)
+candelabra.setFramerate(10.0)
+candelabra.play()
+
+main.add(candelabra)
+Engine.drawers.append(stage.draw)
+
+camera = Camera(stage)
+camera.setBbox(main.bbox)
+Engine.updaters.append(camera.update)
+
+class Richter(Actor):
+ def __init__(self, surfaces=None):
+ Actor.__init__(self, surfaces)
+
+ self.defineState('walk_l',
+ Sprite(['Sprites/Richter/richter%d.png' % n for n in range(0,8)],
+ framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+ self.defineState('walk_r',
+ Sprite(['Sprites/Richter/richter%d.png' % n for n in range(0,8)], mirror=True,
+ framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+ self.defineState('idle_l',
+ Sprite("Sprites/Richter/richter0.png", scale=2.0, gravity=(CENTER,BOTTOM)))
+ self.defineState('idle_r',
+ Sprite("Sprites/Richter/richter0.png", mirror=True, scale=2.0, gravity=(CENTER,BOTTOM)))
+ self.setState('idle_r')
+
+ self.delta = [0,0]
+ self.jumping = False
+ self.grounded = self.collideDelta((0,-1))
+
+
+ def jump(self):
+ if not self.jumping and self.grounded:
+ self.delta[1] = 15.0
+ self.jumping = True
+ self.grounded = False
+
+
+ def jumpCancel(self):
+ self.jumping = False
+ if self.delta[1] > 0.0:
+ self.delta[1] = 0.0
+
+
+ def bounce(self):
+ self.delta[1] = -self.delta[1]
+
+
+ def walkRight(self):
+ self.delta[0] = 2
+ self.setState('walk_r')
+
+
+ def walkLeft(self):
+ self.delta[0] = -2
+ self.setState('walk_l')
+
+
+ def idle(self):
+ if self.currentstate == 'walk_l':
+ self.setState('idle_l')
+ else:
+ self.setState('idle_r')
+ self.delta[0] = 0
+
+
+ def update(self):
+ if self.grounded:
+ self.delta[1] = 0
+ else:
+ if self.delta[1] > -15.0:
+ self.delta[1] -= 0.5
+ if self.grounded and self.delta[0] != 0:
+ # Climb up inclines (visually poetic, is it not?)
+ if self.collideDelta(self.delta):
+ delta = list(self.delta)
+ for delta[1] in range(1, abs(delta[0]) + 1):
+ if not self.collideDelta(delta):
+ self.delta = delta
+ break
+ # ... and walk down slopes
+ else:
+ self.delta[1] = -abs(self.delta[0])
+ self.move(self.delta)
+ self.grounded = self.collideDelta((0,-1))
+
+
+richter = Richter(stage.surfaces)
+richter.setPosition(32, 64)
+main.add(richter)
+camera.setFocus(richter)
+
+class Zombie(Actor):
+ def __init__(self, surfaces=None):
+ Actor.__init__(self, surfaces)
+
+ self.defineState('walk_r',
+ Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
+ framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+ self.defineState('walk_l',
+ Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
+ mirror=True, framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+
+ self.setState('walk_r')
+ self.direction = 0
+ self.disabled = 0
+ self.topbox = pygame.Rect(self.states['walk_l'].hitbox)
+ self.topbox.height = 10
+
+ def update(self):
+ if self.disabled > 0:
+ if self.disabled % 2:
+ self.setState('walk_r')
+ else:
+ self.setState('walk_l')
+ self.disabled -= 1
+ if self.disabled == 0:
+ if self.direction == 0:
+ self.setState('walk_r')
+ else:
+ self.setState('walk_l')
+ else:
+ if self.direction == 0:
+ if self.move((1, 0)):
+ self.direction = 1
+ self.setState('walk_l')
+ elif self.direction == 1:
+ if self.move((-1, 0)):
+ self.direction = 0
+ self.setState('walk_r')
+
+ self.topbox.bottom = self.position[1] + 72
+ self.topbox.left = self.position[0] - 31
+ if self.topbox.collidepoint(richter.position):
+ self.disabled = 120
+ richter.bounce()
+
+zombie = Zombie(stage.surfaces)
+zombie.setPosition(512, 64)
+main.add(zombie)
+
+
+def input(e):
+ if e.type == pygame.KEYDOWN:
+ if e.key == pygame.K_LEFT:
+ richter.walkLeft()
+ elif e.key == pygame.K_RIGHT:
+ richter.walkRight()
+ elif e.key == pygame.K_UP:
+ richter.jump()
+ elif e.key == pygame.K_1:
+ stage.toggleDrawSurfaces()
+ elif e.key == pygame.K_2:
+ camera.clearBbox()
+ elif e.key == pygame.K_3:
+ camera.setBbox(main.bbox)
+ elif e.key == pygame.K_ESCAPE:
+ Engine.stop()
+ elif e.type == pygame.KEYUP:
+ if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
+ richter.idle()
+ elif e.key == pygame.K_UP:
+ richter.jumpCancel()
+ elif e.type == pygame.QUIT:
+ Engine.stop()
+
+Engine.eventhandlers.append(input)
+
+def update():
+ richter.update()
+ if richter.position[1] < 0:
+ richter.position = [richter.position[0],480]
+ zombie.update()
+
+Engine.updaters.append(update)
+
+n = 0
+def fpsthing():
+ global n
+ n += 1
+ if n == 100:
+ fps = "%0.1f FPS" % Engine.fpsman.get_fps()
+ pygame.display.set_caption("platformtest [%s]" % fps)
+ print fps
+ n = 0
+
+Engine.updaters.append(fpsthing)
+
+Engine.run()
+#!/usr/bin/env python
+
+import pygame
+from Sprite import *
+from Surface import *
+import Engine
+
+#resolution = (1280,1024)
+resolution = (640,480)
+#resolution = (320,240)
+
+Engine.init(resolution)
+
+pygame.display.set_caption("surfacetest")
+
+surfaces = SurfaceSet('data/example1')
+# Octagon
+#surfaces.append(Surface( (520,240),(461,98) ))
+#surfaces.append(Surface( (461,98),(319,40) ))
+#surfaces.append(Surface( (319,40),(178,98) ))
+#surfaces.append(Surface( (178,98),(120,240) ))
+#surfaces.append(Surface( (120,240),(178,381) ))
+#surfaces.append(Surface( (178,381),(320,440) ))
+#surfaces.append(Surface( (320,440),(461,381) ))
+#surfaces.append(Surface( (461,381),(520,240) ))
+
+# Concave
+#surfaces.append(Surface( (200,200), (200,400) ))
+#surfaces.append(Surface( (200,400), (300,300) ))
+#surfaces.append(Surface( (300,300), (400,300) ))
+#surfaces.append(Surface( (400,300), (500,400) ))
+#surfaces.append(Surface( (500,400), (500,200) ))
+#surfaces.append(Surface( (500,200), (200,200) ))
+
+player = Sprite('img/star.png')
+player.setGravity(CENTER, CENTER)
+player.position = [resolution[0]/2,resolution[1]/2]
+
+class gamestate:
+ direction = [0,0]
+ colliding = False
+
+def input(e):
+ if e.type == pygame.KEYDOWN:
+ if e.key == pygame.K_LEFT:
+ gamestate.direction[0] = -1
+ elif e.key == pygame.K_RIGHT:
+ gamestate.direction[0] = 1
+ elif e.key == pygame.K_DOWN:
+ gamestate.direction[1] = -1
+ elif e.key == pygame.K_UP:
+ gamestate.direction[1] = 1
+ elif e.type == pygame.KEYUP:
+ if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
+ gamestate.direction[0] = 0
+ elif e.key == pygame.K_UP or e.key == pygame.K_DOWN:
+ gamestate.direction[1] = 0
+
+Engine.eventhandlers.append(input)
+
+
+def update():
+ player.position[0] += gamestate.direction[0]
+ player.position[1] += gamestate.direction[1]
+ if surfaces.collidePoint(player.position):
+ if not gamestate.colliding:
+ player.setColor(1.0,0.2,0.2)
+ gamestate.colliding = True
+ else:
+ if gamestate.colliding:
+ player.setColor(1.0,1.0,1.0)
+ gamestate.colliding = False
+
+Engine.updaters.append(update)
+
+def draw():
+ surfaces.draw()
+ player.draw()
+
+Engine.drawers.append(draw)
+
+Engine.run()
+def n2ceil(n):
+ "Find the next power of two greater than or equal to n"
+ assert n > 0
+ x = 1
+ while x <= 1024:
+ if x >= n:
+ return x
+ x <<= 1
+ # FAIL
+ return 1024
-from OpenGL.GL import *
-from OpenGL.GLU import *
-import pygame
-from pygame.locals import *
-from Numeric import array
-
-from util import *
-import TexMan
-
-LEFT = -1
-BOTTOM = -1
-CENTER = 0
-RIGHT = 1
-TOP = 1
-
-class Sprite:
- frames = None
- frameno = 0
- animating = False
- anim_t = 0
- size = (0,0)
- displaylists = None
-
- showup = False
- showhitbox = False
-
- def __init__(self, file, position=(0,0), scale=1.0, rotation=0,
- mirror=False, flip=False, color=(1.0,1.0,1.0,1.0),
- framerate=0.0, gravity=(LEFT,BOTTOM)):
-
- self.frames = []
- d = None
- if type(file) == type([]):
- d = TexMan.load(file[0])
- self.frames.append(d['texture'])
- for f in file[1:]:
- self.frames.append(TexMan.load(f)['texture'])
- else:
- d = TexMan.load(file)
- self.frames.append(d['texture'])
- self.size = d['size']
- self.tsize = d['tsize']
-
- self.rotation = rotation
- self.mirror = mirror
- self.flip = flip
- self.color = color
- self.setFramerate(framerate)
- self.scale = scale
- self.position = position
- self.gravity = gravity
-
- self.fixHitbox()
- self.gen_displaylist()
-
-
- def gen_displaylist(self):
- if self.displaylists == None:
- n = glGenLists(len(self.frames))
- self.displaylists = range(n,n+len(self.frames))
- c = [0,0]
- for i in range(0,2):
- if self.gravity[i] == LEFT:
- c[i] = 0
- elif self.gravity[i] == CENTER:
- c[i] = -self.size[i]/2
- elif self.gravity[i] == RIGHT:
- c[i] = -self.size[i]
- else:
- print "Invalid gravity:",x
- c[i] = 0
- if self.mirror:
- c[0] -= self.tsize[0] - self.size[0]
- if self.flip:
- c[1] -= self.tsize[1] - self.size[1]
- vertexarray = array(
- ((c[0],c[1],0),(c[0]+self.tsize[0],c[1],0),
- (c[0]+self.tsize[0],c[1]+self.tsize[1],0),
- (c[0],c[1]+self.tsize[1],0))
- ,'f')
- if self.flip and self.mirror:
- texcoordarray = array(
- ((0.0,0.0),(0.0,1.0),(1.0,1.0),(1.0,0.0))
- ,'f')
- elif self.flip:
- texcoordarray = array(
- ((0.0,1.0),(0.0,0.0),(1.0,0.0),(1.0,1.0))
- ,'f')
- elif self.mirror:
- texcoordarray = array(
- ((1.0,1.0),(1.0,0.0),(0.0,0.0),(0.0,1.0))
- ,'f')
- else:
- texcoordarray = array(
- ((1.0,0.0),(1.0,1.0),(0.0,1.0),(0.0,0.0))
- ,'f')
- index = array((0,1,2,3),'i')
- for i in range(0,len(self.frames)):
- glNewList(self.displaylists[i], GL_COMPILE)
- glEnable(GL_TEXTURE_2D)
- glBindTexture(GL_TEXTURE_2D, self.frames[i])
- glColor4fv(self.color)
- glVertexPointer(3, GL_FLOAT, 0, vertexarray)
- glTexCoordPointer(2, GL_FLOAT, 0, texcoordarray)
- glDrawArrays(GL_QUADS, 0, 4)
- glEndList()
-
-
- def setPosition(self, x, y):
- self.position = (x,y)
- self.hitbox.topleft = (x + self.hitbox_offset[0], y + self.hitbox_offset[1])
-
-
- def setColor(self, r, g, b, a = 1.0):
- self.color = (r,g,b,a)
- self.gen_displaylist()
-
-
- def setScale(self, scale):
- self.scale = scale
- self.fixHitbox()
-
-
- def fixHitbox(self):
- self.hitbox_offset = [0,0]
- for i in range(0,2):
- if self.gravity[i] == -1:
- self.hitbox_offset[i] = 0
- elif self.gravity[i] == 0:
- self.hitbox_offset[i] = -(self.size[i] * self.scale) / 2
- elif self.gravity[i] == 1:
- self.hitbox_offset[i] = -(self.size[i] * self.scale)
- self.hitbox = pygame.Rect((self.position[0] + self.hitbox_offset[0], self.position[1] + self.hitbox_offset[1]), (self.size[0] * self.scale, self.size[1] * self.scale))
-
-
- def setGravity(self, xg, yg, genlist=True):
- if self.gravity != (xg,yg):
- self.gravity = (xg,yg)
- if genlist:
- self.gen_displaylist()
- self.fixHitbox()
-
-
- def setFramerate(self, rate):
- self.framerate = rate
- if self.framerate != 0.0:
- self.anim_period = int(1000/rate)
- if self.anim_t == 0:
- self.reset()
- else:
- self.stop()
-
-
- def reset(self):
- self.anim_t = pygame.time.get_ticks()
- self.frameno = 0
-
-
- def play(self):
- if len(self.frames) > 1:
- self.animating = True
-
-
- def stop(self):
- self.animating = False
-
-
- def draw(self):
- self.drawAt(self.position)
-
-
- def drawAt(self,pos):
- if self.animating:
- now = pygame.time.get_ticks()
- dt = now - self.anim_t
- if dt > self.anim_period:
- self.frameno = (self.frameno + int(dt / self.anim_period)) % len(self.frames)
- self.anim_t = now
- glPushMatrix()
- glTranslatef(pos[0], pos[1], 0.0)
- if self.rotation != 0:
- glRotatef(self.rotation, 0.0, 0.0, -1.0)
- if self.scale != 1.0:
- glScalef(self.scale, self.scale, 1.0)
- glCallList(self.displaylists[self.frameno])
- if self.showup:
- glDisable(GL_TEXTURE_2D)
- glColor3f(0.0, 0.0, 1.0)
- glBegin(GL_TRIANGLES)
- glVertex(-5, 0, 0)
- glVertex(0, 20, 0)
- glVertex(5, 0, 0)
- glEnd()
- glPopMatrix()
- if self.showhitbox:
- glDisable(GL_TEXTURE_2D)
- glColor3f(1.0, 1.0, 1.0)
- glBegin(GL_LINE_LOOP)
- glVertex(self.hitbox.left, self.hitbox.bottom, 0)
- glVertex(self.hitbox.left, self.hitbox.top, 0)
- glVertex(self.hitbox.right, self.hitbox.top, 0)
- glVertex(self.hitbox.right, self.hitbox.bottom, 0)
- glEnd()
-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.bbox = self.collage.bbox
- 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()
-import pygame
-from OpenGL.GL import *
-import math
-
-class Surface:
- fuzz = 20
- def __init__(self, p1, p2):
- self.p1 = p1
- self.p2 = p2
- self.dx = p2[0] - p1[0]
- self.dy = p2[1] - p1[1]
- try:
- self.m_x = float(self.dy) / float(self.dx)
- except ZeroDivisionError:
- self.m_x = 1e999
- self.b_x = p1[1] - self.m_x * p1[0]
- try:
- self.m_y = float(self.dx) / float(self.dy)
- except ZeroDivisionError:
- self.m_y = 1e999
- self.b_y = p1[0] - self.m_y * p1[1]
-
- # A collision rect for fast collision culling. Only surfaces
- # whose initial rect collision check passes will run the
- # detailed check
- self.rect = pygame.Rect(p1,(self.dx,self.dy))
- self.rect.normalize()
- #self.rect.width += 1
- #self.rect.height += 1
-
-
- def lineSideTest(self, point):
- if self.m_x > -1.0 and self.m_x < 1.0:
- if self.dx > 0:
- if point[1] < self.m_x * point[0] + self.b_x:
- return True
- else:
- if point[1] > self.m_x * point[0] + self.b_x:
- return True
- else:
- if self.dy > 0:
- if point[0] > self.m_y * point[1] + self.b_y:
- return True
- else:
- if point[0] < self.m_y * point[1] + self.b_y:
- return True
- return False
-
-
- def collidePoint(self, point):
- if self.rect.collidepoint(point[0],point[1]):
- return self.lineSideTest(point)
-
-
- def collideRect(self, rect):
- if not self.rect.colliderect(rect):
- return False
- #print self.collidePoint(rect.bottomleft), self.collidePoint(rect.bottomright), self.collidePoint(rect.topright), self.collidePoint(rect.topright)
- return self.lineSideTest(rect.bottomleft) or \
- self.lineSideTest(rect.bottomright) or \
- self.lineSideTest(rect.topright) or \
- self.lineSideTest(rect.topright)
-
-
- def draw(self):
- angle = math.atan(self.m_x)
- if self.dx < 0 or self.dy < 0:
- arrow = (angle+3*math.pi/2) % (2*math.pi)
- else:
- arrow = (angle+math.pi/2) % (2*math.pi)
- anglepoint = (math.cos(arrow) * 10, math.sin(arrow) * 10)
-
- glPushMatrix()
- glDisable(GL_TEXTURE_2D)
-
- if self.dx > 0:
- py = self.rect.top # rects are upside-down in our space
- else:
- py = self.rect.bottom - 1
- if self.dy > 0:
- px = self.rect.right - 1
- else:
- px = self.rect.left
- glColor4f(1.0,0.0,0.0,0.25)
- glBegin(GL_TRIANGLES)
- glVertex3f(self.p2[0], self.p2[1], 0)
- glVertex3f(self.p1[0], self.p1[1], 0)
- glVertex3f(px, py, 0)
- glEnd()
-
- glColor3f(1.0,1.0,1.0)
- glBegin(GL_LINES)
- glVertex3f(self.p1[0],self.p1[1],0)
- glVertex3f(self.p2[0],self.p2[1],0)
- glVertex3f(self.rect.centerx, self.rect.centery, 0)
- glVertex3f(self.rect.centerx + anglepoint[0], self.rect.centery + anglepoint[1], 0)
- glEnd()
-
- glPopMatrix()
-
-
-class Solid:
- def __init__(self, p1, p2):
- self.rect = pygame.Rect(p1,(p2[0]-p1[0],p2[1]-p1[1]))
- self.rect.normalize()
-
-
- def collidePoint(self, point):
- return self.rect.collidepoint(point[0],point[1])
-
-
- def collideRect(self, rect):
- return bool(self.rect.colliderect(rect))
-
-
- def draw(self):
- glPushMatrix()
- glDisable(GL_TEXTURE_2D)
- glColor4f(1.0,0.0,0.0,0.25)
- glBegin(GL_QUADS)
- glVertex(self.rect.left, self.rect.bottom, 0)
- glVertex(self.rect.left, self.rect.top, 0)
- glVertex(self.rect.right, self.rect.top, 0)
- glVertex(self.rect.right, self.rect.bottom, 0)
- glEnd()
- glColor3f(1.0,1.0,1.0)
- glBegin(GL_LINE_LOOP)
- glVertex(self.rect.left, self.rect.bottom, 0)
- glVertex(self.rect.left, self.rect.top, 0)
- glVertex(self.rect.right, self.rect.top, 0)
- glVertex(self.rect.right, self.rect.bottom, 0)
- glEnd()
- glPopMatrix()
-
-
-class SurfaceSet:
- surfaces = None
-
- def __init__(self, file = None):
- self.surfaces = []
- if file is not None:
- self.parse(file)
-
-
- def parse(self, file):
- self.surfaces = []
-
- mf = (1,1)
- f = open(file,'r')
- for line in f:
- args = line.split()
- if not args:
- continue
- cmd = args[0]
- args = args[1:]
- if cmd == 'tilesize':
- mf = (int(args[0]),int(args[1]))
- elif cmd == 'surface':
- p1 = (int(args[0])*mf[0],int(args[1])*mf[1])
- p2 = (int(args[2])*mf[0],int(args[3])*mf[1])
- self.surfaces.append(Surface(p1,p2))
- elif cmd == 'solid':
- p1 = (int(args[0])*mf[0],int(args[1])*mf[1])
- p2 = (int(args[2])*mf[0],int(args[3])*mf[1])
- self.surfaces.append(Solid(p1,p2))
- f.close()
-
-
- def collidePoint(self,point):
- colliders = filter(lambda x: x.rect.collidepoint(point), self.surfaces)
- if not colliders:
- return False
- for c in colliders:
- if not c.collidePoint(point):
- return False
- return True
-
-
- def collideRect(self, rect):
- colliders = filter(lambda x: x.rect.colliderect(rect), self.surfaces)
- if not colliders:
- return False
- #print [(c,c.rect,c.collideRect(rect)) for c in colliders]
- for c in colliders:
- if not c.collideRect(rect):
- return False
- return True
-
-
- def draw(self):
- for s in self.surfaces:
- s.draw()
-
-
- def append(self, s):
- return self.surfaces.append(s)
-
-
- def remove(self, s):
- return self.surfaces.remove(s)
-from OpenGL.GL import *
-from OpenGL.GLU import *
-import pygame
-from pygame.locals import *
-from Numeric import array,reshape,concatenate
-
-from util import *
-import config
-
-sprites = {}
-
-def load(file):
- global sprites
- if sprites.has_key(file):
- return sprites[file]
-
- image = pygame.image.load(file)
- w = image.get_width()
- h = image.get_height()
- tw = n2ceil(w)
- th = n2ceil(h)
- if config.square_textures:
- tw = th = max(tw,th)
- newImage = pygame.Surface((tw,th), SRCALPHA, 32)
- newImage.fill((255,255,255,0))
- newImage.blit(image, (0, th - h)) # We want this in the lower corner
-
- # Freaking pygame making us do this by hand...
- colors = pygame.surfarray.pixels3d(newImage)
- alphas = pygame.surfarray.pixels_alpha(newImage)
- alphas = reshape(alphas, (tw,th,1))
- pixels = concatenate((colors,alphas),2)
-
- # Create Texture
- texture = glGenTextures(1)
- glBindTexture(GL_TEXTURE_2D, texture)
- glTexImage2Dub(GL_TEXTURE_2D, 0, GL_RGBA8, 0, GL_RGBA, pixels)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
-
- sprites[file] = {'texture':texture,'size':(w,h),'tsize':(tw,th)}
- return sprites[file]
-#!/usr/bin/env python
-
-import random
-import sys
-import pygame
-from pygame.locals import *
-from Sprite import Sprite
-import Engine
-from Numeric import array,reshape
-
-try:
- from OpenGL.GL import *
- from OpenGL.GLU import *
-except:
- print 'This requires PyOpenGL'
- raise SystemExit
-
-
-#resolution = (1280,1024)
-resolution = (640,480)
-#resolution = (320,240)
-
-if sys.argv[1:]:
- spritefile = sys.argv[1:]
-else:
- spritefile = 'img/ball.png'
-
-class Bouncer:
- def __init__(self):
- self.velocity = [random.uniform(-10,10),random.uniform(-10,10)]
- self.sprite = Sprite(spritefile)
- self.sprite.framerate = 9.0
- self.sprite.play()
- #self.sprite.color = (random.random(),random.random(),random.random(), 0.5)
- self.sprite.position = [random.randrange(0,resolution[0]),random.randrange(0,resolution[1])]
- self.sprite.gen_displaylist()
- self.draw = self.sprite.draw
-
- def update(self):
- if self.sprite.position[0] < 0:
- self.sprite.position[0] = 0
- self.velocity[0] = -self.velocity[0]
- elif self.sprite.position[0] > resolution[0]:
- self.sprite.position[0] = resolution[0]
- self.velocity[0] = -self.velocity[0]
- if self.sprite.position[1] < 0:
- self.sprite.position[1] = 0
- self.velocity[1] = -self.velocity[1]
- elif self.sprite.position[1] > resolution[1]:
- self.sprite.position[1] = resolution[1]
- self.velocity[1] = -self.velocity[1]
-
- #self.velocity[1] -= 1.0
-
- self.sprite.position[0] += self.velocity[0]
- self.sprite.position[1] += self.velocity[1]
-
-Engine.init(resolution)
-
-bouncers = []
-for i in range(0,1000):
- h = Bouncer()
- bouncers.append(h)
-
-pygame.display.set_caption("bouncetest")
-
-def update():
- for b in bouncers:
- b.update()
-
-Engine.updaters.append(update)
-
-def draw():
- for b in bouncers:
- b.draw()
-
-Engine.drawers.append(draw)
-
-n = 0
-def fpsthing():
- global n
- n += 1
- if n == 100:
- fps = "%0.1f FPS" % Engine.fpsman.get_fps()
- pygame.display.set_caption("bouncetest [%s]" % fps)
- print fps
- n = 0
-
-Engine.updaters.append(fpsthing)
-
-Engine.run()
-# If your card has trouble with non-square textures (my TNT seems to), set this
-# to true to make all textures square
-square_textures = True
-#scalefactor = 2
-#fullscreen = True
-tilesize 64 64
-bbox 19 7
-texture img/block.png
-tile 0 0
-tile 1 0
-tile 2 0
-tile 3 0
-tile 4 0
-tile 5 0
-tile 6 0
-tile 7 0
-tile 8 0
-tile 9 0
-
-tile 3 1
-tile 4 1
-tile 4 2
-tile 4 3
-tile 4 4
-
-tile 6 1
-tile 6 2
-
-solid 0 0 10 1
-surface 2 1 4 2
-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 11 1
-solid 12 0 19 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
-tilesize 64 64
-texture img/block.png
-tile 0 0
-tile 0 1
-tile 1 0
-tile 1 1
-texture img/star.png
-tile 232 421
-tile 390 193
-tile 548 66
-tile 311 367
-tile 430 12
-tile 88 64
-tile 90 123
-tile 286 370
-tile 263 100
-tile 338 84
-tile 231 47
-tile 132 415
-tile 399 183
-tile 111 470
-tile 445 67
-tile 427 159
-tile 237 103
-tile 533 223
-tile 26 409
-tile 382 87
-tile 507 82
-tile 329 380
-tile 381 417
-tile 518 317
-tile 378 197
-tile 607 463
-tile 502 274
-tile 406 235
-tile 185 249
-tile 448 412
-tile 418 29
-tile 248 121
-tile 177 85
-tile 327 352
-tile 466 39
-tile 591 306
-tile 106 95
-tile 570 209
-tile 163 466
-tile 100 185
-tile 376 469
-tile 586 33
-tile 383 251
-tile 178 209
-tile 121 211
-tile 408 289
-tile 539 222
-tile 476 245
-tile 282 274
-tile 557 90
-tile 249 80
-tile 563 447
-tile 18 229
-tile 255 244
-tile 358 354
-tile 563 323
-tile 292 28
-tile 497 241
-tile 379 67
-tile 260 51
-tile 159 163
-tile 209 304
-tile 541 28
-tile 635 475
-tile 479 130
-tile 473 45
-tile 127 308
-tile 281 420
-tile 332 28
-tile 177 478
-tile 413 12
-tile 265 403
-tile 216 147
-tile 105 117
-tile 64 417
-tile 164 137
-tile 131 416
-tile 386 67
-tile 511 111
-tile 239 419
-tile 600 445
-tile 356 369
-tile 314 266
-tile 153 473
-tile 239 221
-tile 359 135
-tile 288 133
-tile 511 449
-tile 234 401
-tile 333 71
-tile 40 78
-tile 426 235
-tile 251 195
-tile 324 432
-tile 25 334
-tile 205 37
-tile 219 284
-tile 358 414
-tile 260 395
-tile 202 22
-tile 594 64
-tile 187 39
-tile 71 231
-tile 428 5
-tile 180 302
-tile 435 93
-tile 10 161
-tile 45 113
-tile 224 99
-tile 46 401
-tile 196 465
-tile 432 376
-tile 194 342
-tile 375 76
-tile 437 174
-tile 197 275
-tile 208 92
-tile 195 118
-tile 618 202
-tile 554 406
-#!/usr/bin/env python
-
-import pygame
-from Sprite import *
-from Collage import *
-from Surface import *
-from Actor import *
-from Stage import *
-from Camera import *
-import Engine
-
-#resolution = (1280,1024)
-resolution = (640,480)
-#resolution = (320,240)
-
-Engine.init(resolution)
-
-pygame.display.set_caption("platformtest")
-
-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_%d.png' % x for x in range(1,4)])
-candelabra.setPosition(64, 64)
-candelabra.setScale(2.0)
-candelabra.setGravity(CENTER, BOTTOM)
-candelabra.setFramerate(10.0)
-candelabra.play()
-
-main.add(candelabra)
-Engine.drawers.append(stage.draw)
-
-camera = Camera(stage)
-camera.setBbox(main.bbox)
-Engine.updaters.append(camera.update)
-
-class Richter(Actor):
- def __init__(self, surfaces=None):
- Actor.__init__(self, surfaces)
-
- self.defineState('walk_l',
- Sprite(['Sprites/Richter/richter%d.png' % n for n in range(0,8)],
- framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
- self.defineState('walk_r',
- Sprite(['Sprites/Richter/richter%d.png' % n for n in range(0,8)], mirror=True,
- framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
- self.defineState('idle_l',
- Sprite("Sprites/Richter/richter0.png", scale=2.0, gravity=(CENTER,BOTTOM)))
- self.defineState('idle_r',
- Sprite("Sprites/Richter/richter0.png", mirror=True, scale=2.0, gravity=(CENTER,BOTTOM)))
- self.setState('idle_r')
-
- self.delta = [0,0]
- self.jumping = False
- self.grounded = self.collideDelta((0,-1))
-
-
- def jump(self):
- if not self.jumping and self.grounded:
- self.delta[1] = 15.0
- self.jumping = True
- self.grounded = False
-
-
- def jumpCancel(self):
- self.jumping = False
- if self.delta[1] > 0.0:
- self.delta[1] = 0.0
-
-
- def bounce(self):
- self.delta[1] = -self.delta[1]
-
-
- def walkRight(self):
- self.delta[0] = 2
- self.setState('walk_r')
-
-
- def walkLeft(self):
- self.delta[0] = -2
- self.setState('walk_l')
-
-
- def idle(self):
- if self.currentstate == 'walk_l':
- self.setState('idle_l')
- else:
- self.setState('idle_r')
- self.delta[0] = 0
-
-
- def update(self):
- if self.grounded:
- self.delta[1] = 0
- else:
- if self.delta[1] > -15.0:
- self.delta[1] -= 0.5
- if self.grounded and self.delta[0] != 0:
- # Climb up inclines (visually poetic, is it not?)
- if self.collideDelta(self.delta):
- delta = list(self.delta)
- for delta[1] in range(1, abs(delta[0]) + 1):
- if not self.collideDelta(delta):
- self.delta = delta
- break
- # ... and walk down slopes
- else:
- self.delta[1] = -abs(self.delta[0])
- self.move(self.delta)
- self.grounded = self.collideDelta((0,-1))
-
-
-richter = Richter(stage.surfaces)
-richter.setPosition(32, 64)
-main.add(richter)
-camera.setFocus(richter)
-
-class Zombie(Actor):
- def __init__(self, surfaces=None):
- Actor.__init__(self, surfaces)
-
- self.defineState('walk_r',
- Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
- framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
- self.defineState('walk_l',
- Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
- mirror=True, framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
-
- self.setState('walk_r')
- self.direction = 0
- self.disabled = 0
- self.topbox = pygame.Rect(self.states['walk_l'].hitbox)
- self.topbox.height = 10
-
- def update(self):
- if self.disabled > 0:
- if self.disabled % 2:
- self.setState('walk_r')
- else:
- self.setState('walk_l')
- self.disabled -= 1
- if self.disabled == 0:
- if self.direction == 0:
- self.setState('walk_r')
- else:
- self.setState('walk_l')
- else:
- if self.direction == 0:
- if self.move((1, 0)):
- self.direction = 1
- self.setState('walk_l')
- elif self.direction == 1:
- if self.move((-1, 0)):
- self.direction = 0
- self.setState('walk_r')
-
- self.topbox.bottom = self.position[1] + 72
- self.topbox.left = self.position[0] - 31
- if self.topbox.collidepoint(richter.position):
- self.disabled = 120
- richter.bounce()
-
-zombie = Zombie(stage.surfaces)
-zombie.setPosition(512, 64)
-main.add(zombie)
-
-
-def input(e):
- if e.type == pygame.KEYDOWN:
- if e.key == pygame.K_LEFT:
- richter.walkLeft()
- elif e.key == pygame.K_RIGHT:
- richter.walkRight()
- elif e.key == pygame.K_UP:
- richter.jump()
- elif e.key == pygame.K_1:
- stage.toggleDrawSurfaces()
- elif e.key == pygame.K_2:
- camera.clearBbox()
- elif e.key == pygame.K_3:
- camera.setBbox(main.bbox)
- elif e.key == pygame.K_ESCAPE:
- Engine.stop()
- elif e.type == pygame.KEYUP:
- if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
- richter.idle()
- elif e.key == pygame.K_UP:
- richter.jumpCancel()
- elif e.type == pygame.QUIT:
- Engine.stop()
-
-Engine.eventhandlers.append(input)
-
-def update():
- richter.update()
- if richter.position[1] < 0:
- richter.position = [richter.position[0],480]
- zombie.update()
-
-Engine.updaters.append(update)
-
-n = 0
-def fpsthing():
- global n
- n += 1
- if n == 100:
- fps = "%0.1f FPS" % Engine.fpsman.get_fps()
- pygame.display.set_caption("platformtest [%s]" % fps)
- print fps
- n = 0
-
-Engine.updaters.append(fpsthing)
-
-Engine.run()
-#!/usr/bin/env python
-
-import pygame
-from Sprite import *
-from Surface import *
-import Engine
-
-#resolution = (1280,1024)
-resolution = (640,480)
-#resolution = (320,240)
-
-Engine.init(resolution)
-
-pygame.display.set_caption("surfacetest")
-
-surfaces = SurfaceSet('data/example1')
-# Octagon
-#surfaces.append(Surface( (520,240),(461,98) ))
-#surfaces.append(Surface( (461,98),(319,40) ))
-#surfaces.append(Surface( (319,40),(178,98) ))
-#surfaces.append(Surface( (178,98),(120,240) ))
-#surfaces.append(Surface( (120,240),(178,381) ))
-#surfaces.append(Surface( (178,381),(320,440) ))
-#surfaces.append(Surface( (320,440),(461,381) ))
-#surfaces.append(Surface( (461,381),(520,240) ))
-
-# Concave
-#surfaces.append(Surface( (200,200), (200,400) ))
-#surfaces.append(Surface( (200,400), (300,300) ))
-#surfaces.append(Surface( (300,300), (400,300) ))
-#surfaces.append(Surface( (400,300), (500,400) ))
-#surfaces.append(Surface( (500,400), (500,200) ))
-#surfaces.append(Surface( (500,200), (200,200) ))
-
-player = Sprite('img/star.png')
-player.setGravity(CENTER, CENTER)
-player.position = [resolution[0]/2,resolution[1]/2]
-
-class gamestate:
- direction = [0,0]
- colliding = False
-
-def input(e):
- if e.type == pygame.KEYDOWN:
- if e.key == pygame.K_LEFT:
- gamestate.direction[0] = -1
- elif e.key == pygame.K_RIGHT:
- gamestate.direction[0] = 1
- elif e.key == pygame.K_DOWN:
- gamestate.direction[1] = -1
- elif e.key == pygame.K_UP:
- gamestate.direction[1] = 1
- elif e.type == pygame.KEYUP:
- if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
- gamestate.direction[0] = 0
- elif e.key == pygame.K_UP or e.key == pygame.K_DOWN:
- gamestate.direction[1] = 0
-
-Engine.eventhandlers.append(input)
-
-
-def update():
- player.position[0] += gamestate.direction[0]
- player.position[1] += gamestate.direction[1]
- if surfaces.collidePoint(player.position):
- if not gamestate.colliding:
- player.setColor(1.0,0.2,0.2)
- gamestate.colliding = True
- else:
- if gamestate.colliding:
- player.setColor(1.0,1.0,1.0)
- gamestate.colliding = False
-
-Engine.updaters.append(update)
-
-def draw():
- surfaces.draw()
- player.draw()
-
-Engine.drawers.append(draw)
-
-Engine.run()
-def n2ceil(n):
- "Find the next power of two greater than or equal to n"
- assert n > 0
- x = 1
- while x <= 1024:
- if x >= n:
- return x
- x <<= 1
- # FAIL
- return 1024