1. Radomir Dopieralski
  2. jelly

Commits

Radomir Dopieralski  committed a405be7

Make items usable

  • Participants
  • Parent commits 7a57de0
  • Branches default

Comments (0)

Files changed (4)

File jelly/items.py

View file
     'Wooden mallet',
     'First aid kit',
     'Bandaid',
-    'Bomb',
     'Fire axe',
 ]
 
+def use_item(item, mob, level):
+    if item == 'Bandaid':
+        mob.hp = min(mob.max_hp, mob.hp + 10)
+    elif item == 'First aid kit':
+        mob.hp = mob.max_hp
+    elif item == 'Heavy wrench':
+        mob.damage = 2
+    elif item == 'Wooden mallet':
+        mob.damage = 3
+    elif item == 'Fire axe':
+        mob.damage = 8
+

File jelly/level.py

View file
         self.events.append(('mob_open', mob))
         self.events.append(('mob_dust', mob, 10, 4))
         self.message(mob, item)
-        self.pc.items.append(item)
+        if item != 'Empty':
+            self.pc.pick(item)
 
 
     def stand(self, mob, direction, dust=False):

File jelly/message.py

View file
         if self.tiles is None:
             self.tiles = resource.load_tiles('jelly.png', 32, 32)
         self.owner = owner
-        self.image = self.tiles[92].copy()
+        self.image = self.tiles[90].copy()
         self.rect = self.image.get_rect()
         self.rect.center = 32, 32
 
-
     def update(self):
         gauge = int(max(0, min(26, 26 * self.owner.hp / self.owner.max_hp)))
-        self.image = self.tiles[92].copy()
-        self.image.blit(self.tiles[93], (3, 0), (3, 0, gauge, 32))
+        self.image = self.tiles[90].copy()
+        self.image.blit(self.tiles[91], (3, 0), (3, 0, gauge, 32))

File jelly/mobs.py

View file
 
 
 import random
+import collections
 import pygame
 from pygame import locals as pg
 
 }
 
 
-
-
 class Mob(object):
     Sprite = sprites.MobSprite
     has_light = False
                 return
 
     def hit(self, level, attacker):
-        self.hp -= 1
+        self.hp -= attacker.damage
         self.status['asleep'] = False
         if self.hp <= 0:
             level.kill(self)
         self.level = 0
         self.max_hp = 100
         self.hp = self.max_hp
-        self.items = []
+        self.items = collections.Counter()
+        self.damage = 1
 
 
     def place(self, (x, y)):
             level.game_over()
         return True
 
+    def pick(self, item):
+        self.items[item] += 1
+
     def inventory_menu(self, display):
-        menu = message.Menu((480, 20), self.items or ['Empty'])
+        item_list = self.items.keys()
+        labels = ['%s x%d' % i for i in self.items.iteritems()]
+        menu = message.Menu((480, 20), labels or ['Empty'])
         display.render_menu(menu)
         choice = None
         clock = pygame.time.Clock()
             display.update()
             display.draw()
             choice = menu.update()
+        if choice is False or not item_list:
+            return None
+        else:
+            return item_list[choice]
 
     def control(self, level, display):
         direction = None
         keys = pygame.key.get_pressed()
         if keys[pg.K_x]:
-            self.inventory_menu(display)
+            item = self.inventory_menu(display)
+            if item:
+                items.use_item(item, self, level)
+                self.items[item] -= 1
+                return True
             return False
         elif keys[pg.K_DOWN] and keys[pg.K_LEFT]:
             direction = 1
             direction = 5
         elif keys[pg.K_DOWN] and keys[pg.K_RIGHT]:
             direction = 7
-        elif keys[pg.K_z]:
+        if keys[pg.K_z]:
             x, y = self.pos
             for other in level.mobs:
                 if other.pos == (x + DX[self.direction],
                     break
             else:
                 other = None
-            if other is None:
+            if other is None and not keys[pg.K_LCTRL]:
                 for m in level.mobs:
                     if m == self:
                         continue
                         other = m
                         self.direction = pos2dir(self.pos, other.pos)
                         break
-            if not keys[pg.K_LCTRL]:
-                level.attack(self, self.direction, other)
-                return True
-        elif not keys[pg.K_LSHIFT]:
+            level.attack(self, self.direction, other)
+            return True
+        if not keys[pg.K_LSHIFT] and not direction:
             for key, direction in KEY_DIRS.iteritems():
                 if keys[key]:
                     break
             else:
                 direction = None
-        else:
-            direction = None
         if direction == 8:
             level.wait(self, self.direction)
         elif direction is not None: