Mercurial > jelly
changeset 4:b35388027227
Let monsters attack
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sun, 11 Mar 2012 19:25:57 +0100 |
| parents | 6b79835c5b2f |
| children | 69ce5e46b825 |
| files | jelly/jelly.png jelly/mobs.py jelly/sprites.py jelly/util.py |
| diffstat | 4 files changed, 26 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/jelly/mobs.py Sun Mar 11 18:55:42 2012 +0100 +++ b/jelly/mobs.py Sun Mar 11 19:25:57 2012 +0100 @@ -7,7 +7,7 @@ from pygame import locals as pg from jelly import sprites -from jelly.util import DX, DY, dist +from jelly.util import DX, DY, dist, pos2dir KEY_DIRS = { @@ -34,6 +34,9 @@ return sprite, shadow, None def control(self, level): + if dist(level.pc.pos, self.pos) <= 1: + level.attack(self, pos2dir(self.pos, level.pc.pos), level.pc) + return x, y = self.pos possible = [d for (d, (dx, dy)) in enumerate(zip(DX, DY)) if level[x + dx, y + dy]] @@ -97,11 +100,14 @@ has_light = True Sprite = sprites.PlayerSprite animation = None + color = 5 def place(self, (x, y)): self.pos = x, y - self.direction = 3 + self.direction = 0 + def hit(self, level, attacker): + return True def control(self, level):
--- a/jelly/sprites.py Sun Mar 11 18:55:42 2012 +0100 +++ b/jelly/sprites.py Sun Mar 11 19:25:57 2012 +0100 @@ -73,6 +73,17 @@ def stand_animation(self, direction): yield None + def attack_animation(self, direction): + yield None + self.rect.move_ip((DX[direction]*4, DY[direction]*2-2)) + yield None + self.rect.move_ip((DX[direction]*6, DY[direction]*4)) + yield None + self.rect.move_ip((-DX[direction]*3, -DY[direction]*1+2)) + yield None + self.rect.move_ip((-DX[direction]*7, -DY[direction]*5)) + self.depth = self.rect.bottom + def set_animation(self, animation): self.animation = animation try:
--- a/jelly/util.py Sun Mar 11 18:55:42 2012 +0100 +++ b/jelly/util.py Sun Mar 11 19:25:57 2012 +0100 @@ -6,4 +6,10 @@ def dist((x1, y1), (x2, y2)): - return min(abs(x2 - x1), abs(y2 - y1)) + return max(abs(x2 - x1), abs(y2 - y1)) + +def pos2dir(pos1, pos2): + x, y = pos1 + return min( + xrange(len(DX)), + key=lambda d: dist(pos2, (x + DX[d], y + DY[d])))
