X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=blobdiff_plain;f=maingame.py;h=9efc5cd3a65cdaf96ee0035ddbce344880f4a476;hp=c68e2151cb4a86c1cca2844d1a723e8d65483a56;hb=00470eddc34eeaab26209197d9e92995fbf12498;hpb=4b6e952b476861c8979dc395781964099285a4c6 diff --git a/maingame.py b/maingame.py index c68e215..9efc5cd 100644 --- a/maingame.py +++ b/maingame.py @@ -2,17 +2,24 @@ import pygame import sys import random import os.path +import cPickle import level import butaba import utility import gameobjects +import constants +import npcs +import gamestate class MainGame: # initialize the game def __init__ (self): + random.seed () pygame.init () + + self.clock = pygame.time.Clock () self.screen = pygame.display.set_mode ((720, 512)) pygame.display.set_caption ("The Adventures of Butaba") @@ -23,6 +30,11 @@ class MainGame: self.img_inventory = pygame.image.load (os.path.join ("background", "inventory.png")).convert () + 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 () @@ -35,9 +47,14 @@ 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_key2 = pygame.image.load (os.path.join ("objects", "key2.png")).convert () + 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 () @@ -49,6 +66,36 @@ 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_bulisafront = pygame.image.load (os.path.join ("sprite", "bulisa-front.png")).convert () + self.img_bulisafront.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_bulisaback = pygame.image.load (os.path.join ("sprite", "bulisa-back.png")).convert () + self.img_bulisaback.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_bulisaleft = pygame.image.load (os.path.join ("sprite", "bulisa-left.png")).convert () + self.img_bulisaleft.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_bulisaright = pygame.image.load (os.path.join ("sprite", "bulisa-right.png")).convert () + self.img_bulisaright.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_mayorfront = pygame.image.load (os.path.join ("sprite", "mayor-front.png")).convert () + self.img_mayorfront.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_mayorback = pygame.image.load (os.path.join ("sprite", "mayor-back.png")).convert () + self.img_mayorback.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_mayorleft = pygame.image.load (os.path.join ("sprite", "mayor-left.png")).convert () + self.img_mayorleft.set_colorkey (pygame.Color (0, 255, 0)) + + self.img_mayorright = pygame.image.load (os.path.join ("sprite", "mayor-right.png")).convert () + self.img_mayorright.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 () + self.img_mayor_portrait = pygame.image.load (os.path.join ("portraits", "mayor.png")).convert () + # set level data self.setup_levels () # set current level and position of our character @@ -57,29 +104,119 @@ 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, self.img_butabaleft, + self.img_butabaright, self.img_butabafront, + self.img_butababack, self.img_butaba_portrait, constants.RIGHT) # set up the levels and their interactions def setup_levels (self): + # set up the objects first + chest1 = gameobjects.Chest (2, 6, "chest", self.img_chest, constants.KEY_CHEST1, True) + chest2 = gameobjects.Chest (6, 6, "chest", self.img_chest, constants.KEY_CHEST2, True) + 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) + 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) + + npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisaleft, self.img_bulisaright, + self.img_bulisafront, self.img_bulisaback, + self.img_bulisa_portrait, constants.FRONT, + (2, 2, 2, 2), + [ os.path.join ("dialogues", "bulisa1.dlg"), + os.path.join ("dialogues", "bulisa2.dlg"), + os.path.join ("dialogues", "bulisa3.dlg"), + os.path.join ("dialogues", "bulisa4.dlg"), + os.path.join ("dialogues", "bulisa5.dlg"), + os.path.join ("dialogues", "bulisa6.dlg"), + os.path.join ("dialogues", "bulisa7.dlg") ] ) + + npc_mayor = npcs.Mayor (5, 4, self.img_mayorleft, self.img_mayorright, + self.img_mayorfront, self.img_mayorback, + self.img_mayor_portrait, constants.FRONT, + (2, 2, 2, 2), + [ os.path.join ("dialogues", "mayor1.dlg"), + os.path.join ("dialogues", "mayor2.dlg"), + os.path.join ("dialogues", "mayor3.dlg") ]) + + chest1.objects = [ gold50, gold25, key2, gold10 ] + + # create the levels + self.level1 = level.Level (cPickle.load (file (os.path.join ("levels", "level1.dat"))), + objects = [ chest1 ] ) + + 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 = [ potion, chest2 ], npcs = [ npc_bulisa ]) + + 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 ]) + + self.level1nw = level.Level (cPickle.load (file (os.path.join ("levels", "level1nw.dat")))) + + self.level1ne = level.Level (cPickle.load (file (os.path.join ("levels", "level1ne.dat")))) + + self.level1nee = level.Level (cPickle.load (file (os.path.join ("levels", "level1nee.dat")))) + + self.level1sw = level.Level (cPickle.load (file (os.path.join ("levels", "level1sw.dat")))) + + self.level1sww = level.Level (cPickle.load (file (os.path.join ("levels", "level1sww.dat")))) + + self.level1ww = level.Level (cPickle.load (file (os.path.join ("levels", "level1ww.dat"))), + npcs = [ npc_mayor ]) - self.level1 = level.Level (level.LEVEL_1, - objects = [gameobjects.HealthPotion (4, 3, self.img_redpotion) ]) - 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)] - ) + # set up the interaction between levels (level exits) 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 + self.level1n.levelleft = self.level1nw + self.level1w.leveltop = self.level1nw + self.level1nw.levelright = self.level1n + self.level1nw.levelbottom = self.level1w + self.level1n.levelright = self.level1ne + self.level1e.leveltop = self.level1ne + self.level1ne.levelleft = self.level1n + self.level1ne.levelbottom = self.level1e + self.level1ne.levelright = self.level1nee + self.level1ee.leveltop = self.level1nee + self.level1nee.levelleft = self.level1ne + self.level1nee.levelbottom = self.level1ee + self.level1sw.leveltop = self.level1w + self.level1w.levelbottom = self.level1sw + self.level1sww.levelright = self.level1sw + self.level1sw.levelleft = self.level1sww + self.level1ww.levelright = self.level1w + self.level1ww.levelbottom = self.level1sww + self.level1sww.leveltop = self.level1ww + self.level1w.levelleft = self.level1ww def main_loop (self): # main game loop while 1: + self.clock.tick (25) # clear screen self.screen.fill (pygame.Color (0,0,0)) # draw the level 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 @@ -93,6 +230,7 @@ class MainGame: for event in pygame.event.get (): if event.type == pygame.QUIT: sys.exit (0) + # if keyboard event if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.move_butaba_up () @@ -102,13 +240,27 @@ class MainGame: self.move_butaba_left () elif event.key == pygame.K_RIGHT: self.move_butaba_right () + # drinking health potion in inventory + elif event.key == ord ("h") or event.key == ord ("H"): + self.inventory_drink_health_potion () + # quit the game + elif event.key == ord ("q") or event.key == ord ("Q"): + sys.exit (0) + + # drink a health potion if it is in the player's inventory + def inventory_drink_health_potion (self): + # 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 self.status_message = None # first if butaba is not facing up, make him face up - if self.butaba.position <> butaba.Butaba.BACK: - self.butaba.position = butaba.Butaba.BACK + if self.butaba.position <> constants.BACK: + self.butaba.position = constants.BACK return # if butaba is trying to move off the top of the screen @@ -116,9 +268,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 @@ -129,7 +280,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: @@ -139,8 +290,8 @@ class MainGame: # clear any status messages self.status_message = None # first if butaba is not facing forward, make him face forward/down - if self.butaba.position <> butaba.Butaba.FRONT: - self.butaba.position = butaba.Butaba.FRONT + if self.butaba.position <> constants.FRONT: + self.butaba.position = constants.FRONT return # if butaba is trying to move off the bottom of the screen @@ -149,7 +300,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: @@ -159,7 +310,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 @@ -171,51 +322,267 @@ 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 the NPC is dead cannot talk with the NPC + if npc.is_dead is True: + self.status_message = "%s is dead! RIP..." % npc.charname + return + + if isinstance (npc, npcs.Bulisa): + # interact with Bulisa + self.interact_npc_bulisa (npc) + elif isinstance (npc, npcs.Mayor): + # interact with the Mayor + self.interact_npc_mayor (npc) + + # interact with the mayor + def interact_npc_mayor (self, npc): + resp_id = None + + if gamestate.flag["mission_charity_money"] is True: + npc.currentdialog = 2 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + elif gamestate.flag["mission_charity_informed_mayor"] is True: + npc.currentdialog = 1 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + else: + npc.currentdialog = 0 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + + + # deal with the respnses + if resp_id == "missioncharityaccepted": + gamestate.flag["mission_charity_money"] = True + # response ID is none + elif resp_id is None: + self.status_message = "You cannot initiate a conversation with %s" % npc.charname + + # interact with NPC Bulisa + def interact_npc_bulisa (self, npc): + # set initial response ID to none + resp_id = None + + # check for global game states (starting from later flags to earlier ones + + # whether butaba is doing the mayor charity mission + if gamestate.flag["mission_charity_money"] is True: + npc.currentdialog = 6 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + # whether the drawing water from well mission completed + elif gamestate.flag["mission_charity_informed"] is True: + npc.currentdialog = 5 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + elif gamestate.flag['mission_bulisa_water_from_well_complete'] is True: + npc.currentdialog = 4 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + # whether the drawing water from well mission refused + elif gamestate.flag['mission_bulisa_water_from_well_refused'] is True: + npc.currentdialog = 2 + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + # whether the drawing water from well mission is accepted + elif gamestate.flag ['mission_bulisa_water_from_well'] is True: + # check if butaba has a bucket full of water + for invobj in self.butaba.objects: + if isinstance (invobj, gameobjects.Bucket) is True: + # if yes, then mission is completed accordingly + if invobj.liquid == "water": + gamestate.flag["mission_bulisa_water_from_well_complete"] = True + self.butaba.objects.remove (invobj) + key1 = gameobjects.Key (5, 3, "a chest key", self.img_key2, constants.KEY_CHEST1) + self.butaba.objects.append (key1) + break + # water mission is not completed yet + if gamestate.flag["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.butaba.portrait, 0, 90) + # Butaba hasn't been told about the initial mission yet, so tell him now + else: + npc.currentdialog = 0 + # get the response ID + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90) + + # NPC RESPONSES: + # Check each response + + # Drawing water from well mission is refused + if resp_id == "refusedwatermission": + gamestate.flag["mission_bulisa_water_from_well_refused"] = True + # Drawing water from well mission begins + elif resp_id == "acceptedwatermission": + gamestate.flag["mission_bulisa_water_from_well"] = True + gamestate.flag["mission_bulisa_water_from_well_refused"] = False + # Butaba is informed about the mayor's charity + elif resp_id == "informedcharitymission": + gamestate.flag["mission_charity_informed"] = True + # if mayor has been told about Butaba + elif resp_id == "mayortoldaboutbutaba": + gamestate.flag["mission_charity_informed_mayor"] = 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 if obj.interact () is False: notblock = False # if object can be picked up ask - self.pickup_object (obj) + if obj.can_pickup is True: + 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 (container, obj) + elif ans == 2: + # use the object according to its type + self.use_object (container, obj) + # if it cannot be picked up, try to use it anyway + else: + 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, obj): - # only if object can be picked up, pick it up + def pickup_object (self, container, obj): + # 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" - if ans == 1: - # check if the inventory is full - if len (self.butaba.inventory) >= butaba.Butaba.MAXITEMS: - self.status_message = "Failed. Inventory full" + # check if the inventory is full + if len (self.butaba.objects) >= constants.MAXITEMS: + self.status_message = "Cannot pick up item. Inventory full" + else: + # add item to inventory + self.transfer_object (container, obj, self.butaba) + + self.status_message = "You picked up %s" % obj.text + + # this method uses the object first by calling the object use () method + # and then performing specific actions as necessary + def use_object (self, container, obj): + # if the object is a health potion + if isinstance (obj, gameobjects.HealthPotion) is True: + if self.butaba.health < constants.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 + 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.objects: + 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: - # add item to inventory - self.butaba.inventory.append (obj) - self.currentlevel.objects.remove (obj) - - self.status_message = "You picked up %s" % obj.text - + self.status_message = "You unlocked the %s" % obj.text + # remove the key from Butaba + self.butaba.objects.remove (fittedkey) + # add an experience point for unlocking chest subject + # to a limit of KNOWLEDGEMAX_CHEST_UNLOCK + if self.butaba.experience < constants.KNOWLEDGEMAX_CHEST_UNLOCK: + self.butaba.experience += 1 + self.status_message += " and gained experience!" + # display the contents of the chest + else: + 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.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 self.status_message = None # first if Butaba is not facing left, make him face left - if self.butaba.position <> butaba.Butaba.LEFT: - self.butaba.position = butaba.Butaba.LEFT + if self.butaba.position <> constants.LEFT: + self.butaba.position = constants.LEFT return # if butaba is trying to move off the left edge @@ -226,7 +593,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: @@ -236,7 +603,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 @@ -246,8 +613,8 @@ class MainGame: self.status_message = None # First if Butaba is not facing right make him face right - if self.butaba.position <> butaba.Butaba.RIGHT: - self.butaba.position = butaba.Butaba.RIGHT + if self.butaba.position <> constants.RIGHT: + self.butaba.position = constants.RIGHT return # if butaba is trying to move off the right edge @@ -256,7 +623,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 @@ -268,49 +635,49 @@ 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 def draw_butaba (self): - if self.butaba.position == butaba.Butaba.FRONT: - self.screen.blit (self.img_butabafront, (self.butaba.col*48, self.butaba.row*48)) - elif self.butaba.position == butaba.Butaba.BACK: - self.screen.blit (self.img_butababack, (self.butaba.col*48, self.butaba.row*48)) - elif self.butaba.position == butaba.Butaba.LEFT: - self.screen.blit (self.img_butabaleft, (self.butaba.col*48, self.butaba.row*48)) - elif self.butaba.position == butaba.Butaba.RIGHT: - self.screen.blit (self.img_butabaright, (self.butaba.col*48, self.butaba.row*48)) + if self.butaba.position == constants.FRONT: + self.screen.blit (self.butaba.imagefront, (self.butaba.col*48, self.butaba.row*48)) + elif self.butaba.position == constants.BACK: + self.screen.blit (self.butaba.imageback, (self.butaba.col*48, self.butaba.row*48)) + elif self.butaba.position == constants.LEFT: + self.screen.blit (self.butaba.imageleft, (self.butaba.col*48, self.butaba.row*48)) + elif self.butaba.position == constants.RIGHT: + self.screen.blit (self.butaba.imageright, (self.butaba.col*48, self.butaba.row*48)) # Draw the status infodisplay def draw_status (self): self.screen.blit (self.img_redpotion, (485, 10)) - utility.put_text (self.screen, 550, 25, 24, (255, 0, 0), "%d" % self.butaba.health) + utility.put_text (self.screen, 550, 25, 20, (255, 0, 0), "%d" % self.butaba.health) self.screen.blit (self.img_lightning, (620, 10)) - utility.put_text (self.screen, 660, 20, 24, (255,255,255), "%d" % self.butaba.strength) + utility.put_text (self.screen, 660, 25, 20, (255,255,255), "%d" % self.butaba.strength) self.screen.blit (self.img_wand, (485, 65)) - utility.put_text (self.screen, 550, 75, 24, (0, 0, 255), "%d" % self.butaba.magic) + utility.put_text (self.screen, 550, 75, 20, (0, 0, 255), "%d" % self.butaba.magic) self.screen.blit (self.img_bulb, (620, 65)) - utility.put_text (self.screen, 660, 75, 24, (0, 255, 0), "%d" % self.butaba.experience) + utility.put_text (self.screen, 660, 75, 20, (0, 255, 0), "%d" % self.butaba.experience) self.screen.blit (self.img_goldcoins, (485, 110)) - utility.put_text (self.screen, 550, 130, 24, (255, 255, 0), "%d" % self.butaba.gold) + utility.put_text (self.screen, 550, 130, 20, (255, 255, 0), "%d" % self.butaba.gold) if self.status_message is not None: - utility.put_text (self.screen, 10, 485, 18, (255,255, 0), "%s" % self.status_message) + utility.put_text (self.screen, 10, 485, 10, (255,255, 0), "%s" % self.status_message) # display the inventory of the player def draw_inventory (self): # draw the inventory slots r = 1 c = 1 - utility.put_text (self.screen, 490, 170, 22, (255,255 , 0), "Inventory") - for i in range (butaba.Butaba.MAXITEMS): + utility.put_text (self.screen, 490, 170, 16, (255,255 , 0), "Inventory") + for i in range (constants.MAXITEMS): self.screen.blit (self.img_inventory, (440+c*54, 150+r*54)) if c % 4 == 0: r += 1 @@ -320,7 +687,7 @@ class MainGame: r = 1 c = 1 - for obj in self.butaba.inventory: + for obj in self.butaba.objects: self.screen.blit (obj.image, (440+c*54+2, 150+r*54+2)) if c % 4 == 0: r += 1 @@ -346,3 +713,118 @@ 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 is not dead then move the NPC randomly depending on their + # movement area + if npc.is_dead is False: + # move this turn? + if random.randint (1, 500) < npc.movement_speed: + # whether to change direction? + if random.randint (1, 100) < 25: + npc.position = random.randint (0, 3) + else: + # if left movement + if npc.position == constants.LEFT: + # cannot move beyond level and cannot move beyond the + # left limit area + if npc.col > 0 and npc.col > npc.initcol - npc.leftlimit: + # check if there is any background obstacle + if self.check_background_obstacle (level, npc.row, npc.col - 1) is False: + # check if there are any objects + objblock = False + npcblock = False + for lobj in level.objects: + if lobj.row == npc.row and lobj.col == npc.col - 1: + objblock = True + break + # check if there any any npcs blocking + for lnpc in level.npcs: + if lnpc.row == npc.row and lnpc.col == npc.col - 1: + npcblock = True + break + if objblock is False and npcblock is False: + # if butaba is not blocking + if self.butaba.row <> npc.row or self.butaba.col <> npc.col - 1: + npc.col -= 1 + elif npc.position == constants.RIGHT: + # cannot move beyond level and cannot move beyond the + # right limit area + if npc.col < 9 and npc.col < npc.initcol + npc.rightlimit: + # check if there is any background obstacle + if self.check_background_obstacle (level, npc.row, npc.col + 1) is False: + # check if there are any objects + objblock = False + npcblock = False + for lobj in level.objects: + if lobj.row == npc.row and lobj.col == npc.col + 1: + objblock = True + break + # check if there any any npcs blocking + for lnpc in level.npcs: + if lnpc.row == npc.row and lnpc.col == npc.col + 1: + npcblock = True + break + if objblock is False and npcblock is False: + # if butaba is not blocking + if self.butaba.row <> npc.row or self.butaba.col <> npc.col + 1: + npc.col += 1 + elif npc.position == constants.FRONT: + # cannot move beyond level and cannot move beyond the + # lower bottom limit area + if npc.row < 9 and npc.row < npc.initrow + npc.bottomlimit: + # check if there is any background obstacle + if self.check_background_obstacle (level, npc.row + 1, npc.col) is False: + # check if there are any objects + objblock = False + npcblock = False + for lobj in level.objects: + if lobj.row == npc.row + 1 and lobj.col == npc.col: + objblock = True + break + # check if there any any npcs blocking + for lnpc in level.npcs: + if lnpc.row == npc.row + 1 and lnpc.col == npc.col: + npcblock = True + break + if objblock is False and npcblock is False: + # if butaba is not blocking + if self.butaba.row <> npc.row + 1 or self.butaba.col <> npc.col: + npc.row += 1 + elif npc.position == constants.BACK: + # cannot move beyond level and cannot move beyond the + # top upper limit area + if npc.row > 0 and npc.row > npc.initrow - npc.toplimit: + # check if there is any background obstacle + if self.check_background_obstacle (level, npc.row - 1, npc.col) is False: + # check if there are any objects + objblock = False + npcblock = False + for lobj in level.objects: + if lobj.row == npc.row - 1 and lobj.col == npc.col: + objblock = True + break + # check if there any any npcs blocking + for lnpc in level.npcs: + if lnpc.row == npc.row - 1 and lnpc.col == npc.col: + npcblock = True + break + # if no object is blocking + if objblock is False and npcblock is False: + # if butaba is not blocking + if self.butaba.row <> npc.row - 1 or self.butaba.col <> npc.col: + npc.row -= 1 + + if npc.position == constants.FRONT: + img = npc.imagefront + elif npc.position == constants.BACK: + img = npc.imageback + elif npc.position == constants.LEFT: + img = npc.imageleft + else: + img = npc.imageright + + self.screen.blit (img, (npc.col*48, npc.row*48))