Added object interaction - healthpotion, lock and key
[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
29 class HealthPotion (GameObject):
30 # initialize
31 def __init__ (self, row, col, image):
32 text = "health potion"
33 GameObject.__init__ (self, row, col, text, image, True)
34
35 # no interaction with this object
36 def interact (self):
37 return True
38
39 # using the potion
40 def use (self, butaba):
41 butaba.health += 25
42 if butaba.health > butaba.MAXHEALTH:
43 butaba.health = butaba.MAXHEALTH
44
45 class Chest (GameObject):
46 def __init__ (self, row, col, text, image, key_id, locked = False, items = []):
47 self.key_id = key_id
48 self.locked = locked
49 self.items = items
50 GameObject.__init__ (self, row, col, text, image, False)
51
52 # no interaction with this object. Also solid so return False
53 def interact (self):
54 # object is solid
55 return False
56
57 # try to use the key passed to it
58 def use (self, key):
59 # if chest is locked try to unlock it
60 if self.locked is True:
61 # if the item is a key
62 if isinstance (key, Key):
63 # if the key fits the lock
64 # unlock the chest
65 if key.key_id == self.key_id:
66 self.locked = False
67 # return the key
68 return key
69 # return None if not a key or key did not fit
70 return None
71 else:
72 return self.locked
73
74 class Key (GameObject):
75 def __init__ (self, row, col, text, image, key_id):
76 self.key_id = key_id
77 GameObject.__init__ (self, row, col, text, image, True)
78
79 # no interaction with this object
80 def interact (self):
81 # key is not a solid object so return True
82 return True
83
84 # using the key - this is relegated to the locked item for
85 # convenience, so key does nothing by defaults
86 def use (self, lockitem):
87 pass