b4992a352eb38238c1028a9e23cad98ad87f2806
[butaba-adventures.git] / gameobjects.py
1 # object classes - classes for game interactive objects
2 import pygame
3 import os.path
4
5 import utility
6
7 # base class for all objects
8 class GameObject:
9 # initialization routine
10 def __init__ (self, row, col, text, image = None, can_pickup = True):
11 self.row = row
12 self.col = col
13 self.image = image
14 self.text = text
15 self.can_pickup = can_pickup
16
17 # override this for interaction, i.e. when character walks into the item
18 def interact (self):
19 # always return True if the object is interacted with
20 # return False to make the object block the player from
21 # moving over it
22 return True
23
24 # use the object on another object
25 def use (self, otherobject):
26 pass
27
28 class GoldCoins (GameObject):
29 # initialize
30 def __init__ (self, row, col, image, value):
31 text = "gold coins"
32 self.value = value
33 GameObject.__init__ (self, row, col, text, image, False)
34
35 # no interaction with this object
36 def interact (self):
37 return True
38
39 # use the object on Butaba - add to his gold
40 def use (self, butaba):
41 butaba.gold += self.value
42
43
44 class HealthPotion (GameObject):
45 # initialize
46 def __init__ (self, row, col, image):
47 text = "health potion"
48 GameObject.__init__ (self, row, col, text, image, True)
49
50 # no interaction with this object
51 def interact (self):
52 return True
53
54 # using the potion
55 def use (self, butaba):
56 butaba.health += 25
57 if butaba.health > butaba.MAXHEALTH:
58 butaba.health = butaba.MAXHEALTH
59
60 class Chest (GameObject):
61 def __init__ (self, row, col, text, image, key_id, locked = False, items = []):
62 self.key_id = key_id
63 self.locked = locked
64 self.items = items
65 GameObject.__init__ (self, row, col, text, image, False)
66
67 # no interaction with this object. Also solid so return False
68 def interact (self):
69 # object is solid
70 return False
71
72 # try to use the key passed to it
73 def use (self, key):
74 # if chest is locked try to unlock it
75 if self.locked is True:
76 # if the item is a key
77 if isinstance (key, Key):
78 # if the key fits the lock
79 # unlock the chest
80 if key.key_id == self.key_id:
81 self.locked = False
82 # return the key
83 return key
84 # return None if not a key or key did not fit
85 return None
86 else:
87 return self.locked
88
89 class Key (GameObject):
90 def __init__ (self, row, col, text, image, key_id):
91 self.key_id = key_id
92 GameObject.__init__ (self, row, col, text, image, True)
93
94 # no interaction with this object
95 def interact (self):
96 # key is not a solid object so return True
97 return True
98
99 # using the key - this is relegated to the locked item for
100 # convenience, so key does nothing by defaults
101 def use (self, lockitem):
102 pass