Commits

Dominic Kexel  committed 41b06da Draft

create surface in art.py

  • Participants
  • Parent commits a53cb0d

Comments (0)

Files changed (2)

+import pygame
+
+from constants import *
+
 __art = {
 'city':
 """
 
 def get_art(key):
   return __art[key]
+  
+def build_surface(tick, img):
+  size = 3
+  splitted = img.split('\n')[1:-1]
+  width = len(splitted[0])
+  height = len(splitted)
+  _city_surf = pygame.surface.Surface((width * size, height * size), pygame.SRCALPHA, 32)
+  _city_surf.fill(BLACK)
+  
+  def getcolor(c):
+    if c == '#': return GREYGREEN
+    if c == '+': return MALLARD
+    if c == ' ': return COLORLESS
+    
+    if not (tick / 60) % 2:
+      if c == 'G': return GREEN
+      if c == 'R': return RED
+    
+    return YELLOW
+        
+  for y, line in enumerate(splitted):
+    for x, char in enumerate(line):
+      r = pygame.rect.Rect(x * size, y * size, size, size)
+      _city_surf.fill(getcolor(char), r)
+        
+  return _city_surf
 from physic import space, to_pymunk, to_pygame
 from sound import *
 from resource import render_cached
-from art import get_art
+from art import get_art, build_surface
 
 
-#TODO cache this image
-def _city_surface(tick, img):
-  size = 3
-  splitted = img.split('\n')[1:-1]
-  width = len(splitted[0])
-  height = len(splitted)
-  _city_surf = pygame.surface.Surface((width * size, height * size), pygame.SRCALPHA, 32)
-  _city_surf.fill(BLACK)
-  
-  def getcolor(c):
-    if c == '#': return GREYGREEN
-    if c == '+': return MALLARD
-    if c == ' ': return COLORLESS
-    
-    if not (tick / 60) % 2:
-      if c == 'G': return GREEN
-      if c == 'R': return RED
-    
-    return YELLOW
-        
-  for y, line in enumerate(splitted):
-    for x, char in enumerate(line):
-      r = pygame.rect.Rect(x * size, y * size, size, size)
-      _city_surf.fill(getcolor(char), r)
-        
-  return _city_surf
+
 
 class GameState(object):
   
   def __init__(self, **kwargs):
     super(Building, self).__init__(**kwargs)
     self._inx = kwargs['inx']
-    self._hit = False
+    self._is_hit = False
     self._hit_img = get_art(self.__class__.__name__.lower() + '_ruin')
     self._img = get_art(self.__class__.__name__.lower())
-    self._surf = _city_surface(self.scene._game.tick, self._img)
-    
+    self._surf = build_surface(self.scene._game.tick, self._img)
   
   def hit(self):
-    self._hit = True
+    self._is_hit = True
+    self._on_hit()
+  
+  def _on_hit(self):
+    pass
+  
+  
+  @property
+  def is_hit(self):
+    return self._is_hit
     
   @property
   def pos(self):
     return pos
 
   def draw(self, screen):
-    self._surf = _city_surface(self.scene._game.tick, self._hit_img if self._hit else self._img)
+    self._surf = build_surface(self.scene._game.tick, self._hit_img if self._is_hit else self._img)
     screen.blit(self._surf, self.pos)
 
 class City(Building):
   def inx_to_center(self):
     return (SCREEN_WIDTH / 10) * (self._inx + (2 if self._inx <= 2 else 3))
   
-  def hit(self):
-    self._hit = True
+  def _on_hit(self):
     self.scene._state.cities[self._inx] = -1
   
 class Silo(Building):
   def inx_to_center(self):
     return (SCREEN_WIDTH / 10) * (self._inx * 4 + 1)
   
-  def hit(self):
-    self._hit = True
+  def _on_hit(self):
     self.scene._state.silos[self._inx] = -1
   
   @property
     return v
       
   def draw(self, screen):
-    if not self._hit and self.rockets:
+    if not self.is_hit and self.rockets:
       v = [e*30 for e in self._get_firing_vetor(self._inx)]
       s = self.inx_to_center(), SCREEN_HEIGHT
       pygame.draw.line(screen, RED, s, add(s, v), 2)
     super(Silo, self).draw(screen)
       
   def fire(self):
-    if not self.rockets or self._hit: return
+    if not self.rockets or self.is_hit: return
     self.rockets -= 1
     self.scene._state.silos[self._inx] -= 1