# # pygSprites.py # import pygame color_map = { 'r':(255, 0, 0), # red 'o':(255,128, 0), # orange 'y':(255,255, 0), # yellow 'g':( 0,255, 0), # green 'b':( 0, 0,255), # blue 'w':(128,128,128), # white 'T':( 1, 1, 1), # transparent ' ':( 1, 1, 1) # transparent } def cmapToSprite(cmap, xscale, yscale) : "Create sprite surface from char-map" lines = cmap.split("\n") mincol = minrow = 9999 maxcol = maxrow = 0 # calculate minimum and maximum row and col for l in range(len(lines)) : line = lines[l] for i in range(len(line)): if line[i] > ' ' : minrow = min(minrow,l) mincol = min(mincol,i) maxrow = max(maxrow,l+1) maxcol = max(maxcol,i+1) # create a surface for the sprite to the appropriate size sprite = pygame.Surface(((maxcol-mincol)*xscale,(maxrow-minrow)*yscale)) transparent = (1,1,1) # transparent color sprite.set_colorkey(transparent) yp = 0 for row in range(minrow,maxrow) : line = lines[row] + " "*maxcol xp = 0 for col in range(mincol,maxcol) : c = line[col] col = color_map.get(c, transparent) # for each char in cmap draw a rect of correct color pygame.draw.rect(sprite,col,(xp,yp,xscale,yscale),0) xp += xscale yp += yscale return sprite # cmaps - character maps for sprites space = """ wwwwwwww wwwwwwww wwwwwwww wwwwwwww wwwwwwww wwwwwwww """ player = """ T T g g g g g TTTTTTT """ robot = """ T r ooo rrrrr ooo TTTTTTT """ floor = """ wwwwwww wwwwwww wwwwwww wwwwwww wwwwwww bbbbbbb """ ladder = """ wwwbwww wwwbwww wwwbwww wwwbwww wwwbwww bbbbb """ lode = """ T T y y y y TTTTTTT """ S_HEIGHT = 300 # Screen dimension in pixels S_WIDTH = 800 # Screen dimension in pixels C_WIDTH = 14 # in pixels C_HEIGHT = 18 # in pixels XSCALE = 2 # pixels horz per letter in cmap YSCALE = 3 # pixels vert per letter in cmap def buildSpritesMap() : sprites = {} for ch,cmap in (('*',lode),("|",ladder),("_",floor), ("^",player), ("&",robot),(" ",space)) : sprites[ch] = cmapToSprite(cmap, XSCALE, YSCALE) return sprites sprites = buildSpritesMap()