Commits

Radomir Dopieralski  committed 52aed14

Add ladder and mob creation on the level

  • Participants
  • Parent commits 2579c7e

Comments (0)

Files changed (7)

File jelly/__init__.py

                         event.type == pg.KEYDOWN and
                         event.key == pg.K_ESCAPE):
                     self.game_over = True
-            if self.display.mob_sprites[id(self.pc)].animation is None:
+            try:
+                pc_animation = self.display.mob_sprites[id(self.pc)].animation
+            except KeyError:
+                pc_animation = None
+                self.display.events(self.level.events)
+                self.level.events = []
+            if pc_animation is None:
                 if self.pc.control(self.level):
                     for mob in self.level.mobs:
                         if mob == self.pc:

File jelly/display.py

         self.mob_sprites = weakref.WeakValueDictionary()
         self.mob_lights = weakref.WeakValueDictionary()
 
-    def render_level(self, level):
-        self.render_sprites(level)
-        self.background = render_background(level, self.tiles)
-
-    def render_sprites(self, level):
         self.sprites = SortedUpdates()
         self.shadows = pygame.sprite.RenderUpdates()
         self.lights = pygame.sprite.LayeredDirty()
-        for mob in level.mobs:
-            sprite = mob.Sprite(self.tiles, mob.pos, mob.animation)
-            self.sprites.add(sprite)
-            self.mob_sprites[id(mob)] = sprite
-            shadow = sprites.Shadow(self.tiles, sprite)
-            self.shadows.add(shadow)
-            if mob.has_light:
-                light = sprites.Vision(sprite, 64)
-                self.lights.add(light)
-                self.mob_lights[id(mob)] = light
+
+    def render_level(self, level):
+        self.background = render_background(level, self.tiles)
 
     def light_room(self, mob, room):
         (rx, ry), (rw, rh) = room
         sprite = self.mob_sprites[id(mob)]
         sprite.set_animation(sprite.open_animation())
 
+    def on_mob_create(self, mob):
+        sprite = mob.Sprite(self.tiles, mob.pos, mob.animation)
+        self.sprites.add(sprite)
+        self.mob_sprites[id(mob)] = sprite
+        shadow = sprites.Shadow(self.tiles, sprite)
+        self.shadows.add(shadow)
+        if mob.has_light:
+            light = sprites.Vision(sprite, 64)
+            self.lights.add(light)
+            self.mob_lights[id(mob)] = light
 
 
 def load_tiles(filename, width, height):

File jelly/jelly.png

Old
Old image
New
New image

File jelly/level.py

 class BaseLevel(object):
     def __init__(self, pc):
         self.pc = pc
-        self.mobs = [pc]
+        self.mobs = []
         self.width = 31
         self.height = 38
 
         self.events.append(('mob_walk', mob, direction))
         self.events.append(('mob_dust', mob, 1, -4))
 
+    def create(self, mob):
+        self.mobs.append(mob)
+        self.events.append(('mob_create', mob))
+
     def kill(self, mob):
         self.mobs.remove(mob)
         self.events.append(('mob_death', mob))

File jelly/mobs.py

     name = 'purple jelly'
     color = 1
     animation = [46, 46, 47, 47, 47, 47]
+    behaviors = [attack_behavior, wander_behavior]
 
 
 class RedJelly(Mob):
     name = 'black jelly'
     color = 4
     animation = [76, 76, 77, 77, 77, 77]
+    behaviors = [wander_behavior]
 
 
 class Crate(Mob):
         level.open(self)
 
 
+class Ladder(Mob):
+    name = 'ladder'
+    animation = [95, 96] * 7
+    Sprite = sprites.LadderSprite
+
+    def control(self, level):
+        level.kill(self)
+
+    def hit(self, level, attacker):
+        pass
+
+
 class Player(object):
     name = 'player'
     has_light = True

File jelly/rooms_level.py

                     mobs.BlackJelly,
                     mobs.Crate,
                 ])
-                self.mobs.append(MobClass((r(x, x + w), r(y, y + h))))
+                self.create(MobClass((r(x, x + w), r(y, y + h))))
         self[r(x, x+w), r(y, y+h)] = 5
 
         (x, y), (w, h) = random.choice(rooms)
-        self.pc.place((r(x, x+w), r(y, y+h)))
+        pc_pos = (r(x, x+w), r(y, y+h))
+
+        self.create(mobs.Ladder(pc_pos))
+        self.pc.place(pc_pos)
+        self.create(self.pc)
+
         self.rooms = rooms
 
 

File jelly/sprites.py

 from jelly.util import DX, DY
 
 
+class LadderSprite(pygame.sprite.Sprite):
+    def __init__(self, frames, (x, y), steps):
+        super(LadderSprite, self).__init__()
+        self.image = pygame.Surface((32, 480))
+        self.image.set_colorkey((0, 0, 0))
+        for step, frame in enumerate(steps):
+            self.image.blit(frames[frame], (0, 32 * step))
+        self.rect = self.image.get_rect()
+        self.rect.midbottom = (20 + 20 * x, 32 + 12 * y)
+        self.depth = self.rect.bottom
+        self.step = 0
+
+
+    def update(self):
+        self.step += 1
+        if self.step % 4 == 0:
+            self.image = pygame.transform.flip(self.image, True, False)
+        self.rect.move_ip(0, -3)
+        if self.step > 24:
+            self.kill()
+
+    def death_animation(self):
+        pass
+
+    def set_animation(self, animation):
+        pass
+
+
 class MobSprite(pygame.sprite.Sprite):
     def __init__(self, frames, (x, y), steps):
         super(MobSprite, self).__init__()