Started work on container objects like Chests etc.
authorHarishankar <v.harishankar@gmail.com>
Mon, 3 Oct 2011 05:06:37 +0000 (10:36 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 3 Oct 2011 05:06:37 +0000 (10:36 +0530)
Started work on how to manage the container object like chests
etc. So far displaying the contents is implemented.

background/chestcontent.png [new file with mode: 0644]
background/tileset.png
butaba.py
constants.py [new file with mode: 0644]
gameobjects.py
level.py
maingame.py
objects/gold-coins.png
sprite/bulisa.png [new file with mode: 0644]
utility.py

diff --git a/background/chestcontent.png b/background/chestcontent.png
new file mode 100644 (file)
index 0000000..f5eef18
Binary files /dev/null and b/background/chestcontent.png differ
index 686f4da..7b8d16a 100644 (file)
Binary files a/background/tileset.png and b/background/tileset.png differ
index 99c8a0a..7b93980 100644 (file)
--- a/butaba.py
+++ b/butaba.py
@@ -10,7 +10,7 @@ class Butaba:
        MAXHEALTH = 100
 
        # initialize our character
-       def __init__ (self, startrow, startcol, position=LEFT, health=100, magic=10, experience=10, strength=10, gold=0, inventory = []):
+       def __init__ (self, startrow, startcol, position=LEFT, health=100, magic=1, experience=1, strength=1, gold=0, inventory = []):
                self.position = position
                self.row = startrow
                self.col = startcol
@@ -18,6 +18,6 @@ class Butaba:
                self.experience = experience
                self.strength = strength
                self.health = health
-               self.inventory = inventory
+               self.objects = inventory
                self.gold = gold
 
diff --git a/constants.py b/constants.py
new file mode 100644 (file)
index 0000000..bc9e737
--- /dev/null
@@ -0,0 +1,11 @@
+# constants for in-game use
+
+# key ids and lock ids
+KEY_CHEST1 = 1000
+KEY_CHEST2 = 1001
+
+
+# maximum experience points reached by achieving certain actions
+
+# maximum knowledge to be gained by unlocking chests
+KNOWLEDGEMAX_CHEST_UNLOCK = 2
\ No newline at end of file
index b4992a3..190631f 100644 (file)
@@ -58,10 +58,10 @@ class HealthPotion (GameObject):
                        butaba.health = butaba.MAXHEALTH
 
 class Chest (GameObject):
-       def __init__ (self, row, col, text, image, key_id, locked = False, items = []):
+       def __init__ (self, row, col, text, image, key_id, locked = False, objects = []):
                self.key_id = key_id
                self.locked = locked
-               self.items = items
+               self.objects = objects
                GameObject.__init__ (self, row, col, text, image, False)
 
        # no interaction with this object. Also solid so return False
index 4ba77a9..5aea98c 100644 (file)
--- a/level.py
+++ b/level.py
@@ -10,9 +10,6 @@ import object
 # second item is tile col in the tilest
 # third item defines whether solid or not (0 or 1)
 
-# constants for in-game use
-KEY_CHEST1 = 1000
-KEY_ROOM1 = 1001
 
 # Class to represent levels
 class Level:
index 75702b8..05e8916 100644 (file)
@@ -8,6 +8,7 @@ import level
 import butaba
 import utility
 import gameobjects
+import constants
 
 class MainGame:
 
@@ -24,6 +25,8 @@ 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 ()
+
 
                # initialize object graphics
                self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert ()
@@ -61,26 +64,34 @@ class MainGame:
                # set the status message
                self.status_message = "Game started"
 
-               self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT, 75)
+               self.butaba = butaba.Butaba (5,0, butaba.Butaba.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)
+               key1 = gameobjects.Key (5, 3, "a chest key", self.img_key2, constants.KEY_CHEST1)
+               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)
+
+               chest1.objects = [ gold50, gold75 ]
+
+               # create the levels
+               self.level1 = level.Level (cPickle.load (file ("levels/level1.dat")),
+                               objects = [ chest1 ] )
 
-               self.level1 = level.Level (cPickle.load (file ("levels/level1.dat")))
                self.level1w = level.Level (cPickle.load (file ("levels/level1w.dat")))
+
                self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")),
-                                                                       objects = [ gameobjects.Key (5, 3, "a chest key", self.img_key2, level.KEY_CHEST1),
-                                                                                               gameobjects.Key (5, 3, "a room key", self.img_key, level.KEY_ROOM1),
-                                                                                               gameobjects.HealthPotion (5, 2, self.img_redpotion),
-                                                                                               gameobjects.Chest (2, 5, "chest", self.img_chest, level.KEY_CHEST1, True),
-                                                                                               gameobjects.GoldCoins (6, 2, self.img_goldcoins, 50)
-                                                                                       ]
-                                                                                       )
+                               objects = [ key1, key2, potion, chest2 ])
+
+               # set up the interaction between levels
                self.level1.levelright = self.level1e
                self.level1.levelleft = self.level1w
-
                self.level1e.levelleft = self.level1
-
                self.level1w.levelright = self.level1
 
        def main_loop (self):
@@ -131,7 +142,7 @@ class MainGame:
                        # look for a health potion
                        for item in self.butaba.inventory:
                                if isinstance (item, gameobjects.HealthPotion) is True:
-                                       self.use_object (item)
+                                       self.use_object (self.butaba, item)
                                        break
 
        def move_butaba_up (self):
@@ -218,43 +229,42 @@ class MainGame:
                        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", "Use", "Ignore"], self.img_menu)
+                               # if the answer is "pick up"
+                               if ans == 1:
+                                       self.pickup_object (self.currentlevel, obj)
+                               elif ans == 2:
+                                       # use the object according to its type
+                                       self.use_object (self.currentlevel, obj)
+                       # if it cannot be picked up, try to use it anyway
+                       else:
+                               self.use_object (self.currentlevel, obj)
 
                return notblock
 
        # picking up an object
-       def pickup_object (self, obj):
+       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, ["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:
-                                       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
-                       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)
+                       # check if the inventory is full
+                       if len (self.butaba.objects) >= butaba.Butaba.MAXITEMS:
+                               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.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, obj):
+       def use_object (self, container, obj):
                # if the object is a health potion
                if isinstance (obj, gameobjects.HealthPotion) is True:
                        obj.use (self.butaba)
-                       if obj in self.currentlevel.objects:
-                               self.currentlevel.objects.remove (obj)
-                       elif obj in self.butaba.inventory:
-                               self.butaba.inventory.remove (obj)
+                       container.objects.remove (obj)
                        self.status_message = "You gained health"
                # if the object is a chest
                elif isinstance (obj, gameobjects.Chest) is True:
@@ -263,7 +273,7 @@ class MainGame:
                                # 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:
+                               for invobj in self.butaba.objects:
                                        fittedkey = obj.use (invobj)
                                        # if a key fits
                                        if fittedkey is not None:
@@ -274,20 +284,23 @@ class MainGame:
                                # chest successfully unlocked
                                else:
                                        self.status_message = "You unlocked the %s" % obj.text
-                                       # remove the key from inventory
-                                       self.butaba.inventory.remove (fittedkey)
+                                       # 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:
-                               pass
+                               utility.display_container_contents (self.screen, obj, self.img_chestbg, 30)
+
                # 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
-                       if obj in self.currentlevel.objects:
-                               self.currentlevel.objects.remove (obj)
-                       elif obj in self.butaba.inventory:
-                               self.butaba.inventory.remove (obj)
+                       container.remove (obj)
 
        def move_butaba_left (self):
                # clear any status messages
@@ -382,7 +395,7 @@ class MainGame:
                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, 16, (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):
@@ -400,7 +413,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
index a6848a9..d744c5d 100644 (file)
Binary files a/objects/gold-coins.png and b/objects/gold-coins.png differ
diff --git a/sprite/bulisa.png b/sprite/bulisa.png
new file mode 100644 (file)
index 0000000..9570fea
Binary files /dev/null and b/sprite/bulisa.png differ
index 5ef5428..bc760f3 100644 (file)
@@ -67,3 +67,43 @@ def ask_question (surface, question, answers, bgscreen):
                                elif event.key == pygame.K_RETURN:
                                        return sel_answer
 
+# 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):
+
+       # get the number of items
+       numitems = len (obj.objects)
+       # number of rows
+       num_rows = (bgimage.get_height () - edgewidth*2) / 48
+       # number of cols
+       num_cols = (bgimage.get_width () - edgewidth*2) / 48
+
+       objposx = surface.get_width()/2 - bgimage.get_width()/2
+       objposy = surface.get_height()/2 - bgimage.get_height()/2
+
+       while 1:
+               # display the background for the container
+               surface.blit (bgimage, (objposx, objposy))
+
+               # display each item in the container
+               i = 0
+               j = 0
+
+               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
+
+               pygame.display.update ()
+               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