X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=blobdiff_plain;f=maingame.py;h=05e8916ad103d9397a8ede0b1216b9a4f10064d9;hp=c68e2151cb4a86c1cca2844d1a723e8d65483a56;hb=6552a4a3522d0b48177a9c2ebedda92b824874f7;hpb=4b6e952b476861c8979dc395781964099285a4c6 diff --git a/maingame.py b/maingame.py index c68e215..05e8916 100644 --- a/maingame.py +++ b/maingame.py @@ -2,11 +2,13 @@ import pygame import sys import random import os.path +import cPickle import level import butaba import utility import gameobjects +import constants class MainGame: @@ -23,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 () @@ -35,9 +39,12 @@ 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)) # initialize player graphics self.img_butabafront = pygame.image.load (os.path.join ("sprite", "butaba-front.png")).convert () @@ -61,15 +68,31 @@ class MainGame: # 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.level1w = level.Level (cPickle.load (file ("levels/level1w.dat"))) + + self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")), + objects = [ key1, key2, potion, chest2 ]) - 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 self.level1.levelright = self.level1e + self.level1.levelleft = self.level1w self.level1e.levelleft = self.level1 + self.level1w.levelright = self.level1 def main_loop (self): # main game loop @@ -93,6 +116,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,6 +126,24 @@ 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): + # 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 def move_butaba_up (self): # clear any status messages @@ -187,27 +229,78 @@ 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): - # 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) >= 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, 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 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: + 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 + container.remove (obj) def move_butaba_left (self): # clear any status messages @@ -287,29 +380,29 @@ class MainGame: # 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") + utility.put_text (self.screen, 490, 170, 16, (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: @@ -320,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