Mercurial > jelly
changeset 3:6b79835c5b2f
Add monster hitpoints
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sun, 11 Mar 2012 18:55:42 +0100 |
| parents | 8530c7fe1aeb |
| children | b35388027227 |
| files | jelly/display.py jelly/jelly.png jelly/level.py jelly/mobs.py jelly/sprites.py |
| diffstat | 5 files changed, 91 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/jelly/display.py Sun Mar 11 14:12:43 2012 +0100 +++ b/jelly/display.py Sun Mar 11 18:55:42 2012 +0100 @@ -30,35 +30,37 @@ pygame.display.set_caption('Jelly') self.first_draw = True self.tiles = load_tiles('jelly.png', 32, 32) - for tile in self.tiles[35:40]: + for tile in self.tiles[36:40]: tile.set_alpha(192) - for tile in self.tiles[45:50]: + for tile in self.tiles[46:50]: tile.set_alpha(192) - for tile in self.tiles[55:60]: + for tile in self.tiles[56:60]: tile.set_alpha(192) - for tile in self.tiles[65:70]: + for tile in self.tiles[66:70]: tile.set_alpha(192) - for tile in self.tiles[75:80]: + for tile in self.tiles[76:80]: tile.set_alpha(128) - + for step, tile in enumerate(self.tiles[16:20]): + tile.set_alpha(255-step*32) self.background = pygame.Surface(self.screen.get_size()) self.fog = pygame.Surface(self.screen.get_size(), pg.SRCALPHA) self.fog.fill((64, 128, 255, 255)) - self.memory = pygame.Surface(self.screen.get_size(), pg.SRCALPHA) - self.memory.fill((64, 128, 255, 255)) + self._grad(self.fog, self.fog.get_rect(), + (64, 128, 255), (16, 64, 224)) + self.memory = self.fog.copy() self.dirty = [] self.mob_sprites = weakref.WeakValueDictionary() self.mob_lights = weakref.WeakValueDictionary() def render_level(self, level): self.render_sprites(level) - self.background.blit(render_background(level, self.tiles), (0, 0)) + self.background = render_background(level, self.tiles) def render_sprites(self, level): self.sprites = SortedUpdates() self.shadows = pygame.sprite.RenderUpdates() - self.visions = pygame.sprite.LayeredDirty() + self.lights = pygame.sprite.LayeredDirty() for mob in level.mobs: sprite = mob.Sprite(self.tiles, mob.pos, mob.animation) self.sprites.add(sprite) @@ -67,7 +69,7 @@ self.shadows.add(shadow) if mob.has_light: light = sprites.Vision(sprite, 64) - self.visions.add(light) + self.lights.add(light) self.mob_lights[id(mob)] = light def light_room(self, mob, room): @@ -82,22 +84,34 @@ self.fog.blit(self.memory, (mx, my), rect) self.dirty.append(rect) + def _grad(self, surface, rect, startcolor, endcolor): + (sx, sy, w, h) = rect + steps = h / 8 + sr, sg, sb = startcolor + er, eg, eb = endcolor + dr, dg, db = (er-sr)/steps, (eg-sg)/steps, (eb-sb)/steps + for step, y in enumerate(xrange(sy, sy+h, 8)): + surface.fill((max(0, sr), max(0, sg), max(0, sb)), (sx, y, w, 8)) + sr += dr + sg += dg + sb += db + def draw(self): + self.screen.blit(self.background, (0, 0)) if self.first_draw: - self.screen.blit(self.background, (0, 0)) + self.lights.draw(self.fog) self.shadows.draw(self.screen) self.sprites.draw(self.screen) - self.visions.draw(self.fog) self.screen.blit(self.fog, (0, 0)) self.dirty = [self.background.get_rect()] self.first_draw = False else: - self.screen.blit(self.background, (0, 0)) - self.visions.clear(self.fog, self.memory) + self.lights.clear(self.fog, self.memory) + visible = self.lights.draw(self.fog) + self.dirty.extend(visible) self.sprites.clear(self.screen, self.background) self.shadows.draw(self.screen) self.sprites.draw(self.screen) - self.dirty.extend(self.visions.draw(self.fog)) self.screen.blit(self.fog, (0, 0)) pygame.display.update(self.dirty) self.dirty = [] @@ -105,24 +119,31 @@ def update(self): self.sprites.update() self.shadows.update() - self.visions.update() + self.lights.update() def events(self, events): for event in events: getattr(self, 'on_%s' % event[0])(*event[1:]) def on_mob_dust(self, mob, amount=4, delay=0): + sprite = self.mob_sprites[id(mob)] for i in xrange(amount): - sprite = self.mob_sprites[id(mob)] x = sprite.rect.centerx + random.randint(-8, 8) y = sprite.rect.centery + random.randint(-3, 3) self.sprites.add(sprites.Dust( self.tiles, (x, y), min(0, delay + random.randint(0, 4)))) - def on_mob_blood(self, mob, delay=0): + def on_mob_gibs(self, mob, color=0, amount=8, delay=0): sprite = self.mob_sprites[id(mob)] - self.sprites.add(sprites.Blood( - self.tiles, sprite.rect.center, delay)) + for i in xrange(amount): + x = sprite.rect.centerx + random.randint(-10, 10) + y = sprite.rect.centery + random.randint(-3, 5) + self.sprites.add(sprites.Gib( + self.tiles, + (x, y), + min(0, delay + random.randint(0, 6)), + color=color, + )) def on_room_light(self, mob, room): self.light_room(mob, room) @@ -170,7 +191,7 @@ def render_background(level, tiles): background = pygame.Surface((level.width * 20 + 20, level.height * 12 + 20)) - background.fill((16, 64, 224)) + background.fill((8, 32, 112)) for y, row in enumerate(level.map): for x, square in enumerate(row): pos = x * 20 + 4, y * 12
--- a/jelly/level.py Sun Mar 11 14:12:43 2012 +0100 +++ b/jelly/level.py Sun Mar 11 18:55:42 2012 +0100 @@ -61,6 +61,10 @@ self.mobs.remove(mob) self.events.append(('mob_death', mob)) + def hit(self, mob, attacker): + if mob.hit(self, mob): + self.events.append(('mob_gibs', mob, mob.color, 8, 2)) + def open(self, mob): self.mobs.remove(mob) self.events.append(('mob_open', mob)) @@ -77,7 +81,7 @@ self.events.append(('mob_wait', mob, direction)) def attack(self, mob, direction, other): - other.hit(self, mob) + self.hit(other, mob) self.events.append(('mob_attack', mob, direction, other)) self.events.append(('mob_dust', mob, 4, -2))
--- a/jelly/mobs.py Sun Mar 11 14:12:43 2012 +0100 +++ b/jelly/mobs.py Sun Mar 11 18:55:42 2012 +0100 @@ -22,9 +22,11 @@ class Mob(object): Sprite = sprites.MobSprite has_light = False + max_hp = 2 def __init__(self, (x, y)): self.pos = x, y + self.hp = self.max_hp def make_sprite(self, tiles): sprite = self.Sprite(tiles, self.pos, self.animation) @@ -42,31 +44,40 @@ pass def hit(self, level, attacker): - level.kill(self) + self.hp -= 1 + if self.hp <= 0: + level.kill(self) + return True + class PinkJelly(Mob): name = 'pink jelly' + color = 2 animation = [56, 56, 57, 57, 57, 57] class PurpleJelly(Mob): name = 'purple jelly' + color = 1 animation = [46, 46, 47, 47, 47, 47] class RedJelly(Mob): name = 'red jelly' + color = 3 animation = [66, 66, 67, 67, 67, 67] class BlueJelly(Mob): name = 'blue jelly' + color = 0 animation = [36, 36, 37, 37, 37, 37] class BlackJelly(Mob): name = 'black jelly' + color = 4 animation = [76, 76, 77, 77, 77, 77]
--- a/jelly/sprites.py Sun Mar 11 14:12:43 2012 +0100 +++ b/jelly/sprites.py Sun Mar 11 18:55:42 2012 +0100 @@ -65,6 +65,9 @@ yield None self.steps = [19] yield None + yield None + yield None + yield None self.kill() def stand_animation(self, direction): @@ -225,3 +228,31 @@ except StopIteration: self.kill() +class Gib(pygame.sprite.Sprite): + def __init__(self, frames, (x, y), delay=0, color=0): + super(Gib, self).__init__() + self.image = frames[38].copy() + self.image.set_alpha(0) + self.rect = self.image.get_rect() + self.rect.center = (x, y) + self.depth = self.rect.bottom + 2 + self.animation = self.gib_animation(frames, delay, color) + + def gib_animation(self, frames, delay, color): + dx = random.randint(-3, 3) + dy = random.randint(-5, 1) + steps = random.randint(4, 8) + for step in xrange(delay): + yield None + self.image = frames[38+color*10 + random.randint(0, 1)].copy() + for step in xrange(steps): + self.image.set_alpha(192*(steps-step)/steps) + self.rect.move_ip(dx, dy) + dy += 1 + yield None + + def update(self): + try: + self.animation.next() + except StopIteration: + self.kill()
