From: Harishankar Date: Wed, 5 Oct 2011 13:19:09 +0000 (+0530) Subject: Added the Conversation Interaction X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=commitdiff_plain;h=99f418a54ea2816559c23569fc880812d669644d Added the Conversation Interaction Added conversation for NPC code --- diff --git a/maingame.py b/maingame.py index 32df4cc..687db49 100644 --- a/maingame.py +++ b/maingame.py @@ -64,7 +64,8 @@ class MainGame: self.img_bulisa = pygame.image.load (os.path.join ("sprite", "bulisa.png")).convert () self.img_bulisa.set_colorkey (pygame.Color (0, 255, 0)) - # initialize NPC portraits + # initialize portraits + self.img_butaba_portrait = pygame.image.load (os.path.join ("portraits", "butaba.png")).convert () self.img_bulisa_portrait = pygame.image.load (os.path.join ("portraits", "bulisa.png")).convert () # set level data @@ -255,7 +256,7 @@ class MainGame: # interaction with npcs def interact_npc (self, npc): # interact with NPC and get the response ID - resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba, 0, 90) + resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90) # if none if resp_id is None: diff --git a/portraits/bulisa.png b/portraits/bulisa.png index f8f7a97..2563f9e 100644 Binary files a/portraits/bulisa.png and b/portraits/bulisa.png differ diff --git a/portraits/butaba.png b/portraits/butaba.png new file mode 100644 index 0000000..02355ad Binary files /dev/null and b/portraits/butaba.png differ diff --git a/utility.py b/utility.py index 93dac19..f452e7f 100644 --- a/utility.py +++ b/utility.py @@ -6,7 +6,7 @@ import xml.etree.cElementTree as ET import textwrap # function to run through an interactive dialogue and return the response ID -def dialogue_play (screen, bgscreen, npc, responder, +def dialogue_play (screen, bgscreen, npc, responder_portrait=None, marginleft=0, margintop=0, marginright=0, marginbottom=0): # first check if NPC has a dialogue at all @@ -15,6 +15,8 @@ def dialogue_play (screen, bgscreen, npc, responder, # get the conversation as a dictionary convtree = xml_to_dict (npc.dialogues[npc.currentdialog]) + import pprint + pprint.pprint (convtree) scrwidth = screen.get_width () scrheight = screen.get_height () @@ -25,25 +27,78 @@ def dialogue_play (screen, bgscreen, npc, responder, # now initiate the conversation dlg_id = "1" + reply_mode = False + current_resp = convtree[dlg_id][1][0][1] + while 1: screen.blit (bgscreen, (leftedge, topedge)) - if npc.portrait is not None: - screen.blit (npc.portrait, (leftedge, topedge)) - - # get the lines to display wrapped using textwrap module - lines = [] - textnpc = textwrap.wrap (convtree[dlg_id][0], 40) - for text in textnpc: - lines.append ((10, 0, 0, 0, text)) - - put_lines (screen, lines, - pygame.Rect(leftedge+marginleft, topedge+margintop, - bgwidth-marginleft-marginright, bgheight-margintop-marginbottom)) - pygame.display.update () - for event in pygame.event.get (): - if event.type == pygame.QUIT: - sys.exit (0) + # a dialgoue ID of 0 always exits the conversation + # return the unique response ID + if dlg_id == "0": + print current_resp + return current_resp + + # not reply mode + if reply_mode is False: + if npc.portrait is not None: + screen.blit (npc.portrait, (leftedge, topedge)) + + # get the lines to display wrapped using textwrap module + lines = [] + textnpc = textwrap.wrap (convtree[dlg_id][0], 40) + for text in textnpc: + lines.append ((10, 0, 0, 0, text)) + + put_lines (screen, lines, + pygame.Rect(leftedge+marginleft, topedge+margintop, + bgwidth-marginleft-marginright, bgheight-margintop-marginbottom)) + pygame.display.update () + + for event in pygame.event.get (): + if event.type == pygame.QUIT: + sys.exit (0) + elif event.type == pygame.KEYDOWN: + # now continue the dialog + if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: + reply_mode = True + current_resp = convtree[dlg_id][1][0][1] + sel = 0 + # reply mode + else: + if responder_portrait is not None: + screen.blit (responder_portrait, (leftedge, topedge)) + + lines = [] + for resptext, respid, nextdlgid in convtree[dlg_id][1]: + if respid == current_resp: + lines.append ((10, 255, 0, 0, resptext)) + else: + lines.append ((10, 0, 0, 0, resptext)) + + put_lines (screen, lines, + pygame.Rect (leftedge+marginleft, topedge+margintop, + bgwidth-marginleft-marginright, bgheight-margintop-marginbottom)) + + 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_UP or event.key == pygame.K_LEFT: + sel -= 1 + if sel < 0: + sel = 0 + current_resp = convtree[dlg_id][1][sel][1] + elif event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT: + sel += 1 + if sel >= len (convtree[dlg_id][1]): + sel = len(convtree[dlg_id][1]) - 1 + current_resp = convtree[dlg_id][1][sel][1] + + elif event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: + dlg_id = convtree[dlg_id][1][sel][2] + reply_mode = False # function to parse a conversation XML into a python dictionary def xml_to_dict (file): @@ -56,10 +111,13 @@ def xml_to_dict (file): convtree = dict () for dlg in conversation.findall ("dialogue"): id = dlg.get ("id") - convtree[id] = [] - convtree[id].append (dlg.find ("speech").text) + speech = dlg.find ("speech").text + responses = [] for resp in dlg.findall ("response"): - convtree[id].append ((resp.text, resp.get ("id"), resp.get ("nextdialogue"))) + responses.append ((resp.text, resp.get ("id"), resp.get ("nextdialogue"))) + + convtree[id] = (speech, responses) + return convtree