From 0b36c3ccd4cb9cdf58fa5285366f023a8c931c61 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Mon, 3 Oct 2011 19:18:10 +0530 Subject: [PATCH] Added chest interaction Added interaction with chests. And also cleaned up the code for object interaction a bit so that the code is clean and the "pick up" and "use" code are not mixed up. --- maingame.py | 77 +++++++++++++++++++++++++++++++---------------------- utility.py | 38 ++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 37 deletions(-) diff --git a/maingame.py b/maingame.py index 05e8916..02527db 100644 --- a/maingame.py +++ b/maingame.py @@ -75,9 +75,12 @@ 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) + potion2 = gameobjects.HealthPotion (5, 2, self.img_redpotion) + potion3 = gameobjects.HealthPotion (5, 2, self.img_redpotion) - chest1.objects = [ gold50, gold75 ] + chest1.objects = [ gold50, gold25, potion2, potion3, key2, gold10 ] # create the levels self.level1 = level.Level (cPickle.load (file ("levels/level1.dat")), @@ -86,7 +89,7 @@ class MainGame: self.level1w = level.Level (cPickle.load (file ("levels/level1w.dat"))) self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")), - objects = [ key1, key2, potion, chest2 ]) + objects = [ key1, potion, chest2 ]) # set up the interaction between levels self.level1.levelright = self.level1e @@ -135,15 +138,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 +157,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 +169,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 +189,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 +199,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 +211,24 @@ 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) + + return notblock + + + # 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 @@ -233,13 +239,15 @@ class MainGame: ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", "Use", "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, ["Use", "Ignore"], self.img_menu) + if ans == 1: + self.use_object (container, obj) return notblock @@ -263,9 +271,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 +304,16 @@ 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) def move_butaba_left (self): # clear any status messages @@ -319,7 +332,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 +342,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 +362,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 +374,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 diff --git a/utility.py b/utility.py index bc760f3..f88430c 100644 --- a/utility.py +++ b/utility.py @@ -70,7 +70,7 @@ def ask_question (surface, question, answers, bgscreen): # function displaying the contents of a container. Object must contain # items list # edgewidth - container edges to avoid drawing items in -def display_container_contents (surface, obj, bgimage, edgewidth=0): +def get_container_object (surface, obj, bgimage, edgewidth=0): # get the number of items numitems = len (obj.objects) @@ -82,6 +82,10 @@ def display_container_contents (surface, obj, bgimage, edgewidth=0): objposx = surface.get_width()/2 - bgimage.get_width()/2 objposy = surface.get_height()/2 - bgimage.get_height()/2 + selitem = 0 + selrow = 0 + selcol = 0 + while 1: # display the background for the container surface.blit (bgimage, (objposx, objposy)) @@ -90,20 +94,44 @@ def display_container_contents (surface, obj, bgimage, edgewidth=0): i = 0 j = 0 + # display all the items in container for item in obj.objects: surface.blit (item.image, (objposx + edgewidth+ j*48, objposy + edgewidth + i*48)) j += 1 if j >= num_cols: j = 0 i += 1 - # only display as many items as will fit in the container - if i >= num_rows: - break + # only draw selector if there is at least one item + if numitems > 0: + pygame.draw.rect (surface, pygame.Color (255,255,255), + pygame.Rect(objposx + edgewidth + selcol*48, objposy + edgewidth + selrow*48, 48, 48), 1) pygame.display.update () + + # get events for event in pygame.event.get (): if event.type == pygame.QUIT: sys.exit (0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: - return + return None + elif event.key == pygame.K_RETURN: + if numitems > 0 and selitem >= 0 and selitem < numitems: + return obj.objects[selitem] + else: + return None + elif event.key == pygame.K_UP or event.key == pygame.K_LEFT: + # go to the prev item + selitem -= 1 + if selitem < 0: + selitem = numitems - 1 + selrow = selitem / num_cols + selcol = selitem % num_cols + + elif event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT: + # go to the next item + selitem += 1 + if selitem > numitems - 1: + selitem = 0 + selrow = selitem / num_cols + selcol = selitem % num_cols -- 2.20.1