comparison gamelib/gameboard.py @ 202:3074784c93f4

Animation support
author Neil Muller <drnlmuller@gmail.com>
date Fri, 04 Sep 2009 20:23:30 +0000
parents fe1e9c18d4d7
children 653da96db572
comparison
equal deleted inserted replaced
201:fe1e9c18d4d7 202:3074784c93f4
147 self.vid = vid 147 self.vid = vid
148 self.vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size)) 148 self.vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
149 149
150 def paint(self, surface): 150 def paint(self, surface):
151 self.vid.paint(surface) 151 self.vid.paint(surface)
152 # Blit animation frames on top of the drawing
153 x, y = self.vid.view.x, self.vid.view.y
154 for anim in self.gameboard.animations:
155 anim.fix_pos(self.vid)
156 anim.irect.x = anim.rect.x - anim.shape.x
157 anim.irect.y = anim.rect.y - anim.shape.y
158 surface.blit(anim.image, (anim.irect.x - x, anim.irect.y - y))
159 # We don't store anim._irect, since we only update anims if the
160 # image changes, which kills irect
152 161
153 def update(self, surface): 162 def update(self, surface):
154 return self.vid.update(surface) 163 us = []
164 x, y = self.vid.view.x, self.vid.view.y
165 for anim in self.gameboard.animations[:]:
166 """Handle completed animations"""
167 if anim.removed:
168 us.append(pygame.Rect(anim.irect.x - x, anim.irect.y - y,
169 anim.irect.width, anim.irect.height))
170 self.gameboard.animations.remove(anim)
171 # Flag the underlying tiles/sprites to be redrawn
172 self.vid.alayer[anim.pos.y][anim.pos.x]=1
173 us.extend(self.vid.update(surface))
174 for anim in self.gameboard.animations:
175 if anim.updated:
176 anim.fix_pos(self.vid)
177 # setimage has happened
178 anim.irect.x = anim.rect.x - anim.shape.x
179 anim.irect.y = anim.rect.y - anim.shape.y
180 surface.blit(anim.image, (anim.irect.x - x, anim.irect.y - y))
181 anim.updated = 0
182 us.append(pygame.Rect(anim.irect.x - x, anim.irect.y - y,
183 anim.irect.width, anim.irect.height))
184 return us
155 185
156 def move_view(self, x, y): 186 def move_view(self, x, y):
157 self.vid.view.move_ip((x, y)) 187 self.vid.view.move_ip((x, y))
158 188
159 def event(self, e): 189 def event(self, e):
184 self.animal_to_place = None 214 self.animal_to_place = None
185 self.sprite_cursor = None 215 self.sprite_cursor = None
186 self.chickens = set() 216 self.chickens = set()
187 self.foxes = set() 217 self.foxes = set()
188 self.buildings = [] 218 self.buildings = []
219 self.animations = []
189 self.cash = 0 220 self.cash = 0
190 self.eggs = 0 221 self.eggs = 0
191 self.days = 0 222 self.days = 0
192 self.killed_foxes = 0 223 self.killed_foxes = 0
193 self.add_cash(constants.STARTING_CASH) 224 self.add_cash(constants.STARTING_CASH)
590 != self.WOODLAND: 621 != self.WOODLAND:
591 self.kill_fox(fox) 622 self.kill_fox(fox)
592 else: 623 else:
593 self.tv.sprites.remove(fox) 624 self.tv.sprites.remove(fox)
594 self.foxes = set() # Remove all the foxes 625 self.foxes = set() # Remove all the foxes
626
627 def run_animations(self):
628 for anim in self.animations:
629 anim.animate()
595 630
596 def move_foxes(self): 631 def move_foxes(self):
597 """Move the foxes. 632 """Move the foxes.
598 633
599 We return True if there are no more foxes to move or all the 634 We return True if there are no more foxes to move or all the