From: Harishankar Date: Fri, 30 Sep 2011 14:23:25 +0000 (+0530) Subject: Added object interaction - healthpotion, lock and key X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=commitdiff_plain;h=f4b468331db23412a3d8195e8ca2650093411697 Added object interaction - healthpotion, lock and key Added object interaction for health potion, keys and locked items. --- diff --git a/background/tileset.png b/background/tileset.png index a891310..686f4da 100644 Binary files a/background/tileset.png and b/background/tileset.png differ diff --git a/gameobjects.py b/gameobjects.py index 6cb93f0..e64dc91 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -38,7 +38,38 @@ class HealthPotion (GameObject): # using the potion def use (self, butaba): - pass + butaba.health += 25 + if butaba.health > butaba.MAXHEALTH: + butaba.health = butaba.MAXHEALTH + +class Chest (GameObject): + def __init__ (self, row, col, text, image, key_id, locked = False, items = []): + self.key_id = key_id + self.locked = locked + self.items = items + GameObject.__init__ (self, row, col, text, image, False) + + # no interaction with this object. Also solid so return False + def interact (self): + # object is solid + return False + + # try to use the key passed to it + def use (self, key): + # if chest is locked try to unlock it + if self.locked is True: + # if the item is a key + if isinstance (key, Key): + # if the key fits the lock + # unlock the chest + if key.key_id == self.key_id: + self.locked = False + # return the key + return key + # return None if not a key or key did not fit + return None + else: + return self.locked class Key (GameObject): def __init__ (self, row, col, text, image, key_id): @@ -50,11 +81,7 @@ class Key (GameObject): # key is not a solid object so return True return True - # using the key + # using the key - this is relegated to the locked item for + # convenience, so key does nothing by defaults def use (self, lockitem): - if type (lockitem) == Chest or type (lockitem) == Door: - if self.key_id == lockitem.key_id: - if lockitem.unlocked is False: - lockitem.unlocked = True - else: - lockitem.unlocked = True + pass diff --git a/level.py b/level.py index 04bc6cd..8224332 100644 --- a/level.py +++ b/level.py @@ -16,29 +16,29 @@ KEY_ROOM1 = 1001 # start level background data LEVEL_1 = [ - [ (0, 0, 0), (1, 0, 0), (0, 5, 1), (0, 6, 1), (1, 0, 0), (0, 0, 0), (1, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ], + [ (0, 0, 0), (1, 0, 0), (0, 5, 1), (0, 6, 1), (1, 0, 0), (0, 0, 0), (1, 0, 0), (1, 0, 0), (0, 0, 0), (1, 0, 0) ], [ (0, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (0, 0, 0), (3, 8, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1) ], [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], - [ (0, 0, 0), (1, 0, 0), (0, 0, 0), (2, 0, 1), (0, 0, 0), (1, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], + [ (2, 0, 1), (2, 0, 1), (2, 0, 1), (2, 0, 1), (0, 0, 0), (1, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], [ (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (4, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], [ (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (5, 5, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], - [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], + [ (2, 0, 1), (2, 0, 1), (2, 0, 1), (2, 0, 1), (1, 0, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (2, 0, 1), (1, 0, 0), (1,11, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0) ], [ (0, 0, 0), (0, 0, 0), (0, 5, 1), (0, 6, 1), (0, 0, 0), (0,10, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0, 7, 1) ], - [ (1, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (1, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (2, 0, 1), (1, 0, 0) ] + [ (1, 0, 0), (0, 0, 0), (1, 5, 1), (1, 6, 1), (1, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 0, 0), (1, 0, 0) ] ] # level to the east of start level background data LEVEL_1E = [ [ (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0) ], - [ (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (3, 9, 1), (0, 0, 0), (0, 0, 0), (2, 0, 0) ], + [ (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (3, 9, 1), (0, 0, 0), (0, 0, 0), (2, 0, 1) ], [ (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (0, 0, 0), (0, 0, 0) ], - [ (3,10, 0), (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ], - [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ], - [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ], - [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ], - [ (3,10, 0), (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (0, 0, 0), (0, 0, 0) ], - [ (0, 7, 1), (3 ,7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0,11, 1), (0, 0, 0), (0, 0, 0), (2, 0, 0) ], + [ (0, 9, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ], + [ (0, 8, 1), (3,10, 0), (6, 2, 1), (6, 3, 1), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ], + [ (0, 8, 1), (3,10, 0), (7, 2, 0), (7, 3, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 6, 0), (3, 5, 0) ], + [ (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (3, 5, 0), (3, 6, 0) ], + [ (0, 8, 1), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (3,10, 0), (0, 8, 1), (0, 0, 0), (0, 0, 0), (0, 0, 0) ], + [ (3, 7, 1), (0 ,7, 1), (0, 7, 1), (0, 7, 1), (0, 7, 1), (1,10, 1), (0,11, 1), (0, 0, 0), (0, 0, 0), (2, 0, 1) ], [ (0, 0, 0), (0, 0, 0), (1, 0, 0), (3, 4, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0) ] ] diff --git a/maingame.py b/maingame.py index c68e215..b894164 100644 --- a/maingame.py +++ b/maingame.py @@ -35,9 +35,10 @@ class MainGame: self.img_bulb.set_colorkey (pygame.Color (0, 255, 0)) self.img_lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert () self.img_lightning.set_colorkey (pygame.Color (0, 255, 0)) - self.img_key = pygame.image.load (os.path.join ("objects", "key.png")).convert () self.img_key.set_colorkey (pygame.Color (0, 255, 0)) + self.img_chest = pygame.image.load (os.path.join ("objects", "chest.png")).convert () + self.img_chest.set_colorkey (pygame.Color (0, 255, 0)) # initialize player graphics self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert () @@ -57,16 +58,18 @@ class MainGame: # set the status message self.status_message = "Game started" - self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT) + self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT, 75) # set up the levels and their interactions def setup_levels (self): - self.level1 = level.Level (level.LEVEL_1, - objects = [gameobjects.HealthPotion (4, 3, self.img_redpotion) ]) + self.level1 = level.Level (level.LEVEL_1) self.level1e = level.Level (level.LEVEL_1E, objects = [ gameobjects.Key (4, 3, "a chest key", self.img_key, level.KEY_CHEST1), - gameobjects.Key (4, 3, "a room key", self.img_key, level.KEY_ROOM1)] + gameobjects.Key (4, 3, "a room key", self.img_key, level.KEY_ROOM1), + gameobjects.HealthPotion (4, 2, self.img_redpotion), + gameobjects.Chest (2, 5, "chest", self.img_chest, level.KEY_CHEST1, True) + ] ) self.level1.levelright = self.level1e self.level1e.levelleft = self.level1 @@ -193,10 +196,10 @@ class MainGame: # picking up an object def pickup_object (self, obj): - # only if object can be picked up, pick it up + # only if object can be picked up, pick it up or use it if obj.can_pickup is True: - ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Carry", "Ignore"], self.img_menu) - # if the answer is "take" + ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", "Use", "Ignore"], self.img_menu) + # if the answer is "carry" if ans == 1: # check if the inventory is full if len (self.butaba.inventory) >= butaba.Butaba.MAXITEMS: @@ -207,7 +210,44 @@ class MainGame: self.currentlevel.objects.remove (obj) self.status_message = "You picked up %s" % obj.text - + elif ans == 2: + # use the object according to its type + self.use_object (obj) + # if it cannot be picked up, try to use it anyway + else: + self.use_object (obj) + + # this method uses the object first by calling the object use () method + # and then performing specific actions as necessary + def use_object (self, obj): + # if the object is a health potion + if isinstance (obj, gameobjects.HealthPotion) is True: + obj.use (self.butaba) + self.currentlevel.objects.remove (obj) + self.status_message = "You gained health" + # if the object is a chest + elif isinstance (obj, gameobjects.Chest) is True: + # if chest is locked, try to open it + if obj.locked is True: + # try opening the chest with every item 9the use () function + # of the chest determines if item is a key anyway + fittedkey = None + for invobj in self.butaba.inventory: + fittedkey = obj.use (invobj) + # if a key fits + if fittedkey is not None: + break + # if no key found + if fittedkey is None: + self.status_message = "No key found to open %s" % obj.text + # chest successfully unlocked + else: + self.status_message = "You unlocked the %s" % obj.text + # remove the key from inventory + self.butaba.inventory.remove (fittedkey) + # display the contents of the chest + else: + pass def move_butaba_left (self): # clear any status messages