changeset 5:87e2727bb411

animations
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 11 Mar 2013 23:00:24 +0100
parents 56b6fab47935
children fc17045bef2f
files fhrl/__main__.py
diffstat 1 files changed, 33 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/fhrl/__main__.py	Mon Mar 11 21:47:43 2013 +0100
+++ b/fhrl/__main__.py	Mon Mar 11 23:00:24 2013 +0100
@@ -103,17 +103,22 @@
         self.rect.topleft = x * 48 - 24, y * 32 - 32
         self.x, self.y = x, y
 
+
+    def _walk_anim(self, d):
+        for frame in xrange(8):
+            self.rect.move_ip(DX[d] * 6, DY[d] * 4)
+            yield
+
     def walk(self, d, level):
         x = self.x + DX[d]
         y = self.y + DY[d]
         if not level.grid[y][x]:
             return False
-        self.rect.move_ip(DX[d] * 48, DY[d] * 32)
         self.x, self.y = x, y
-        return True
+        return self._walk_anim(d)
 
     def act(self, level):
-        self.walk(random.randrange(4), level)
+        return self.walk(random.randrange(4), level)
 
 
 class Hero(Mob):
@@ -185,17 +190,25 @@
             screen.fill((0x66, 0x66, 0x66), rect)
         screen.blit(self.background, rect, source)
 
+    def update(self):
+        self.sprites.clear(self.screen, self._sprite_clear)
+        dirty = self.sprites.draw(self.screen)
+        pygame.display.update(dirty)
+        self.clock.tick(24)
 
     def run(self):
         self.scroll_by(0, 0)
         while True:
-            while not self.level.hero.act(self.level):
-                self.sprites.clear(self.screen, self._sprite_clear)
-                dirty = self.sprites.draw(self.screen)
-                pygame.display.update(dirty)
-                self.clock.tick(12)
+            self.animations = []
+            while not self.animations:
+                anim = self.level.hero.act(self.level)
+                if anim:
+                    self.animations.append(anim)
+                self.update()
             for m in self.level.monsters:
-                m.act(self.level)
+                anim = m.act(self.level)
+                if anim:
+                    self.animations.append(anim)
             if self.level.hero.rect.right > self.width - self.margin:
                 self.scroll_by(48, 0)
             if self.level.hero.rect.bottom > self.height - self.margin:
@@ -204,7 +217,16 @@
                 self.scroll_by(-48, 0)
             if self.level.hero.rect.top < self.margin:
                 self.scroll_by(0, -32)
-
-
+            while self.animations:
+                cont = []
+                for anim in self.animations:
+                    try:
+                        anim.next()
+                    except StopIteration:
+                        pass
+                    else:
+                        cont.append(anim)
+                self.animations = cont
+                self.update()
 
 Game().run()