Commits

Szymon Wróblewski committed 8d74cfa

Potion uses DirtySprite now

  • Participants
  • Parent commits 28aadf7

Comments (0)

Files changed (4)

File alchemymadness/main.py

 import pygame
 from pygame.locals import *
 
-import skelutil
-
+import resources
 import potion
 
 SCREEN_SIZE = (800, 600)
     screen = pygame.display.set_mode(SCREEN_SIZE)
     clock = pygame.time.Clock()
     going = True
+    
+    resources.init()
 
     screen.fill((0,0,0))
-    background = pygame.image.load(skelutil.data_dir("bg.png")).convert_alpha()
 
-    screen.blit(background, (0,0))
+    screen.blit(resources.background, (0,0))
     pygame.display.flip()
 
     # Initialise level
     pot = potion.Potion()
     pot.set_formula(3, 5, 3)
+    sprites = pygame.sprite.RenderUpdates(pot)
 
     #TEST
     pot.add_color((0,0,255))
     while going:
         time = clock.tick(30)
 
-        rects = []
         events = pygame.event.get()
         for e in events:
             if e.type == KEYDOWN and e.key == K_SPACE:
             if e.type == QUIT or e.type == KEYDOWN and e.key == K_ESCAPE:
                 going = False
 
-        # Update potion
-        pot.update(time)
-        screen.fill(pot.col, pot.rect)
-        rects.append(pot.rect)
-
-        screen.blit(background, (0,0))
+        # Update sprites
+        sprites.clear(screen, resources.background)
+        sprites.update(time)
+        
+        # Draw sprites
+        rects = sprites.draw(screen)
         pygame.display.update(rects)
+        
 
     # End game
     score = pot.get_score()

File alchemymadness/potion.py

 """Mixing potion"""
 
+import pygame
 from pygame.locals import Rect
+from pygame.sprite import DirtySprite
 
-class Potion():
+import resources
+
+class Potion(DirtySprite):
     chems = 0  # Chem count
     formula = (0,0,0)  # Target formula
     col = (0,0,0)  # Current formula
     target_col = None
     max_chems = 0  # Target number of chems
-    start_y = 470
+    rect = Rect(450, 470, 170, 113)
+    image_rect = Rect((0, rect.y), rect.size)
     target_y = None
-    rect = Rect(450, 470, 170, 113)
+    
+    
+    def __init__(self, *groups):
+        super(Potion, self).__init__(*groups)
+        # Image with transparency
+        self.t_image = resources.background.subsurface(self.rect)
+        # Sprite image
+        self._image = pygame.Surface(self.rect.size)
+        self.dirty = 1
+    
+    @property
+    def image(self):
+        if self.dirty == 1:
+            self._image.fill((0,0,0))
+            self._image.fill(self.col, self.image_rect)
+            self._image.blit(self.t_image, (0,0))
+        return self._image
+
 
     def set_formula(self, r=0, g=0, b=0):
         """Set the target formula for the level."""
         self.formula = (r, g, b)
         self.max_chems = sum(self.formula)
         # Offset the rect to be at bottom of potion
-        self.rect.y = self.start_y + self.rect.h
+        self.image_rect.y = self.image_rect.h
 
     def add_color(self, col):
         """
 
         # Update position
         n = self.chems / float(self.max_chems)
-        self.target_y = self.start_y + self.rect.h*(1-n)
+        self.target_y = self.image_rect.h*(1-n)
 
     def get_score(self):
         """Returns a score based on accuracy of the formula."""
     def update(self, time):
         # Move rect smoothly
         if self.target_y is not None:
-            self.rect.y -= 5*(time/1000.)
-            if self.rect.y < self.target_y:
-                self.rect.y = self.target_y
+            self.dirty = 1
+            self.image_rect.y -= 5*(time/1000.)
+            if self.image_rect.y < self.target_y:
+                self.image_rect.y = self.target_y
                 self.target_y = None
         # Lerp colours
         if self.target_col is not None:
+            self.dirty = 1
             diff = (self.diff_col[0] * (time/500.),
                     self.diff_col[1] * (time/500.),
                     self.diff_col[2] * (time/500.))

File alchemymadness/resources.py

+import pygame
+import skelutil
+
+background = None
+red_block = green_block = blue_block = None
+red_small_block = green_small_block = blue_small_block = None
+block = small_block = None
+
+def init():
+    global background, red_block, green_block, blue_block
+    global red_small_block, green_small_block, blue_small_block
+    global block, small_block
+    background = pygame.image.load(skelutil.data_dir("bg.png")).convert_alpha()
+    red_block = pygame.image.load(skelutil.data_dir("red.png")).convert_alpha()
+    green_block = pygame.image.load(skelutil.data_dir("green.png")).convert_alpha()
+    blue_block = pygame.image.load(skelutil.data_dir("blue.png")).convert_alpha()
+    red_small_block = pygame.image.load(skelutil.data_dir("red_.png")).convert_alpha()
+    green_small_block = pygame.image.load(skelutil.data_dir("green_.png")).convert_alpha()
+    blue_small_block = pygame.image.load(skelutil.data_dir("blue_.png")).convert_alpha()
+    block = (red_block, green_block, blue_block)
+    small_block = (red_small_block, green_small_block, blue_small_block)
+    
+    

File alchemymadness/skelutil.py

     """
     global _DATA_DIR
     if _DATA_DIR is None:
-        _DATA_DIR = os.path.join('yourgamename', 'data')
+        _DATA_DIR = os.path.join('alchemymadness', 'data')
         if not os.path.exists(_DATA_DIR):
             _DATA_DIR = os.path.join(os.path.split(__file__)[0], 'data')
     return os.path.join(*([_DATA_DIR] + list(args)))