import butaba
import utility
import gameobjects
+import constants
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 ()
# 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):
# 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):
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:
# 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:
# 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
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):
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