changeset 34:a7c3f8601a6c

Move sprites to separate module
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 10 Mar 2012 11:53:48 +0100
parents 6f3eac7b166e
children d95d229f6931
files mhrok/__init__.py mhrok/sprites.py mhrok/util.py
diffstat 3 files changed, 131 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/mhrok/__init__.py	Sat Mar 10 11:49:26 2012 +0100
+++ b/mhrok/__init__.py	Sat Mar 10 11:53:48 2012 +0100
@@ -8,88 +8,19 @@
 from pygame import locals as pg
 
 from mhrok import display
-
-
-DX = [-1,  0,  1,  0]
-DY = [ 0, -1,  0,  1]
+from mhrok import sprites
+from mhrok.util import DX, DY
 
 
-class Shadow(pygame.sprite.Sprite):
-    def __init__(self, frames, owner):
-        super(Shadow, self).__init__()
-        self.image = frames[20].copy()
-        self.image.set_alpha(128)
-        self.rect = self.image.get_rect()
-        self.owner = owner
-
-    def update(self):
-        self.rect.center = self.owner.rect.center
-
-class Vision(pygame.sprite.DirtySprite):
-    def __init__(self, owner, radius):
-        super(Vision, self).__init__()
-        self.owner = owner
-        self.blendmode = pg.BLEND_RGBA_MIN
-        self.dirty = 2
-        self.images = {}
-        for r in xrange(40, 65):
-            self.images[r] = self.get_image(r)
-        self.radius = radius
-        self.set_radius(radius)
-        self.rooms = []
-
-    def set_radius(self, radius):
-        self.current_radius = radius
-        self.image = self.images[radius]
-        self.rect = self.image.get_rect()
-        self.rect.center = self.owner.rect.center
-
-    def get_image(self, radius):
-        image = pygame.Surface((radius*5/2, radius*2), pg.SRCALPHA)
-        image.fill((255, 255, 255, 255))
-        for r in xrange(radius, radius-25, -1):
-            pygame.draw.ellipse(
-                    image,
-                    (255, 255, 255, max(0, min(255, (r-radius+25) * 10))),
-                    (radius*5/4-r*5/4, radius-r, 2*r*5/4, 2*r), 0)
-        return image
-
-    def update(self):
-        if self.current_radius > self.radius:
-            self.set_radius(max(self.current_radius - 4, self.radius))
-        elif self.current_radius < self.radius:
-            self.set_radius(min(self.current_radius + 4, self.radius))
-        self.rect.center = self.owner.rect.center
-
-
-class MobSprite(pygame.sprite.Sprite):
-    def __init__(self, frames, (x, y), animation):
-        super(MobSprite, self).__init__()
-        self.animation = animation
-        self.frames = frames
-        self.image = self.frames[0]
-        self.rect = self.image.get_rect()
-        self.frame = 0
-        self.rect.move_ip(4 + 20 * x, 12 * y)
-        self.depth = self.rect.bottom
-        self.direction = None
-
-    def update(self):
-        self.frame = (self.frame + 1) % len(self.animation)
-        self.image = self.frames[self.animation[self.frame]]
-        if self.direction is not None:
-            self.rect.move_ip((DX[self.direction]*5, DY[self.direction]*3))
-            self.depth = self.rect.bottom
-
 class Mob(object):
-    Sprite = MobSprite
+    Sprite = sprites.MobSprite
 
     def __init__(self, (x, y)):
         self.pos = x, y
 
     def make_sprite(self, tiles):
         self.sprite = self.Sprite(tiles, self.pos, self.animation)
-        shadow = Shadow(tiles, self.sprite)
+        shadow = sprites.Shadow(tiles, self.sprite)
         return self.sprite, shadow, None
 
     def control(self, level):
@@ -127,49 +58,6 @@
     animation = [30, 30, 30, 31, 31, 31, 30, 30, 30, 32, 32, 32]
 
 
-class PlayerSprite(pygame.sprite.Sprite):
-    def __init__(self, frames, (x, y)):
-        super(PlayerSprite, self).__init__()
-        self.direction = 0
-        self.frames = frames
-        self.image = self.frames[0]
-        self.animation = self.stand_animation(self.direction)
-        self.rect = self.image.get_rect()
-        self.rect.move_ip(4 + 20 * x, 12 * y)
-        self.depth = self.rect.bottom
-
-    def update(self):
-        if self.animation:
-            try:
-                self.animation.next()
-            except StopIteration:
-                self.animation = None
-
-    def walk_animation(self, direction):
-        frames = [
-            [5, 6, 5, 7],
-            [10, 11, 10, 12],
-            [15, 16, 15, 16],
-            [0, 1, 0, 2],
-        ][direction]
-        for frame in frames:
-            yield None
-            self.rect.move_ip((DX[direction]*5, DY[direction]*3))
-            self.image = self.frames[frame]
-            self.depth = self.rect.bottom
-
-    def stand_animation(self, direction):
-        yield None
-        frame = [5, 10, 15, 0][direction]
-        self.image = self.frames[frame]
-
-    def set_animation(self, animation):
-        self.animation = animation
-        try:
-            self.animation.next()
-        except StopIteration:
-            self.animation = None
-
 class Player(object):
     def place(self, (x, y)):
         self.pos = x, y
@@ -177,9 +65,9 @@
         self.sprite = None
 
     def make_sprite(self, tiles):
-        self.sprite = PlayerSprite(tiles, self.pos)
-        shadow = Shadow(tiles, self.sprite)
-        self.vision = Vision(self.sprite, 64)
+        self.sprite = sprites.PlayerSprite(tiles, self.pos)
+        shadow = sprites.Shadow(tiles, self.sprite)
+        self.vision = sprites.Vision(self.sprite, 64)
         return self.sprite, shadow, self.vision
 
     def walk(self, level, direction):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/sprites.py	Sat Mar 10 11:53:48 2012 +0100
@@ -0,0 +1,119 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+import pygame
+from pygame import locals as pg
+
+from mhrok.util import DX, DY
+
+
+class PlayerSprite(pygame.sprite.Sprite):
+    def __init__(self, frames, (x, y)):
+        super(PlayerSprite, self).__init__()
+        self.direction = 0
+        self.frames = frames
+        self.image = self.frames[0]
+        self.animation = self.stand_animation(self.direction)
+        self.rect = self.image.get_rect()
+        self.rect.move_ip(4 + 20 * x, 12 * y)
+        self.depth = self.rect.bottom
+
+    def update(self):
+        if self.animation:
+            try:
+                self.animation.next()
+            except StopIteration:
+                self.animation = None
+
+    def walk_animation(self, direction):
+        frames = [
+            [5, 6, 5, 7],
+            [10, 11, 10, 12],
+            [15, 16, 15, 16],
+            [0, 1, 0, 2],
+        ][direction]
+        for frame in frames:
+            yield None
+            self.rect.move_ip((DX[direction]*5, DY[direction]*3))
+            self.image = self.frames[frame]
+            self.depth = self.rect.bottom
+
+    def stand_animation(self, direction):
+        yield None
+        frame = [5, 10, 15, 0][direction]
+        self.image = self.frames[frame]
+
+    def set_animation(self, animation):
+        self.animation = animation
+        try:
+            self.animation.next()
+        except StopIteration:
+            self.animation = None
+
+class Shadow(pygame.sprite.Sprite):
+    def __init__(self, frames, owner):
+        super(Shadow, self).__init__()
+        self.image = frames[20].copy()
+        self.image.set_alpha(128)
+        self.rect = self.image.get_rect()
+        self.owner = owner
+
+    def update(self):
+        self.rect.center = self.owner.rect.center
+
+class Vision(pygame.sprite.DirtySprite):
+    def __init__(self, owner, radius):
+        super(Vision, self).__init__()
+        self.owner = owner
+        self.blendmode = pg.BLEND_RGBA_MIN
+        self.dirty = 2
+        self.images = {}
+        for r in xrange(40, 65):
+            self.images[r] = self.get_image(r)
+        self.radius = radius
+        self.set_radius(radius)
+        self.rooms = []
+
+    def set_radius(self, radius):
+        self.current_radius = radius
+        self.image = self.images[radius]
+        self.rect = self.image.get_rect()
+        self.rect.center = self.owner.rect.center
+
+    def get_image(self, radius):
+        image = pygame.Surface((radius*5/2, radius*2), pg.SRCALPHA)
+        image.fill((255, 255, 255, 255))
+        for r in xrange(radius, radius-25, -1):
+            pygame.draw.ellipse(
+                    image,
+                    (255, 255, 255, max(0, min(255, (r-radius+25) * 10))),
+                    (radius*5/4-r*5/4, radius-r, 2*r*5/4, 2*r), 0)
+        return image
+
+    def update(self):
+        if self.current_radius > self.radius:
+            self.set_radius(max(self.current_radius - 4, self.radius))
+        elif self.current_radius < self.radius:
+            self.set_radius(min(self.current_radius + 4, self.radius))
+        self.rect.center = self.owner.rect.center
+
+
+class MobSprite(pygame.sprite.Sprite):
+    def __init__(self, frames, (x, y), animation):
+        super(MobSprite, self).__init__()
+        self.animation = animation
+        self.frames = frames
+        self.image = self.frames[0]
+        self.rect = self.image.get_rect()
+        self.frame = 0
+        self.rect.move_ip(4 + 20 * x, 12 * y)
+        self.depth = self.rect.bottom
+        self.direction = None
+
+    def update(self):
+        self.frame = (self.frame + 1) % len(self.animation)
+        self.image = self.frames[self.animation[self.frame]]
+        if self.direction is not None:
+            self.rect.move_ip((DX[self.direction]*5, DY[self.direction]*3))
+            self.depth = self.rect.bottom
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/util.py	Sat Mar 10 11:53:48 2012 +0100
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+DX = [-1,  0,  1,  0]
+DY = [ 0, -1,  0,  1]