Commits

Radomir Dopieralski  committed 17b5a0c

Started separating the player object into sprite and actor

  • Participants
  • Parent commits e373300

Comments (0)

Files changed (5)

File lib/display.py

         self.sprites.add(sprite)
         sprite.rect.move_ip(self.scrollpos[0], self.scrollpos[1])
 
-    def place_sprites(self, sprites):
-        for sprite in sprites:
+    def place_monsters(self, monsters):
+        for monster in monsters.values():
+            sprite = monster.sprite
             self.add_sprite(sprite)
             self.shadows.add(Shadow(sprite))
 
         # Prapare the map 
         self.level = Level('data/map.txt')
         self.display.draw_map(self.level.map)
-        self.display.place_sprites(self.level.monsters.values())
+        self.display.place_monsters(self.level.monsters)
         self.level.player.display = self.display
     
     def main_loop(self):
                 if monster!=self.level.player:
                     monster.act()
             for step in range(8):
-                self.display.scroll_after(self.level.player.rect)
+                self.display.scroll_after(self.level.player.sprite.rect)
                 self.display.show_frame()
 

File lib/level.py

     def create_monster(self, x, y, kind):
         module = __import__("monster.%s" % kind, globals(), {}, ["Mob"])
         monster = module.Mob((x, y), self)
+        self.monsters[(x,y)] = monster
         return monster
 
 class Map:

File lib/monster/__init__.py

     def __repr__(self):
         return "block (%d, %d)" % (self.pos[0], self.pos[1])
 
+class Actor():
+    def __init__(self, pos, level):
+        self.x, self.y = pos
+        self.level = level
+#        self.sprite = Monster(pos, level)
+        self.place(pos)
+
+    def place(self, pos):
+        self.move(pos)
+        self.sprite.place(pos)
+                
+    def move(self, pos):
+        x, y = pos
+        if self.level.map.is_blocked(x,y):
+            raise MoveBlocked(pos)
+        if self.level.monsters.get(pos, None):
+            raise MoveBump(pos, self.level.monsters[pos])
+        try:
+            del self.level.monsters[(self.x, self.y)]
+        except KeyError:
+            pass
+        self.x, self.y = pos
+        self.level.monsters[pos] = self
+        self.sprite.move(pos)
+
+    def die(self):
+        self.sprite.die()
+        try:
+            del self.level.monsters[(self.x, self.y)]
+        except KeyError:
+            pass
+    
+    
+    def walk(self, direction):
+        self.sprite.rotate(direction)
+        self.move((self.x+delta_x[direction],self.y+delta_y[direction]))
+        self.sprite.step = 8
+        self.sprite.anim = self.sprite.anim_walk
+
 
 class Monster(pygame.sprite.Sprite):
     '''Monsters only have two frames of animation.'''
         self.x, self.y = pos
         self.h = 0
         self.dead = False
+        self.anim = self.anim_stand()
+        self.sprite = self
         self.place(pos)
-        self.anim = self.anim_stand()
         self.update()
 
     def place(self, pos):
         
     def move(self, pos):
         x, y = pos
-        if self.level.map.is_blocked(x,y):
-            raise MoveBlocked(pos)
-        if self.level.monsters.get(pos, None):
-            raise MoveBump(pos, self.level.monsters[pos])
-        try:
-            del self.level.monsters[(self.x, self.y)]
-        except KeyError:
-            pass
         self.x, self.y = pos
-        self.level.monsters[pos] = self
         
     def animate(self, frames, delay=0):
         if self.delay>=delay:

File lib/monster/player.py

 import sys
         
 from monster import MoveError, MoveBump, MoveBlocked
-from monster import Monster
+from monster import Monster, Actor
 import mesg
 from util import *
 
-class Mob(Monster):
-    name = "player"
+class Sprite(Monster):
     frames = read_frames(os.path.join("data", "image", "abi2.png"))
-
     def __init__(self, pos, level):
-        self.facing = 0
+        self.facing = 3
         Monster.__init__(self, pos, level)
         self.rotate(3)
         self.z += 1
-        self.level.player = self
-        self.display = None
-    
+
     def anim_stand(self):
         self.image = self.frames[self.facing-1][0]
         
         self.facing = facing
         self.image = self.frames[self.facing-1][0]
 
+class Mob(Actor):
+    name = "player"
+
+    def __init__(self, pos, level):
+        self.sprite = Sprite(pos, level)
+        Actor.__init__(self, pos, level)
+        self.level.player = self
+        self.display = None
+            
     def wait(self):
-        self.step = 8
-        self.anim = self.anim_stand
+        self.sprite.step = 8
+        self.sprite.anim = self.anim_stand
    
     def attack(self, direction, monster):
-        self.rotate(direction)
-        self.step = 4
-        self.anim = self.anim_attack
-        mesg.Mesg(self.display, monster, "-1", color=(255,0,0))
+        self.sprite.rotate(direction)
+        self.sprite.step = 4
+        self.sprite.anim = self.anim_attack
+        mesg.Mesg(self.display, monster.sprite, "-1", color=(255,0,0))
         monster.hit()
         
     def hit(self, damage=5):
-        mesg.Mesg(self.display, self, "-%d" % damage, color=(255,0,0), delay=4)
+        mesg.Mesg(self.display, self.sprite, "-%d" % damage, color=(255,0,0), delay=4)
         pass
         
     def act(self):
                 self.attack(d, bump.monster)
                 return
             except MoveError:
-                mesg.Mesg(self.display, self, "blocked")                
+                mesg.Mesg(self.display, self.sprite, "blocked")                
             if key(K_SPACE):
                 self.wait()
                 return