From: Harishankar Date: Fri, 30 Sep 2011 05:23:01 +0000 (+0530) Subject: Added the inventory system (basic) X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=4b6e952b476861c8979dc395781964099285a4c6;p=butaba-adventures.git Added the inventory system (basic) Now implemented adding items to the character's inventory. Not yet done object interactions yet --- diff --git a/background/inventory.png b/background/inventory.png new file mode 100644 index 0000000..685912d Binary files /dev/null and b/background/inventory.png differ diff --git a/background/menu_screen.png b/background/menu_screen.png new file mode 100644 index 0000000..028b4c2 Binary files /dev/null and b/background/menu_screen.png differ diff --git a/butaba.py b/butaba.py index c59ee11..99c8a0a 100644 --- a/butaba.py +++ b/butaba.py @@ -7,6 +7,7 @@ class Butaba: FRONT = 2 BACK = 3 MAXITEMS = 8 + MAXHEALTH = 100 # initialize our character def __init__ (self, startrow, startcol, position=LEFT, health=100, magic=10, experience=10, strength=10, gold=0, inventory = []): diff --git a/gameobjects.py b/gameobjects.py new file mode 100644 index 0000000..6cb93f0 --- /dev/null +++ b/gameobjects.py @@ -0,0 +1,60 @@ +# object classes - classes for game interactive objects +import pygame +import os.path + +import utility + +# base class for all objects +class GameObject: + # initialization routine + def __init__ (self, row, col, text, image = None, can_pickup = True): + self.row = row + self.col = col + self.image = image + self.text = text + self.can_pickup = can_pickup + + # override this for interaction, i.e. when character walks into the item + def interact (self): + # always return True if the object is interacted with + # return False to make the object block the player from + # moving over it + return True + + # use the object on another object + def use (self, otherobject): + pass + + +class HealthPotion (GameObject): + # initialize + def __init__ (self, row, col, image): + text = "health potion" + GameObject.__init__ (self, row, col, text, image, True) + + # no interaction with this object + def interact (self): + return True + + # using the potion + def use (self, butaba): + pass + +class Key (GameObject): + def __init__ (self, row, col, text, image, key_id): + self.key_id = key_id + GameObject.__init__ (self, row, col, text, image, True) + + # no interaction with this object + def interact (self): + # key is not a solid object so return True + return True + + # using the key + 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 diff --git a/level.py b/level.py index a449365..04bc6cd 100644 --- a/level.py +++ b/level.py @@ -12,8 +12,9 @@ import object # constants for in-game use KEY_CHEST1 = 1000 +KEY_ROOM1 = 1001 -# start level +# 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), (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) ], @@ -27,7 +28,7 @@ LEVEL_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) ] ] -# level to the east of start level +# 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) ], diff --git a/maingame.py b/maingame.py index af06593..c68e215 100644 --- a/maingame.py +++ b/maingame.py @@ -6,57 +6,68 @@ import os.path import level import butaba import utility -import object +import gameobjects class MainGame: # initialize the game def __init__ (self): pygame.init () - self.screen = pygame.display.set_mode ((640, 480)) + self.screen = pygame.display.set_mode ((720, 512)) pygame.display.set_caption ("The Adventures of Butaba") # initalize background graphics - self.tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert () + self.img_tileset = pygame.image.load (os.path.join ("background", "tileset.png")).convert () + + self.img_menu = pygame.image.load (os.path.join ("background", "menu_screen.png")).convert () + + self.img_inventory = pygame.image.load (os.path.join ("background", "inventory.png")).convert () + # initialize object graphics - self.redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert () - self.redpotion.set_colorkey (pygame.Color (0, 255, 0)) - self.goldcoins = pygame.image.load (os.path.join ("objects", "gold-coins.png")).convert () - self.goldcoins.set_colorkey (pygame.Color (0, 255, 0)) - self.wand = pygame.image.load (os.path.join ("objects", "wand.png")).convert () - self.wand.set_colorkey (pygame.Color (0, 255, 0)) - self.bulb = pygame.image.load (os.path.join ("objects", "bulb.png")).convert () - self.bulb.set_colorkey (pygame.Color (0, 255, 0)) - self.lightning = pygame.image.load (os.path.join ("objects", "lightning.png")).convert () - self.lightning.set_colorkey (pygame.Color (0, 255, 0)) - - self.key = pygame.image.load (os.path.join ("objects", "key.png")).convert () - self.key.set_colorkey (pygame.Color (0, 255, 0)) + self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert () + self.img_redpotion.set_colorkey (pygame.Color (0, 255, 0)) + self.img_goldcoins = pygame.image.load (os.path.join ("objects", "gold-coins.png")).convert () + self.img_goldcoins.set_colorkey (pygame.Color (0, 255, 0)) + self.img_wand = pygame.image.load (os.path.join ("objects", "wand.png")).convert () + self.img_wand.set_colorkey (pygame.Color (0, 255, 0)) + self.img_bulb = pygame.image.load (os.path.join ("objects", "bulb.png")).convert () + 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)) # initialize player graphics - self.butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert () - self.butabafront.set_colorkey (pygame.Color (0, 255, 0)) - self.butababack = pygame.image.load (os.path.join ("sprite", "butaba-back.png")).convert () - self.butababack.set_colorkey (pygame.Color (0, 255, 0)) - self.butabaleft = pygame.image.load (os.path.join ("sprite", "butaba-left.png")).convert () - self.butabaleft.set_colorkey (pygame.Color (0, 255, 0)) - self.butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert () - self.butabaright.set_colorkey (pygame.Color (0, 255, 0)) + self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert () + self.img_butabafront.set_colorkey (pygame.Color (0, 255, 0)) + self.img_butababack = pygame.image.load (os.path.join ("sprite", "butaba-back.png")).convert () + self.img_butababack.set_colorkey (pygame.Color (0, 255, 0)) + self.img_butabaleft = pygame.image.load (os.path.join ("sprite", "butaba-left.png")).convert () + self.img_butabaleft.set_colorkey (pygame.Color (0, 255, 0)) + self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert () + self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0)) # set level data self.setup_levels () # set current level and position of our character self.currentlevel = self.level1 + # set the status message + self.status_message = "Game started" + self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT) # set up the levels and their interactions def setup_levels (self): - self.level1 = level.Level (level.LEVEL_1) + self.level1 = level.Level (level.LEVEL_1, + objects = [gameobjects.HealthPotion (4, 3, self.img_redpotion) ]) self.level1e = level.Level (level.LEVEL_1E, - objects = [ object.Key (4, 3, self.key, level.KEY_CHEST1) ]) + 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)] + ) self.level1.levelright = self.level1e self.level1e.levelleft = self.level1 @@ -71,6 +82,8 @@ class MainGame: self.draw_level_objects (self.currentlevel) # draw our character self.draw_butaba () + # display the character's inventory + self.draw_inventory () # draw the status info self.draw_status () # update the display @@ -91,6 +104,8 @@ class MainGame: self.move_butaba_right () 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 @@ -100,16 +115,29 @@ class MainGame: if self.butaba.row <= 0: # if there is a level above set current level to that one if self.currentlevel.leveltop is not None: - # make sure there is no obstacle 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: + return + + # make sure there is no obstacle if self.check_background_obstacle (self.currentlevel.leveltop, lastrow, self.butaba.col) is False: self.currentlevel = self.currentlevel.leveltop self.butaba.row = lastrow # normal upward movement - elif self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False: - self.butaba.row -= 1 + 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: + return + + if self.check_background_obstacle (self.currentlevel, self.butaba.row-1, self.butaba.col) is False: + self.butaba.row -= 1 def move_butaba_down (self): + # 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 @@ -119,12 +147,21 @@ class MainGame: if self.butaba.row >= len (self.currentlevel.background)-1: # if there is a level below set current level to that one 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: + return # make sure there is no obstacle at that position if self.check_background_obstacle (self.currentlevel.levelbottom, 0, self.butaba.col) is False: self.currentlevel = self.currentlevel.levelbottom self.butaba.row = 0 # normal downward movement - elif self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False: + 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: + return + if self.check_background_obstacle (self.currentlevel, self.butaba.row+1, self.butaba.col) is False: self.butaba.row += 1 # check if a background tile is an obstacle @@ -134,7 +171,48 @@ class MainGame: else: return False + # get and interact with objects if present in a particular row/col + def interact_objects (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) + + # 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) + + return notblock + + # picking up an object + def pickup_object (self, obj): + # only if object can be picked up, pick it up + 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" + else: + # add item to inventory + self.butaba.inventory.append (obj) + self.currentlevel.objects.remove (obj) + + self.status_message = "You picked up %s" % 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 @@ -144,17 +222,29 @@ class MainGame: if self.butaba.col <= 0: # if there is a level to the right set current level to that one if self.currentlevel.levelleft is not None: - # make sure there is no obstacle at that position of movement # get the last column of the previous level 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: + 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: self.currentlevel = self.currentlevel.levelleft self.butaba.col = lastcol # normal left movement - elif self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False: - self.butaba.col -= 1 + 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: + return + if self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col-1) is False: + self.butaba.col -= 1 def move_butaba_right (self): + # clear any status messages + 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 @@ -164,39 +254,79 @@ class MainGame: if self.butaba.col >= len (self.currentlevel.background[0])-1: # if there is a level to the right swap current level with that one if self.currentlevel.levelright is not None: - # make sure there is no obstacle at that position of movement + # 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: + return + + # make sure there is no obstacle at that position of movement # get the last column of the previous level if self.check_background_obstacle (self.currentlevel.levelright, self.butaba.row, 0) is False: self.currentlevel = self.currentlevel.levelright self.butaba.col = 0 # normal right movement - elif self.check_background_obstacle (self.currentlevel, self.butaba.row, self.butaba.col + 1) is False: - self.butaba.col += 1 + 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: + 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.butabafront, (self.butaba.col*48, self.butaba.row*48)) + 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.butababack, (self.butaba.col*48, self.butaba.row*48)) + 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.butabaleft, (self.butaba.col*48, self.butaba.row*48)) + 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.butabaright, (self.butaba.col*48, self.butaba.row*48)) + self.screen.blit (self.img_butabaright, (self.butaba.col*48, self.butaba.row*48)) # Draw the status infodisplay def draw_status (self): - self.screen.blit (self.redpotion, (485, 10)) - utility.put_text (self.screen, 550, 25, 28, (255, 0, 0), "%d" % self.butaba.health) - self.screen.blit (self.goldcoins, (485, 50)) - utility.put_text (self.screen, 550, 75, 28, (255, 255, 0), "%d" % self.butaba.gold) - self.screen.blit (self.wand, (485, 120)) - utility.put_text (self.screen, 550, 130, 28, (0, 0, 255), "%d" % self.butaba.magic) - self.screen.blit (self.bulb, (485, 180)) - utility.put_text (self.screen, 550, 190, 28, (0, 255, 0), "%d" % self.butaba.experience) - self.screen.blit (self.lightning, (485, 240)) - utility.put_text (self.screen, 550, 250, 28, (255,255,255), "%d" % self.butaba.strength) + self.screen.blit (self.img_redpotion, (485, 10)) + utility.put_text (self.screen, 550, 25, 24, (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) + + self.screen.blit (self.img_wand, (485, 65)) + utility.put_text (self.screen, 550, 75, 24, (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) + + self.screen.blit (self.img_goldcoins, (485, 110)) + utility.put_text (self.screen, 550, 130, 24, (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) + + # 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): + self.screen.blit (self.img_inventory, (440+c*54, 150+r*54)) + if c % 4 == 0: + r += 1 + c = 1 + else: + c += 1 + r = 1 + c = 1 + for obj in self.butaba.inventory: + self.screen.blit (obj.image, (440+c*54+2, 150+r*54+2)) + if c % 4 == 0: + r += 1 + c = 1 + else: + c += 1 # Draw the level background tiles on surface def draw_level_background (self, level): @@ -206,7 +336,7 @@ class MainGame: for tilerow, tilecol, is_solid in row: tilex = tilecol * 48 tiley = tilerow * 48 - self.screen.blit (self.tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48)) + self.screen.blit (self.img_tileset, (j*48, i*48), pygame.Rect (tilex, tiley, 48, 48)) j += 1 i += 1 diff --git a/object.py b/object.py deleted file mode 100644 index f6638f1..0000000 --- a/object.py +++ /dev/null @@ -1,41 +0,0 @@ -# object classes - classes for game interactive objects -import pygame -import os.path - -import utility - -# base class for all objects -class GameObject: - # initialization routine - def __init__ (self, row, col, image = None): - self.row = row - self.col = col - self.image = image - - # override this for interaction - def interact (self): - pass - - # use the object on another object - def use (self, otherobject): - pass - - -class Key (GameObject): - def __init__ (self, row, col, image, key_id): - self.key_id = key_id - GameObject.__init__ (self, row, col, image) - - # ask whether to pick up the key - def interact (self, surface): - ans = utility.ask_question (surface, "You found a key.", ["Pick it up", "Leave it"]) - return ans - - # using the key - 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 diff --git a/utility.py b/utility.py index 5d8c45d..9c68c05 100644 --- a/utility.py +++ b/utility.py @@ -1,5 +1,6 @@ # utility functions for the game import pygame +import sys import os.path # function to draw text on surface @@ -8,22 +9,61 @@ def put_text (surface, x, y, size, (r,g,b), text): textsurf = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b)) surface.blit (textsurf, (x, y)) +# function to draw several lines of text, centered horizontally and vertically on surface +def put_lines (surface, text_lines): + textsurfs = [] + height = 0 + harisfont = os.path.join ("font", "HarisComic-2.ttf") + for size, r, g, b, text in text_lines: + s = pygame.font.Font (harisfont, size).render (text, True, pygame.Color (r,g,b)) + # add 10 for spacing + height = height + s.get_height() + 10 + textsurfs.append (s) -# function to ask a question and return answer -def ask_question (surface, question, answers): - pygame.draw.rect (surface, pygame.Color (255, 255, 128), pygame.Rect (40, 40, 480-40, 480-40)) + scrwidth = surface.get_width () + scrheight = surface.get_height () + i = 0 + for s in textsurfs: + surface.blit (s, (scrwidth/2 - s.get_width()/2, scrheight/2 - height/2+ i*s.get_height()+10)) + i += 1 - put_text (surface, 60, 60, 22, (0, 0, 192), question) +# function to ask a question and return answer +def ask_question (surface, question, answers, bgscreen): - i = 1 - for answer in answers: - put_text (surface, 60, i*20+60, 22, (0, 0, 128), "%d" % i) - put_text (surface, 80, i*20+60, 22, (0, 0, 0), answer) + sel_answer = 1 while 1: + textarray = [ [ 22, 128, 0, 0, question ] ] + + i = 1 + for answer in answers: + if sel_answer == i: + r, g, b = 0, 0, 216 + else: + r, g, b = 0, 0, 0 + textarray.append ( [20, r, g, b, answer] ) + + i += 1 + + surface.blit (bgscreen, (surface.get_width()/2 - bgscreen.get_width()/2, + surface.get_height()/2 - bgscreen.get_height()/2)) + put_lines (surface, textarray) + + pygame.display.update () + for event in pygame.event.get (): if event.type == pygame.QUIT: sys.exit (0) elif event.type == pygame.KEYDOWN: - print event.key + if event.key == pygame.K_UP: + sel_answer -= 1 + if sel_answer < 1: + sel_answer = 1 + elif event.key == pygame.K_DOWN: + sel_answer += 1 + if sel_answer > len(answers): + sel_answer = len(answers) + elif event.key == pygame.K_RETURN: + return sel_answer +