This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

asciisprites yet again

By: a guest on Mar 25th, 2011  |  syntax: Python  |  size: 8.06 KB  |  views: 570  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. """ Inspirated and partly taken from the game Gorillas.py by Al Sweigart.
  2. Visit him (and learn programming games) at:
  3.  
  4.        http://inventwithpython.com
  5. """
  6. import pygame
  7. from pygame.locals import SRCALPHA
  8.  
  9. class Image:
  10.     def __init__(self, ascii, colours={'X': (255, 255, 255, 255)}):
  11.         self.ascii      = ascii
  12.         self.colours    = {}
  13.         for colour in colours:
  14.             self.colours[colour] = colours[colour]
  15.         self.update()
  16.  
  17.     def update(self):
  18.         ''' Update everything. '''
  19.         self.update_ascii()
  20.         self.update_colours()
  21.  
  22.     def update_ascii(self):
  23.         ''' Call this after You changed self.ascii. '''
  24.         self.asciilist  = self.ascii.split('\n')[1:-1]
  25.         self.width      = max([len(x) for x in self.asciilist])
  26.         self.height     = len(self.asciilist)
  27.         self.surface    = pygame.Surface((self.width, self.height), SRCALPHA, 32)
  28.         try: self.surface.fill(self.colours['bg'])
  29.         except KeyError: self.surface.fill((0,0,0,0))
  30.         self.update_colours()
  31.  
  32.     def update_colours(self):
  33.         ''' Call this after You changed self.colours. '''
  34.         for colour in self.colours:
  35.             self.update_colour(colour)
  36.  
  37.     def update_colour(self, colour):
  38.         ''' Update a single colour. Used internally. '''
  39.         if colour in self.colours:
  40.             for y in range(self.height):
  41.                 for x in range(self.width):
  42.                     if self.asciilist[y][x] == colour:
  43.                         self.surface.set_at((x, y), self.colours[colour])
  44.        
  45.     def set_colour(self, key, value):
  46.         ''' Set the colour of any byte in self.colours.'''
  47.         self.colours[key] = value
  48.         self.update_colour(key)
  49.  
  50.     def set_colours(self, colours):
  51.         for i in colours:
  52.             self.colours[i] = colours[i]
  53.         self.update_colours()
  54.  
  55.     def set_ascii(self, ascii):
  56.         self.ascii = ascii
  57.         self.update_ascii()
  58.  
  59.     def get_surface(self):
  60.         return self.surface
  61.  
  62. class Animation:
  63.     ''' self.frames are the indexes in self.timeline of self.images,
  64.        which are the Image(object)s. '''
  65.     def __init__(self, asciis, timeline=['0'], colours={'X': (255, 255, 255, 255)}):
  66.         self.frames     = {}
  67.         self.timeline   = timeline
  68.         for i in asciis:
  69.             self.frames[i] = Image(asciis[i], colours)
  70.         self.curframe   = 0
  71.         self.update_curframe()
  72.         self.update_frames()
  73.  
  74.     def update_curframe(self):
  75.         ''' Update current shown Image. '''
  76.         self.curimage = self.frames[self.timeline[self.curframe]]
  77.         self.surface = self.curimage.surface
  78.  
  79.     def update_frames(self):
  80.         for i in self.frames:
  81.             self.frames[i].update()
  82.  
  83.     def set_colours(self, colours):
  84.         for i in self.frames:
  85.             self.frames[i].set_colours(colours)
  86.  
  87.     def set_colour(self, key, value):
  88.         for i in self.frames:
  89.             self.frames[i].set_colour(key, value)
  90.  
  91.     def swap(self, amount = 1):
  92.         if self.curframe + amount > len(self.timeline) -1:
  93.             self.curframe = 0
  94.         else:
  95.             self.curframe += amount
  96.         self.update_curframe()
  97.  
  98.     def get_image(self):
  99.         frames = []
  100.         for i in self.timeline:
  101.             frames.append(self.frames[i])
  102.         return frames
  103.  
  104.     def get_frames(self):
  105.         images = []
  106.         for i in self.timeline:
  107.             images.append(self.frames[i].surface)
  108.         return images
  109.  
  110.     def get_asciis(self):
  111.         asciis = {}
  112.         for i in self.frames:
  113.             asciis[i] = self.frames[i].ascii
  114.         return asciis
  115.  
  116.  
  117.  
  118. ### Example time:
  119.  
  120. # Data:
  121. image = """
  122. TTTTTTTT  EEEEEEE   SSSSSS  AAAAAAAA
  123. TTTTTTTT  EEEEEEE  SSSSSSS  AAAAAAAA
  124.   TT     EE       SS          AA  
  125.   TT     EEEEE    SSSSSS      AA  
  126.   TT     EEEEE     SSSSSS     AA  
  127.   TT     EE            SS     AA  
  128.   TT     EEEEEEE  SSSSSSS     AA  
  129.   TT     EEEEEEE  SSSSSS      AA  
  130. """
  131.  
  132. animation = {
  133.         '0': """
  134. XXX
  135. XXXXX
  136. XXOXX
  137. XXXXX
  138. XXX
  139. """,    '1': """
  140.  X  
  141. XXX
  142. XXX
  143. XXX
  144.  X  
  145. """,    '2': """
  146.  X  
  147.  X  
  148.  X  
  149.  X  
  150.  X  
  151. """,    '3': """
  152.  X  
  153. XXX
  154. XOX
  155. XXX
  156.  X  
  157. """,    '4': """
  158. XXX
  159. XXOXX
  160. XOXOX
  161. XXOXX
  162. XXX
  163. """}
  164. backwards = ['0','1','2','3','4','3','2','1']
  165. forewards = ['4','3','2','1','0','1','2','3']
  166.  
  167. coloursI = {'T': (255, 0, 0, 255),
  168.             'E': (0, 255, 0, 255),
  169.             'S': (255, 255, 0, 255),
  170.             'A': (0, 0, 255, 255)
  171.     }
  172. coloursB = {'T': (64, 64, 64, 255),
  173.             'E': (128, 128, 128, 255),
  174.             'S': (192, 192, 192, 255),
  175.             'A': (255, 255, 255, 255)
  176.     }
  177. Gold = {'X': (200, 150, 0, 255),
  178.         'O': (255, 255, 0, 255)
  179.     }
  180. Silver={'X': (200, 200, 200, 255),
  181.         'O': (100, 100, 100, 255)
  182.     }
  183. Bronze={'X': (150, 50, 0, 255),
  184.         'O': (200, 75, 0, 255)
  185.     }
  186.  
  187. import sys#, pygame
  188. from pygame.locals import KEYUP, QUIT, K_ESCAPE, K_q, K_LEFT, K_RIGHT, K_SPACE, K_DOWN
  189.  
  190. # I made a symlink 'local' from some directory into the python libs,
  191. # put an empty '__init__.py' file in it and have now a place, to easyly
  192. # use my custom libs globally in all my projects:
  193. #from local.asciisprites import *
  194.  
  195.  
  196. # setup pygame
  197. pygame.init()
  198. clock = pygame.time.Clock()
  199. screen = pygame.display.set_mode((66, 18), SRCALPHA, 32)
  200.  
  201. # setup the objects
  202. image = Image(image, coloursI)
  203. ani1  = Animation(animation, backwards, Gold)
  204. ani2  = Animation(animation, forewards, Gold)
  205.  
  206. gold = True
  207. blind = False
  208.  
  209. while True:
  210.     for e in pygame.event.get():
  211.         if e.type == QUIT:
  212.             # pygame.quit() is needed by idle users.
  213.             pygame.quit()
  214.             sys.exit()
  215.         if e.type == KEYUP:
  216.             if e.key == K_ESCAPE or e.key == K_q:
  217.                 pygame.quit()
  218.                 sys.exit()
  219.             if e.key == K_RIGHT:
  220.                 # one way to change the colours of the image
  221.                 temp = image.colours['S']
  222.                 image.colours['S'] = image.colours['E']
  223.                 image.colours['E'] = image.colours['T']
  224.                 image.colours['T'] = image.colours['A']
  225.                 image.colours['A'] = temp
  226.                 image.update_colours()
  227.             if e.key == K_LEFT:
  228.                 # and another way...
  229.                 temp = image.colours['S']
  230.                 image.set_colour('S', image.colours['A'])
  231.                 image.set_colour('A', image.colours['T'])
  232.                 image.set_colour('T', image.colours['E'])
  233.                 image.set_colour('E', temp)
  234.             if e.key == K_SPACE:
  235.                 # print the asciis stored in ani1 and switch colour blind mode!
  236.                 #print ani1.get_asciis()
  237.                 if blind:
  238.                     blind = False
  239.                     image.set_colours(coloursI)
  240.                     if gold:
  241.                         ani1.set_colours(Gold)
  242.                         ani2.set_colours(Gold)
  243.                     else:
  244.                         ani1.set_colours(Bronze)
  245.                         ani2.set_colours(Bronze)
  246.                 else:
  247.                     image.set_colours(coloursB)
  248.                     ani1.set_colours(Silver)
  249.                     ani2.set_colours(Silver)
  250.                     blind = True
  251.  
  252.             if e.key == K_DOWN:
  253.                 if gold:
  254.                     gold = False
  255.                     if not blind:
  256.                         ani1.set_colours(Bronze)
  257.                         ani2.set_colours(Bronze)
  258.                 else:
  259.                     gold = True
  260.                     if not blind:
  261.                         ani1.set_colours(Gold)
  262.                         ani2.set_colours(Gold)
  263.            
  264.     # fill the window with a grey to show off the transparency, that bugged around so long
  265.     screen.fill((88, 88, 88, 255))
  266.  
  267.     # update and draw ani1
  268.     ani1.swap()
  269.     screen.blit(ani1.surface, (56, 7))
  270.     # update and draw ani2
  271.     ani2.swap()
  272.     screen.blit(ani2.surface, (5,7))
  273.     # draw the TEST-image
  274.     screen.blit(image.surface, (15, 5))
  275.  
  276.     pygame.display.flip()
  277.     clock.tick(6)
clone this paste RAW Paste Data