X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=blobdiff_plain;f=maingame.py;h=9ba5ea279030242ffbc388b2dc80f7a0468c0fdb;hp=05e8916ad103d9397a8ede0b1216b9a4f10064d9;hb=58f80c37fa3c97f3cfb2a0cdd3096c529f65e204;hpb=6552a4a3522d0b48177a9c2ebedda92b824874f7 diff --git a/maingame.py b/maingame.py index 05e8916..9ba5ea2 100644 --- a/maingame.py +++ b/maingame.py @@ -9,6 +9,8 @@ import butaba import utility import gameobjects import constants +import npcs +import gamestate class MainGame: @@ -27,6 +29,9 @@ class MainGame: self.img_chestbg = pygame.image.load (os.path.join ("background", "chestcontent.png")).convert () + self.img_dialogue = pygame.image.load (os.path.join ("background", "dialog_screen.png")).convert () + self.img_dialogue.set_colorkey (pygame.Color (0, 255, 0)) + # initialize object graphics self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert () @@ -45,6 +50,8 @@ class MainGame: self.img_key2.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)) + self.img_bucket = pygame.image.load (os.path.join ("objects", "bucket.png")).convert () + self.img_bucket.set_colorkey (pygame.Color (0, 255, 0)) # initialize player graphics self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert () @@ -56,6 +63,14 @@ class MainGame: self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert () self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0)) + # initialize NPC graphics + self.img_bulisa = pygame.image.load (os.path.join ("sprite", "bulisa.png")).convert () + self.img_bulisa.set_colorkey (pygame.Color (0, 255, 0)) + + # initialize portraits + self.img_butaba_portrait = pygame.image.load (os.path.join ("portraits", "butaba.png")).convert () + self.img_bulisa_portrait = pygame.image.load (os.path.join ("portraits", "bulisa.png")).convert () + # set level data self.setup_levels () # set current level and position of our character @@ -75,24 +90,47 @@ class MainGame: key2 = gameobjects.Key (5, 3, "a chest key", self.img_key, constants.KEY_CHEST2) potion = gameobjects.HealthPotion (5, 2, self.img_redpotion) gold50 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 50) - gold75 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 75) + gold25 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 25) + gold10 = gameobjects.GoldCoins (6, 2, self.img_goldcoins, 10) + bucket = gameobjects.Bucket (6, 3, self.img_bucket) + + well1 = gameobjects.Well (4, 7) + well2 = gameobjects.Well (5, 7) + well3 = gameobjects.Well (4, 8) + well4 = gameobjects.Well (5, 8) - chest1.objects = [ gold50, gold75 ] + npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisa, self.img_bulisa_portrait, + [ os.path.join ("dialogues", "bulisa1.dlg"), + os.path.join ("dialogues", "bulisa2.dlg"), + os.path.join ("dialogues", "bulisa3.dlg"), + os.path.join ("dialogues", "bulisa4.dlg") ] ) + + chest1.objects = [ gold50, gold25, key2, gold10 ] # create the levels - self.level1 = level.Level (cPickle.load (file ("levels/level1.dat")), + self.level1 = level.Level (cPickle.load (file (os.path.join ("levels", "level1.dat"))), objects = [ chest1 ] ) - self.level1w = level.Level (cPickle.load (file ("levels/level1w.dat"))) + self.level1w = level.Level (cPickle.load (file (os.path.join ("levels", "level1w.dat")))) + + self.level1e = level.Level (cPickle.load (file (os.path.join ("levels", "level1e.dat"))), + objects = [ key1, potion, chest2 ], npcs = [ npc_bulisa ]) - self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")), - objects = [ key1, key2, potion, chest2 ]) + self.level1ee = level.Level (cPickle.load (file (os.path.join ("levels", "level1ee.dat"))), + objects = [ well1, well2, well3, well4 ]) + + self.level1n = level.Level (cPickle.load (file (os.path.join ("levels", "level1n.dat"))), + objects = [ bucket ]) # set up the interaction between levels self.level1.levelright = self.level1e self.level1.levelleft = self.level1w self.level1e.levelleft = self.level1 + self.level1e.levelright = self.level1ee + self.level1ee.levelleft = self.level1e self.level1w.levelright = self.level1 + self.level1.leveltop = self.level1n + self.level1n.levelbottom = self.level1 def main_loop (self): # main game loop @@ -103,6 +141,8 @@ class MainGame: self.draw_level_background (self.currentlevel) # draw level objects self.draw_level_objects (self.currentlevel) + # draw the NPCs in the level + self.draw_level_npcs (self.currentlevel) # draw our character self.draw_butaba () # display the character's inventory @@ -135,15 +175,11 @@ class MainGame: # drink a health potion if it is in the player's inventory def inventory_drink_health_potion (self): - # if health is maxed out then ignore - if self.butaba.health == butaba.Butaba.MAXHEALTH: - self.status_message = "You already have maximum health." - else: - # look for a health potion - for item in self.butaba.inventory: - if isinstance (item, gameobjects.HealthPotion) is True: - self.use_object (self.butaba, item) - break + # look for a health potion + for item in self.butaba.objects: + if isinstance (item, gameobjects.HealthPotion) is True: + self.use_object (self.butaba, item) + break def move_butaba_up (self): # clear any status messages @@ -158,9 +194,8 @@ class MainGame: # if there is a level above set current level to that one if self.currentlevel.leveltop is not None: lastrow = len (self.currentlevel.leveltop.background) - 1 - # if there is any object in that place interact with it - # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel.leveltop, lastrow, self.butaba.col) is False: + # interact with objects + if self.level_interact (self.currentlevel.leveltop, lastrow, self.butaba.col) is False: return # make sure there is no obstacle @@ -171,7 +206,7 @@ class MainGame: else: # if there is any object in that place interact with it # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel, self.butaba.row-1, self.butaba.col) is False: + if self.level_interact (self.currentlevel, self.butaba.row-1, self.butaba.col) is False: return if self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False: @@ -191,7 +226,7 @@ class MainGame: if self.currentlevel.levelbottom is not None: # interact with objects if any # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel.levelbottom, 0, self.butaba.col) is False: + if self.level_interact (self.currentlevel.levelbottom, 0, self.butaba.col) is False: return # make sure there is no obstacle at that position if self.check_background_obstacle (self.currentlevel.levelbottom, 0, self.butaba.col) is False: @@ -201,7 +236,7 @@ class MainGame: else: # interact with objects if any # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel, self.butaba.row+1, self.butaba.col) is False: + if self.level_interact (self.currentlevel, self.butaba.row+1, self.butaba.col) is False: return if self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False: self.butaba.row += 1 @@ -213,16 +248,88 @@ class MainGame: else: return False - # get and interact with objects if present in a particular row/col - def interact_objects (self, level, row, col): + # get and interact with objects and characters if present in a particular row/col + def level_interact (self, level, row, col): objs = [] # get list of objects at current location for obj in level.objects: if obj.row == row and obj.col == col: objs.append (obj) + notblock = self.interact_objects (level, objs) + + # get npc at current location + current_npc = None + for npc in level.npcs: + if npc.row == row and npc.col == col: + current_npc = npc + break + + # npcs always block the tile. So return false if there is an NPC + # at the location + if current_npc is not None: + self.interact_npc (current_npc) + return False + + return notblock + + # interaction with npcs + def interact_npc (self, npc): + # interact with NPC and get the response ID + # if the NPC is Bulisa + if isinstance (npc, npcs.Bulisa): + self.interact_npc_bulisa (npc) + + # interact with NPC Bulisa + def interact_npc_bulisa (self, npc): + # set initial response ID to none + resp_id = None + # not yet started mission drawing water from well and not refused it + if (gamestate.mission_bulisa_water_from_well is False + and gamestate.mission_bulisa_water_from_well_refused is False): + # set the current dialogue + npc.currentdialog = 0 + # get the response ID + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90) + if (gamestate.mission_bulisa_water_from_well_refused is True and + gamestate.mission_bulisa_water_from_well is False): + # set the current dialog + npc.currentdialog = 2 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90) + # mission accepted but not completed - check if completed and set value + # accordingly + elif (gamestate.mission_bulisa_water_from_well is True + and gamestate.mission_bulisa_water_from_well_complete is False): + for invobj in self.butaba.objects: + if isinstance (invobj, gameobjects.Bucket) is True: + if invobj.liquid == "water": + gamestate.mission_bulisa_water_from_well_complete = True + self.butaba.objects.remove (invobj) + break + # water mission is not completed yet + if gamestate.mission_bulisa_water_from_well_complete is False: + npc.currentdialog = 1 + else: + npc.currentdialog = 3 + + # get the response ID + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90) + + # if response ID is 12, then drawing water from well mission is refused + if resp_id == "12" or resp_id == "18": + gamestate.mission_bulisa_water_from_well_refused = True + # if response ID is 13: that is accepted the drawing water from well mission begins + if resp_id == "13" or resp_id == "17": + gamestate.mission_bulisa_water_from_well = True + # if response ID is none + elif resp_id is None: + self.status_message = "You cannot initiate a conversation with %s" % npc.charname + + # interaction with objects + def interact_objects (self, container, objs): # overall flag for blocking/non-blocking objects notblock = True + # now perform interaction for obj in objs: # run the object interact function @@ -230,19 +337,29 @@ class MainGame: notblock = False # if object can be picked up ask if obj.can_pickup is True: - ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", "Use", "Ignore"], self.img_menu) + ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", obj.use_str, "Ignore"], self.img_menu) # if the answer is "pick up" if ans == 1: - self.pickup_object (self.currentlevel, obj) + self.pickup_object (container, obj) elif ans == 2: # use the object according to its type - self.use_object (self.currentlevel, obj) + self.use_object (container, obj) # if it cannot be picked up, try to use it anyway else: - self.use_object (self.currentlevel, obj) + ans = utility.ask_question (self.screen, "Found %s." % obj.text, [obj.use_str, "Ignore"], self.img_menu) + if ans == 1: + self.use_object (container, obj) return notblock + # transfer an object from one container to another + # container must have an objects list + def transfer_object (self, source, obj, dest): + # remove object from source + source.objects.remove (obj) + # add object to destination + dest.objects.append (obj) + # picking up an object def pickup_object (self, container, obj): # only if object can be picked up, pick it up or use it @@ -252,9 +369,7 @@ class MainGame: self.status_message = "Cannot pick up item. Inventory full" else: # add item to inventory - self.butaba.objects.append (obj) - # remove from container - container.objects.remove (obj) + self.transfer_object (container, obj, self.butaba) self.status_message = "You picked up %s" % obj.text @@ -263,9 +378,12 @@ class MainGame: def use_object (self, container, obj): # if the object is a health potion if isinstance (obj, gameobjects.HealthPotion) is True: - obj.use (self.butaba) - container.objects.remove (obj) - self.status_message = "You gained health" + if self.butaba.health < butaba.Butaba.MAXHEALTH: + obj.use (self.butaba) + container.objects.remove (obj) + self.status_message = "You gained health" + else: + self.status_message = "You already have maximum health!" # if the object is a chest elif isinstance (obj, gameobjects.Chest) is True: # if chest is locked, try to open it @@ -293,14 +411,42 @@ class MainGame: self.status_message += " and gained experience!" # display the contents of the chest else: - utility.display_container_contents (self.screen, obj, self.img_chestbg, 30) + item = utility.get_container_object (self.screen, obj, self.img_chestbg, 30) + if item is not None: + self.interact_objects (obj, [ item, ]) # if the object is gold coins elif isinstance (obj, gameobjects.GoldCoins) is True: obj.use (self.butaba) self.status_message = "You picked up %d gold." % obj.value # remove the gold coins after adding it to Butaba's gold - container.remove (obj) + container.objects.remove (obj) + # using a bucket means emptying it + elif isinstance (obj, gameobjects.Bucket) is True: + if obj.liquid is not None: + self.status_message = "You emptied the bucket of %s" % obj.liquid + obj.use (self.butaba) + else: + self.status_message = "Bucket is already empty." + # using a well + elif isinstance (obj, gameobjects.Well) is True: + # if the well is not dry, i.e. it has some liquid + if obj.liquid is not None: + # search butaba inventory for an empty bucket + for invobj in self.butaba.objects: + # bucket found, now check if it is empty + if isinstance (invobj, gameobjects.Bucket) is True: + # if empty fill it + if invobj.liquid is None: + obj.use (invobj) + self.status_message = "You successfully filled the %s with %s" % (invobj.text, obj.liquid) + if self.butaba.strength < constants.STRENGTHMAX_DRAW_WELL_WATER: + self.butaba.strength += 2 + self.status_message += " and gained strength!" + return + self.status_message = "You have no empty bucket to draw %s with!" % obj.liquid + else: + self.status_message = "%s appears to be dry!" % obj.text def move_butaba_left (self): # clear any status messages @@ -319,7 +465,7 @@ class MainGame: lastcol = len (self.currentlevel.levelleft.background[0]) - 1 # interact with objects if any # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel.levelleft, self.butaba.row, lastcol) is False: + if self.level_interact (self.currentlevel.levelleft, self.butaba.row, lastcol) is False: return # make sure there is no obstacle at that position of movement if self.check_background_obstacle (self.currentlevel.levelleft, self.butaba.row, lastcol) is False: @@ -329,7 +475,7 @@ class MainGame: else: # interact with objects if any # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel, self.butaba.row, self.butaba.col-1) is False: + if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col-1) is False: return if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False: self.butaba.col -= 1 @@ -349,7 +495,7 @@ class MainGame: if self.currentlevel.levelright is not None: # interact with objects if any # if any object is a blocking object then avoid movement - if self.interact_objects (self.currentlevel.levelright, self.butaba.row, 0) is False: + if self.level_interact (self.currentlevel.levelright, self.butaba.row, 0) is False: return # make sure there is no obstacle at that position of movement @@ -361,7 +507,7 @@ class MainGame: else: # interact with objects if any # if any object is a blocking object then avoid moving - if self.interact_objects (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False: + if self.level_interact (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False: return if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False: self.butaba.col += 1 @@ -439,3 +585,10 @@ class MainGame: for obj in level.objects: if obj.image is not None: self.screen.blit (obj.image, (obj.col*48, obj.row*48)) + + + # Draw the NPCs in the level + def draw_level_npcs (self, level): + for npc in level.npcs: + if npc.image is not None: + self.screen.blit (npc.image, (npc.col*48, npc.row*48))