source: gamelib/equipment.py @ 146:423050ec188b

Last change on this file since 146:423050ec188b was 146:423050ec188b, checked in by Jeremy Thurgood <firxen@…>, 11 years ago

Equipment images.

File size: 1.9 KB
Line 
1"""Stuff for animals to use."""
2
3import random
4import sound
5
6class Equipment(object):
7    IS_EQUIPMENT = True
8
9    def __init__(self):
10        self._buy_price = self.BUY_PRICE
11        self._sell_price = self.SELL_PRICE
12        self._name = self.NAME
13
14    def buy_price(self):
15        return self._buy_price
16
17    def sell_price(self):
18        return self._sell_price
19
20    def name(self):
21        return self._name
22
23class Weapon(Equipment):
24    IS_WEAPON = True
25
26    def in_range(self, gameboard, wielder, target):
27        """Can the lucky wielder hit the potentially unlucky target with this?"""
28        return wielder.pos.dist(target.pos) <= self.RANGE
29
30    def hit(self, gameboard, wielder, target):
31        """Is the potentially unlucky target actually unlucky?"""
32        if hasattr(self, 'HIT_SOUND'):
33            sound.play_sound(self.HIT_SOUND)
34        roll = random.randint(1, 100)
35        return roll > (100-self.BASE_HIT) + self.RANGE_MODIFIER*wielder.pos.dist(target.pos)
36
37    def place(self, animal):
38        for eq in animal.equipment:
39            if is_weapon(eq):
40                return False
41        return True
42
43class Rifle(Weapon):
44    NAME = "rifle"
45    BUY_PRICE = 100
46    SELL_PRICE = 75
47
48    RANGE = 3
49    BASE_HIT = 55
50    RANGE_MODIFIER = 15
51    HIT_SOUND = "fire-rifle.ogg"
52
53    CHICKEN_IMAGE_FILE = 'sprites/equip_rifle.png'
54
55class Knife(Weapon):
56    NAME = "knife"
57    BUY_PRICE = 20
58    SELL_PRICE = 15
59
60    RANGE = 1
61    BASE_HIT = 70
62    RANGE_MODIFIER = 0
63
64    CHICKEN_IMAGE_FILE = 'sprites/equip_knife.png'
65
66def is_equipment(obj):
67    """Return true if obj is a build class."""
68    return getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME")
69
70def is_weapon(obj):
71    return is_equipment(obj) and getattr(obj, 'IS_WEAPON', False)
72
73EQUIPMENT = []
74for name in dir():
75    obj = eval(name)
76    try:
77        if is_equipment(obj):
78            EQUIPMENT.append(obj)
79    except TypeError:
80        pass
Note: See TracBrowser for help on using the repository browser.