def load_bkgr_cache(self, level_fname, tiles_fname): """ try and load the painted background from cache. Returns True if it can load the background. Otherwise False. """ if not CACHE_USE_LEVEL_CACHE: return False #try: # check if the image is in cache. exts = [".jpg", ".tga"] found_image = False for ext in exts: cached_fname = data_dir('cache', 'levels', os.path.basename(level_fname)) cached_fname = cached_fname[:-4] + ext if os.path.exists(cached_fname): # check if the cached file is newer. fname_stat = os.stat(level_fname) cached_stat = os.stat(cached_fname) if fname_stat.st_mtime > cached_stat.st_mtime: return False found_image = True break #TODO: check the tiles mtime too. if found_image: print "loading :%s:" % cached_fname self.bkgr = pygame.image.load(cached_fname) self.bkgr = self.bkgr.convert() return True else: return False
def load_bkgr_cache(self, level_fname, tiles_fname): """ try and load the painted background from cache. Returns True if it can load the background. Otherwise False. """ if not CACHE_USE_LEVEL_CACHE: return False #try: # check if the image is in cache. exts = [".jpg", ".tga"] found_image = False for ext in exts: cached_fname = data_dir('cache', 'levels', os.path.basename(level_fname)) cached_fname = cached_fname[:-4] + ext if os.path.exists(cached_fname): # check if the cached file is newer. fname_stat = os.stat(level_fname) cached_stat = os.stat( cached_fname ) if fname_stat.st_mtime > cached_stat.st_mtime: return False found_image = True break #TODO: check the tiles mtime too. if found_image: print "loading :%s:" % cached_fname self.bkgr = pygame.image.load(cached_fname) self.bkgr = self.bkgr.convert() return True else: return False
NOTE: not using pygames channel queueing as it only allows one sound to be queued. Also the sound can only be queued on a certain channel. """ import pygame import os import glob import time from pygame.locals import * from const import data_dir SOUND_PATH = data_dir("sounds") EXTENSIONS = [".wav", ".ogg"] def get_sound_list(path=SOUND_PATH, extensions=EXTENSIONS): """ gets a list of sound names without thier path, or extension. """ # load a list of sounds without path at the beginning and .ogg at the end. sound_list = [] for ext in extensions: sound_list.extend(map(lambda x: x[len(path) + 1 : -4], glob.glob(os.path.join(path, "*" + ext)))) return sound_list
Probably have different sets of sounds in here somehow. NOTE: not using pygames channel queueing as it only allows one sound to be queued. Also the sound can only be queued on a certain channel. """ import pygame import os import glob import time from pygame.locals import * from const import data_dir SOUND_PATH = data_dir("sounds") EXTENSIONS = [".wav", ".ogg"] def get_sound_list(path=SOUND_PATH, extensions=EXTENSIONS): """ gets a list of sound names without thier path, or extension. """ # load a list of sounds without path at the beginning and .ogg at the end. sound_list = [] for ext in extensions: sound_list.extend( map(lambda x: x[len(path) + 1:-4], glob.glob(os.path.join(path, "*" + ext)))) return sound_list
def paint(self, screen): sw, sh = screen.get_width(), screen.get_height() tlayer = self.tlayer blayer = self.blayer zlayer = self.zlayer w, h = len(tlayer[0]), len(tlayer) if not hasattr(self, 'bkgr') and not self.load_bkgr_cache( self.level_fname, self.tiles_fname): t1 = time.time() self.bkgr = None v = self.view tmp, y1 = self.tile_to_view((0, 0)) x1, tmp = self.tile_to_view((0, h + 1)) tmp, y2 = self.tile_to_view((w + 1, h + 1)) x2, tmp = self.tile_to_view((w + 1, 0)) ww, hh = x2 - x1, y2 - y1 self.view = pygame.Rect(x1, y1, ww, hh) s = pygame.Surface((ww, hh)).convert() s.fill((128, 0, 0)) self.paint(s) s2 = pygame.Surface((ww, hh)).convert() for n in [1, 2]: #,4]: s2.blit(s, (0, 0)) s.set_alpha(48) for x, y in [(-16 / n, 0), (16 / n, 0), (0, -8 / n), (0, 8 / n)]: s2.blit(s, (x + random.randrange(-2, 3), y + random.randrange(-2, 3))) s.set_alpha(255) s2, s = s, s2 self.bkgr = s t2 = time.time() print "_" * 40 print "time to paint background: %s" % (t2 - t1) # this is the caching stuff. if CACHE_USE_LEVEL_CACHE: if t2 - t1 > 1.0: # the machine didn't process this fast enough, so we cache it. if not os.path.exists(data_dir('cache')): os.mkdir(data_dir('cache')) if not os.path.exists(data_dir('cache', 'levels')): os.mkdir(data_dir('cache', 'levels')) try: cached_fname = data_dir( 'cache', 'levels', os.path.basename(self.level_fname)) pygame.image.save(self.bkgr, cached_fname) except: pass # no cache for this person. Maybe no permission. iso_w, iso_h, iso_z, tile_w, tile_h, base_w, base_h = self.iso_w, self.iso_h, self.iso_z, self.tile_w, self.tile_h, self.base_w, self.base_h base_h2 = base_h / 2 base_w2 = base_w / 2 bot = tile_h / base_h2 todo_max = sh / base_h2 + bot #todo = [[] for y in xrange(0,todo_max)] todo = {} self.view.w, self.view.h = sw, sh view = self.view adj = self.adj = pygame.Rect(-self.view.x, -self.view.y, 0, 0) shift = pygame.Rect(adj.x, adj.y / base_h2 * base_h2, 0, 0) for s in self.sprites: self.sprite_calc_irect(s) if s.irect.colliderect(self.view): x, y = self.iso_to_view((s.rect.centerx, s.rect.centery)) #v = (y+adj.y)/base_h2 - 1 #v = (y+shift.y)/base_h2 -1 #if v >= 0 and v < todo_max: # todo[v].append((s,s.image,s.irect)) v = y / base_h2 - 1 if v not in todo: todo[v] = [] todo[v].append((s, s.image, s.irect)) if hasattr(s, 'effect'): e = s.effect if hasattr(e, 'zloop'): z = 0 if hasattr(s, 'z'): z = s.z irect = s.irect e.zloop((s.irect.x + irect.w / 2, s.irect.y + irect.h / 2 + z), v) for z, img, pos in e.zpaint(): if z not in todo: todo[z] = [] todo[z].append( (None, img, pygame.Rect(pos[0], pos[1], 1, 1))) #else: print 'doesnt fit',v w, h = len(tlayer[0]), len(tlayer) tiles = self.tiles if self.bkgr != None: #"" if self.bounds == None: tmp, y1 = self.tile_to_view((0, 0)) x1, tmp = self.tile_to_view((0, h + 1)) tmp, y2 = self.tile_to_view((w + 1, h + 1)) x2, tmp = self.tile_to_view((w + 1, 0)) self.bounds = pygame.Rect(x1, y1, x2 - x1, y2 - y1) #"" if self.bounds != None: self.view.clamp_ip(self.bounds) #print self.bounds ox, oy = self.screen_to_tile((0, 0)) sx, sy = self.iso_to_view((ox * iso_w, oy * iso_h)) dx, dy = sx - self.view.x, sy - self.view.y if self.bkgr == None: print 'paint the background!!!' t1 = time.time() for i2 in xrange(-bot, self.view.h / base_h2 + bot): tx, ty = ox + i2 / 2 + i2 % 2, oy + i2 / 2 x, y = (i2 % 2) * base_w2 + dx, i2 * base_h2 + dy #to adjust for the -1 in i1 x, tx, ty = x - base_w, tx - 1, ty + 1 for i1 in xrange(-1, self.view.w / base_w + 2): #NOTE: not sure why +2 #print tx,ty if ty >= 0 and ty < h and tx >= 0 and tx < w: z = zlayer[ty][tx] * iso_z if blayer != None: n = blayer[ty][tx] if n != 0: t = tiles[n] if t != None and t.image != None: screen.blit(t.image, (x - base_w2, y + z)) tx += 1 ty -= 1 x += base_w t2 = time.time() print "*" * 40 print "time to paint background: %s" % (t2 - t1) return tmp, y1 = self.tile_to_view((0, 0)) x1, tmp = self.tile_to_view((0, h + 1)) tmp, y2 = self.tile_to_view((w + 1, h + 1)) x2, tmp = self.tile_to_view((w + 1, 0)) #print x1,y1,adj screen.blit(self.bkgr, (x1 + adj.x, y1 + adj.y)) for i2 in xrange(-bot, self.view.h / base_h2 + bot): tx, ty = ox + i2 / 2 + i2 % 2, oy + i2 / 2 x, y = (i2 % 2) * base_w2 + dx, i2 * base_h2 + dy #to adjust for the -1 in i1 x, tx, ty = x - base_w, tx - 1, ty + 1 for i1 in xrange(-1, self.view.w / base_w + 2): #NOTE: not sure why +2 if ty >= 0 and ty < h and tx >= 0 and tx < w: z = zlayer[ty][tx] * iso_z # if blayer != None: # n = blayer[ty][tx] # if n != 0: # t = tiles[n] # if t != None and t.image != None: # screen.blit(t.image,(x-base_w2,y+z)) n = tlayer[ty][tx] if n != 0: t = tiles[n] if t != None and t.image != None: screen.blit(t.image, (x - base_w2, y - (t.image_h - base_h) + z)) tx += 1 ty -= 1 x += base_w #for s,img,irect in todo[y/base_h2]: #if i2 in todo: zz = (y - adj.y) / base_h2 if zz not in todo: continue for s, img, irect in todo[zz]: z = 0 if hasattr(s, 'z'): z = s.z screen.blit(img, (irect.x + adj.x, irect.y + adj.y + z)) if hasattr(s, 'effect'): if not hasattr(s.effect, 'zloop'): s.effect.loop((s.irect.x + irect.w / 2, s.irect.y + irect.h / 2 + z)) s.effect.paint( screen, (-adj.x, -adj.y) ) #(irect.x+adj.x+irect.w/2,irect.y+adj.y+irect.h/2)) self.updates = [] return [pygame.Rect(0, 0, screen.get_width(), screen.get_height())]
def paint(self,screen): sw,sh = screen.get_width(),screen.get_height() tlayer = self.tlayer blayer = self.blayer zlayer = self.zlayer w,h = len(tlayer[0]),len(tlayer) if not hasattr(self,'bkgr') and not self.load_bkgr_cache(self.level_fname, self.tiles_fname): t1 = time.time() self.bkgr = None v = self.view tmp,y1 = self.tile_to_view((0,0)) x1,tmp = self.tile_to_view((0,h+1)) tmp,y2 = self.tile_to_view((w+1,h+1)) x2,tmp = self.tile_to_view((w+1,0)) ww,hh = x2-x1,y2-y1 self.view = pygame.Rect(x1,y1,ww,hh) s = pygame.Surface((ww,hh)).convert() s.fill((128,0,0)) self.paint(s) s2 = pygame.Surface((ww,hh)).convert() for n in [1,2]: #,4]: s2.blit(s,(0,0)) s.set_alpha(48) for x,y in [(-16/n,0),(16/n,0),(0,-8/n),(0,8/n)]: s2.blit(s,(x+random.randrange(-2,3),y+random.randrange(-2,3))) s.set_alpha(255) s2,s = s,s2 self.bkgr = s t2 = time.time() print "_" * 40 print "time to paint background: %s" % (t2 - t1) # this is the caching stuff. if CACHE_USE_LEVEL_CACHE: if t2 - t1 > 1.0: # the machine didn't process this fast enough, so we cache it. if not os.path.exists(data_dir('cache')): os.mkdir(data_dir('cache')) if not os.path.exists(data_dir('cache', 'levels')): os.mkdir(data_dir('cache', 'levels')) try: cached_fname = data_dir('cache', 'levels', os.path.basename(self.level_fname)) pygame.image.save(self.bkgr, cached_fname) except: pass # no cache for this person. Maybe no permission. iso_w,iso_h,iso_z,tile_w,tile_h,base_w,base_h = self.iso_w,self.iso_h,self.iso_z,self.tile_w,self.tile_h,self.base_w,self.base_h base_h2 = base_h/2 base_w2 = base_w/2 bot = tile_h/base_h2 todo_max = sh/base_h2+bot #todo = [[] for y in xrange(0,todo_max)] todo = {} self.view.w,self.view.h = sw,sh view = self.view adj = self.adj = pygame.Rect(-self.view.x,-self.view.y,0,0) shift = pygame.Rect(adj.x,adj.y/base_h2*base_h2,0,0) for s in self.sprites: self.sprite_calc_irect(s) if s.irect.colliderect(self.view): x,y = self.iso_to_view((s.rect.centerx,s.rect.centery)) #v = (y+adj.y)/base_h2 - 1 #v = (y+shift.y)/base_h2 -1 #if v >= 0 and v < todo_max: # todo[v].append((s,s.image,s.irect)) v = y/base_h2 -1 if v not in todo: todo[v] = [] todo[v].append((s,s.image,s.irect)) if hasattr(s,'effect'): e = s.effect if hasattr(e,'zloop'): z = 0 if hasattr(s,'z'): z = s.z irect= s.irect e.zloop((s.irect.x+irect.w/2,s.irect.y+irect.h/2+z),v) for z,img,pos in e.zpaint(): if z not in todo: todo[z] = [] todo[z].append((None,img,pygame.Rect(pos[0],pos[1],1,1))) #else: print 'doesnt fit',v w,h = len(tlayer[0]),len(tlayer) tiles = self.tiles if self.bkgr != None: #"" if self.bounds == None: tmp,y1 = self.tile_to_view((0,0)) x1,tmp = self.tile_to_view((0,h+1)) tmp,y2 = self.tile_to_view((w+1,h+1)) x2,tmp = self.tile_to_view((w+1,0)) self.bounds = pygame.Rect(x1,y1,x2-x1,y2-y1) #"" if self.bounds != None: self.view.clamp_ip(self.bounds) #print self.bounds ox,oy = self.screen_to_tile((0,0)) sx,sy = self.iso_to_view((ox*iso_w,oy*iso_h)) dx,dy = sx - self.view.x,sy - self.view.y if self.bkgr == None: print 'paint the background!!!' t1 = time.time() for i2 in xrange(-bot,self.view.h/base_h2+bot): tx,ty = ox + i2/2 + i2%2,oy + i2/2 x,y = (i2%2)*base_w2 + dx,i2*base_h2 + dy #to adjust for the -1 in i1 x,tx,ty = x-base_w,tx-1,ty+1 for i1 in xrange(-1,self.view.w/base_w+2): #NOTE: not sure why +2 #print tx,ty if ty >= 0 and ty < h and tx >= 0 and tx < w: z = zlayer[ty][tx]*iso_z if blayer != None: n = blayer[ty][tx] if n != 0: t = tiles[n] if t != None and t.image != None: screen.blit(t.image,(x-base_w2,y+z)) tx += 1 ty -= 1 x += base_w t2 = time.time() print "*" * 40 print "time to paint background: %s" % (t2 - t1) return tmp,y1 = self.tile_to_view((0,0)) x1,tmp = self.tile_to_view((0,h+1)) tmp,y2 = self.tile_to_view((w+1,h+1)) x2,tmp = self.tile_to_view((w+1,0)) #print x1,y1,adj screen.blit(self.bkgr,(x1+adj.x,y1+adj.y)) for i2 in xrange(-bot,self.view.h/base_h2+bot): tx,ty = ox + i2/2 + i2%2,oy + i2/2 x,y = (i2%2)*base_w2 + dx,i2*base_h2 + dy #to adjust for the -1 in i1 x,tx,ty = x-base_w,tx-1,ty+1 for i1 in xrange(-1,self.view.w/base_w+2): #NOTE: not sure why +2 if ty >= 0 and ty < h and tx >= 0 and tx < w: z = zlayer[ty][tx]*iso_z # if blayer != None: # n = blayer[ty][tx] # if n != 0: # t = tiles[n] # if t != None and t.image != None: # screen.blit(t.image,(x-base_w2,y+z)) n = tlayer[ty][tx] if n != 0: t = tiles[n] if t != None and t.image != None: screen.blit(t.image,(x-base_w2,y-(t.image_h-base_h)+z)) tx += 1 ty -= 1 x += base_w #for s,img,irect in todo[y/base_h2]: #if i2 in todo: zz = (y - adj.y) / base_h2 if zz not in todo: continue for s,img,irect in todo[zz]: z = 0 if hasattr(s,'z'): z = s.z screen.blit(img,(irect.x+adj.x,irect.y+adj.y+z)) if hasattr(s,'effect'): if not hasattr(s.effect,'zloop'): s.effect.loop((s.irect.x+irect.w/2,s.irect.y+irect.h/2+z)) s.effect.paint(screen,(-adj.x,-adj.y))#(irect.x+adj.x+irect.w/2,irect.y+adj.y+irect.h/2)) self.updates = [] return [pygame.Rect(0,0,screen.get_width(),screen.get_height())]