From: Harishankar Date: Tue, 4 Oct 2011 11:21:41 +0000 (+0530) Subject: Began working on NPC code X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=4d50728c1454554fef2ad4841345d0c28101702d;p=butaba-adventures.git Began working on NPC code Started with the NPC code. Interaction and other details will follow. --- diff --git a/dialogues/bulisa1.dlg b/dialogues/bulisa1.dlg new file mode 100644 index 0000000..c5db25b --- /dev/null +++ b/dialogues/bulisa1.dlg @@ -0,0 +1,35 @@ + + + + Hello, Butaba. Welcome back. How are you? + I am fine + I am looking for a job + Bye! + + + Glad to know you're fine my friend. So what's up? + I am looking for a job + Bye! + + + I haven't a job for you now, but I could use your help. + What help? + Not interested. + Bye! + + + If you could draw some water from my well, I would be very grateful. + I will do so! + Oh, I am afraid not. So sorry + Bye! + + + Oh, no problem. That's all right, then. + Bye! + + + Thanks so much! There is an empty bucket somewhere in the garden. + Take that, go to the well behind the house, draw water and bring it back to me. + Right. I'll go at once. + + \ No newline at end of file diff --git a/gameobjects.py b/gameobjects.py index 190631f..2edbc5b 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -1,18 +1,15 @@ # 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): + def __init__ (self, row, col, text, image = None, can_pickup = True, use_str = "Use"): self.row = row self.col = col self.image = image self.text = text self.can_pickup = can_pickup + self.use_str = use_str # override this for interaction, i.e. when character walks into the item def interact (self): @@ -30,7 +27,7 @@ class GoldCoins (GameObject): def __init__ (self, row, col, image, value): text = "gold coins" self.value = value - GameObject.__init__ (self, row, col, text, image, False) + GameObject.__init__ (self, row, col, text, image, False, "Take") # no interaction with this object def interact (self): @@ -45,7 +42,7 @@ class HealthPotion (GameObject): # initialize def __init__ (self, row, col, image): text = "health potion" - GameObject.__init__ (self, row, col, text, image, True) + GameObject.__init__ (self, row, col, text, image, True, "Drink") # no interaction with this object def interact (self): @@ -62,7 +59,7 @@ class Chest (GameObject): self.key_id = key_id self.locked = locked self.objects = objects - GameObject.__init__ (self, row, col, text, image, False) + GameObject.__init__ (self, row, col, text, image, False, "Open") # no interaction with this object. Also solid so return False def interact (self): diff --git a/level.py b/level.py index 5aea98c..de8d76f 100644 --- a/level.py +++ b/level.py @@ -13,7 +13,8 @@ import object # Class to represent levels class Level: - def __init__ (self, bgdata, levelleft=None, levelright=None, leveltop = None, levelbottom = None, objects=[]): + def __init__ (self, bgdata, levelleft=None, levelright=None, + leveltop = None, levelbottom = None, objects=[], npcs=[]): self.background = bgdata # portals for level above, below, left or right of the character self.levelleft = levelleft @@ -23,3 +24,6 @@ class Level: # objects in the level as a list self.objects = objects + + # npcs in the level as a list + self.npcs = npcs diff --git a/maingame.py b/maingame.py index 02527db..431d44d 100644 --- a/maingame.py +++ b/maingame.py @@ -9,6 +9,7 @@ import butaba import utility import gameobjects import constants +import npcs class MainGame: @@ -56,6 +57,10 @@ class MainGame: self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert () self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0)) + # initialize NPC graphics + self.img_bulisa = pygame.image.load (os.path.join ("sprite", "bulisa.png")).convert () + self.img_bulisa.set_colorkey (pygame.Color (0, 255, 0)) + # set level data self.setup_levels () # set current level and position of our character @@ -80,6 +85,8 @@ class MainGame: potion2 = gameobjects.HealthPotion (5, 2, self.img_redpotion) potion3 = gameobjects.HealthPotion (5, 2, self.img_redpotion) + npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisa, None) + chest1.objects = [ gold50, gold25, potion2, potion3, key2, gold10 ] # create the levels @@ -89,7 +96,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, potion, chest2 ]) + objects = [ key1, potion, chest2 ], npcs = [ npc_bulisa ]) # set up the interaction between levels self.level1.levelright = self.level1e @@ -106,6 +113,8 @@ class MainGame: self.draw_level_background (self.currentlevel) # draw level objects self.draw_level_objects (self.currentlevel) + # draw the NPCs in the level + self.draw_level_npcs (self.currentlevel) # draw our character self.draw_butaba () # display the character's inventory @@ -221,8 +230,24 @@ class MainGame: notblock = self.interact_objects (level, objs) + # get npc at current location + current_npc = None + for npc in level.npcs: + if npc.row == row and npc.col == col: + current_npc = npc + break + + # npcs always block the tile. So return false if there is an NPC + # at the location + if current_npc is not None: + self.interact_npc (current_npc) + return False + return notblock + # interaction with npcs + def interact_npc (self, npc): + pass # interaction with objects def interact_objects (self, container, objs): @@ -236,7 +261,7 @@ class MainGame: notblock = False # if object can be picked up ask if obj.can_pickup is True: - ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", "Use", "Ignore"], self.img_menu) + ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", obj.use_str, "Ignore"], self.img_menu) # if the answer is "pick up" if ans == 1: self.pickup_object (container, obj) @@ -245,12 +270,20 @@ class MainGame: self.use_object (container, obj) # if it cannot be picked up, try to use it anyway else: - ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Use", "Ignore"], self.img_menu) + ans = utility.ask_question (self.screen, "Found %s." % obj.text, [obj.use_str, "Ignore"], self.img_menu) if ans == 1: self.use_object (container, obj) return notblock + # transfer an object from one container to another + # container must have an objects list + def transfer_object (self, source, obj, dest): + # remove object from source + source.objects.remove (obj) + # add object to destination + dest.objects.append (obj) + # picking up an object def pickup_object (self, container, obj): # only if object can be picked up, pick it up or use it @@ -260,9 +293,7 @@ class MainGame: 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.transfer_object (container, obj, self.butaba) self.status_message = "You picked up %s" % obj.text @@ -452,3 +483,10 @@ class MainGame: for obj in level.objects: if obj.image is not None: self.screen.blit (obj.image, (obj.col*48, obj.row*48)) + + + # Draw the NPCs in the level + def draw_level_npcs (self, level): + for npc in level.npcs: + if npc.image is not None: + self.screen.blit (npc.image, (npc.col*48, npc.row*48)) diff --git a/npcs.py b/npcs.py new file mode 100644 index 0000000..ae781da --- /dev/null +++ b/npcs.py @@ -0,0 +1,34 @@ +# class for Non-Player characters in the game + +import os.path + +# NPC base class + +class NPC: + # initalize the NPC + def __init__ (self, charname, row, col, image=None, portrait=None, dialogue_set=set(), + currentdialog=0, is_dead=False): + # name of the character + self.charname = charname + # row and column to appear (in level) + self.row = row + self.col = col + # image to represent on level + self.image = image + # portrait on dialogues + self.portrait = portrait + # dialogue set for NPC + # each dialogue in the set is a path to an XML file containing the dialogue + self.dialogue_set = dialogue_set + # index of the current dialogue which will be initiated with the main + # character + self.currentdialog = currentdialog + # whether the character is dead or alive. If dead no interaction of any + # kind can take place. + self.is_dead = is_dead + +# Bulisa is Butaba's friend +class Bulisa (NPC): + def __init__ (self, row, col, image, portrait, dialogue_set=set(), currentdialog=0): + NPC.__init__ (self, "Bulisa", row, col, image, portrait, dialogue_set, currentdialog) + diff --git a/utility.py b/utility.py index f88430c..05faab5 100644 --- a/utility.py +++ b/utility.py @@ -39,7 +39,7 @@ def ask_question (surface, question, answers, bgscreen): i = 1 for answer in answers: if sel_answer == i: - r, g, b = 0, 0, 216 + r, g, b = 255, 0, 0 else: r, g, b = 0, 0, 0 textarray.append ( [10, r, g, b, answer] )