Implemented NPC movement (random)
authorHarishankar <v.harishankar@gmail.com>
Fri, 7 Oct 2011 10:45:52 +0000 (16:15 +0530)
committerHarishankar <v.harishankar@gmail.com>
Fri, 7 Oct 2011 10:45:52 +0000 (16:15 +0530)
Added random movement for NPCs in level based on a limiting
area. NPC cannot move on solid tiles, or tiles with objects,
where other NPCs are standing and also where Butaba is
standing

12 files changed:
butaba.py
constants.py
dialogues/bulisa4.dlg
gameobjects.py
level.py
maingame.py
npcs.py
sprite/bulisa-back.png [new file with mode: 0644]
sprite/bulisa-front.png [new file with mode: 0644]
sprite/bulisa-left.png [new file with mode: 0644]
sprite/bulisa-right.png [new file with mode: 0644]
sprite/bulisa.png [deleted file]

index ca44883..d9237ca 100644 (file)
--- a/butaba.py
+++ b/butaba.py
@@ -1,17 +1,18 @@
 # Main player Butaba class
 
+import constants
+
 class Butaba:
-       # Position definitions
-       LEFT = 0
-       RIGHT = 1
-       FRONT = 2
-       BACK = 3
-       MAXITEMS = 8
-       MAXHEALTH = 100
 
        # initialize our character
-       def __init__ (self, startrow, startcol, position=LEFT, health=100, magic=1, experience=1, strength=1, gold=0, inventory = []):
+       def __init__ (self, startrow, startcol, imageleft, imageright, imagefront, imageback, portrait,
+                       position=constants.LEFT, health=100, magic=1, experience=1, strength=1, gold=0, inventory = []):
                self.position = position
+               self.imageleft = imageleft
+               self.imageright = imageright
+               self.imagefront = imagefront
+               self.imageback = imageback
+               self.portrait = portrait
                self.row = startrow
                self.col = startcol
                self.magic = magic
index 18111c6..a37f3d3 100644 (file)
@@ -1,5 +1,16 @@
 # constants for in-game use
 
+# butaba constants
+MAXITEMS = 8
+MAXHEALTH = 100
+
+
+# describing the orientation of characters
+LEFT = 0
+RIGHT = 2
+FRONT = 1
+BACK = 3
+
 # key ids and lock ids
 KEY_CHEST1 = 1000
 KEY_CHEST2 = 1001
index 2674783..d439c59 100644 (file)
@@ -2,6 +2,12 @@
 <conversation>
   <dialogue id="1">
     <speech>Ah, I see you're back with the water! Thanks so much, Butaba.</speech>
-    <response id="19" nextdialogue="0">No problem! My pleasure...</response>
+    <response id="19" nextdialogue="2">No problem! My pleasure...</response>
+  </dialogue>
+  <dialogue id="2">
+    <speech>I am very grateful for your assistance. Here is the key 
+to my chest which you will find in the front room. There is a bit of money there. Please accept 
+it as a token of my appreciation. I hope you don't feel offended by this offer!</speech>
+    <response id="20" nextdialogue="0">Oh, not at all! Thanks so much!</response>
   </dialogue>
 </conversation>
\ No newline at end of file
index c9c945b..bc73fad 100644 (file)
@@ -90,8 +90,8 @@ class HealthPotion (GameObject):
        # using the potion
        def use (self, butaba):
                butaba.health += 25
-               if butaba.health > butaba.MAXHEALTH:
-                       butaba.health = butaba.MAXHEALTH
+               if butaba.health > constants.MAXHEALTH:
+                       butaba.health = constants.MAXHEALTH
 
 class Chest (GameObject):
        def __init__ (self, row, col, text, image, key_id, locked = False, objects = []):
index de8d76f..e634b00 100644 (file)
--- a/level.py
+++ b/level.py
@@ -1,7 +1,5 @@
 # level.py - level data and class
 
-import object
-
 # Background level data
 # A level is a list of list of tuples. Level is a 10x10 room of 48 pixel images
 #
index 9ba5ea2..4529e9a 100644 (file)
@@ -16,7 +16,10 @@ class MainGame:
 
        # initialize the game
        def __init__ (self):
+               random.seed ()
                pygame.init ()
+
+               self.clock = pygame.time.Clock ()
                self.screen  = pygame.display.set_mode ((720, 512))
                pygame.display.set_caption ("The Adventures of Butaba")
 
@@ -64,8 +67,17 @@ class MainGame:
                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))
+               self.img_bulisafront = pygame.image.load (os.path.join ("sprite", "bulisa-front.png")).convert ()
+               self.img_bulisafront.set_colorkey (pygame.Color (0, 255, 0))
+
+               self.img_bulisaback = pygame.image.load (os.path.join ("sprite", "bulisa-back.png")).convert ()
+               self.img_bulisaback.set_colorkey (pygame.Color (0, 255, 0))
+
+               self.img_bulisaleft = pygame.image.load (os.path.join ("sprite", "bulisa-left.png")).convert ()
+               self.img_bulisaleft.set_colorkey (pygame.Color (0, 255, 0))
+
+               self.img_bulisaright = pygame.image.load (os.path.join ("sprite", "bulisa-right.png")).convert ()
+               self.img_bulisaright.set_colorkey (pygame.Color (0, 255, 0))
 
                # initialize portraits
                self.img_butaba_portrait = pygame.image.load (os.path.join ("portraits", "butaba.png")).convert ()
@@ -79,14 +91,15 @@ class MainGame:
                # set the status message
                self.status_message = "Game started"
 
-               self.butaba = butaba.Butaba (5,0, butaba.Butaba.RIGHT)
+               self.butaba = butaba.Butaba (5,0, self.img_butabaleft,
+                                                                       self.img_butabaright, self.img_butabafront,
+                                                                       self.img_butababack, self.img_butaba_portrait, constants.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)
@@ -99,11 +112,14 @@ class MainGame:
                well3 = gameobjects.Well (4, 8)
                well4 = gameobjects.Well (5, 8)
 
-               npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisa, self.img_bulisa_portrait,
-                                                                               [ os.path.join ("dialogues", "bulisa1.dlg"),
-                                                                                 os.path.join ("dialogues", "bulisa2.dlg"),
-                                                                                 os.path.join ("dialogues", "bulisa3.dlg"),
-                                                                                 os.path.join ("dialogues", "bulisa4.dlg") ] )
+               npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisaleft, self.img_bulisaright,
+                                                                       self.img_bulisafront, self.img_bulisaback,
+                                                                       self.img_bulisa_portrait, constants.FRONT,
+                                                                       (2, 2, 2, 2),
+                                                                       [ os.path.join ("dialogues", "bulisa1.dlg"),
+                                                                         os.path.join ("dialogues", "bulisa2.dlg"),
+                                                                         os.path.join ("dialogues", "bulisa3.dlg"),
+                                                                         os.path.join ("dialogues", "bulisa4.dlg") ] )
 
                chest1.objects = [ gold50, gold25, key2, gold10 ]
 
@@ -114,7 +130,7 @@ class MainGame:
                self.level1w = level.Level (cPickle.load (file (os.path.join ("levels", "level1w.dat"))))
 
                self.level1e = level.Level (cPickle.load (file (os.path.join ("levels", "level1e.dat"))),
-                               objects = [ key1, potion, chest2 ], npcs = [ npc_bulisa ])
+                               objects = [ potion, chest2 ], npcs = [ npc_bulisa ])
 
                self.level1ee = level.Level (cPickle.load (file (os.path.join ("levels", "level1ee.dat"))),
                                objects = [ well1, well2, well3, well4 ])
@@ -135,6 +151,7 @@ class MainGame:
        def main_loop (self):
                # main game loop
                while 1:
+                       self.clock.tick (25)
                        # clear screen
                        self.screen.fill (pygame.Color (0,0,0))
                        # draw the level
@@ -185,8 +202,8 @@ class MainGame:
                # clear any status messages
                self.status_message = None
                # first if butaba is not facing up, make him face up
-               if self.butaba.position <> butaba.Butaba.BACK:
-                       self.butaba.position = butaba.Butaba.BACK
+               if self.butaba.position <> constants.BACK:
+                       self.butaba.position = constants.BACK
                        return
 
                # if butaba is trying to move off the top of the screen
@@ -216,8 +233,8 @@ class MainGame:
                # clear any status messages
                self.status_message = None
                # first if butaba is not facing forward, make him face forward/down
-               if self.butaba.position <> butaba.Butaba.FRONT:
-                       self.butaba.position = butaba.Butaba.FRONT
+               if self.butaba.position <> constants.FRONT:
+                       self.butaba.position = constants.FRONT
                        return
 
                # if butaba is trying to move off the bottom of the screen
@@ -277,7 +294,14 @@ class MainGame:
        def interact_npc (self, npc):
                # interact with NPC and get the response ID
                # if the NPC is Bulisa
+
+               # if the NPC is dead cannot talk with the NPC
+               if npc.is_dead is True:
+                       self.status_message = "%s is dead! RIP..." % npc.charname
+                       return
+
                if isinstance (npc, npcs.Bulisa):
+                       # interact
                        self.interact_npc_bulisa (npc)
 
        # interact with NPC Bulisa
@@ -290,12 +314,12 @@ class MainGame:
                        # set the current dialogue
                        npc.currentdialog = 0
                        # get the response ID
-                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
                if (gamestate.mission_bulisa_water_from_well_refused is True and
                                gamestate.mission_bulisa_water_from_well is False):
                        # set the current dialog
                        npc.currentdialog = 2
-                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
                # mission accepted but not completed - check if completed and set value
                # accordingly
                elif (gamestate.mission_bulisa_water_from_well is True
@@ -305,6 +329,9 @@ class MainGame:
                                        if invobj.liquid == "water":
                                                gamestate.mission_bulisa_water_from_well_complete = True
                                                self.butaba.objects.remove (invobj)
+                                               key1 = gameobjects.Key (5, 3, "a chest key", self.img_key2, constants.KEY_CHEST1)
+                                               self.butaba.objects.append (key1)
+
                                                break
                        # water mission is not completed yet
                        if gamestate.mission_bulisa_water_from_well_complete is False:
@@ -313,7 +340,7 @@ class MainGame:
                                npc.currentdialog = 3
 
                        # get the response ID
-                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.butaba.portrait, 0, 90)
 
                # if response ID is 12, then drawing water from well mission is refused
                if resp_id == "12" or resp_id == "18":
@@ -365,7 +392,7 @@ class MainGame:
                # only if object can be picked up, pick it up or use it
                if obj.can_pickup is True:
                        # check if the inventory is full
-                       if len (self.butaba.objects) >= butaba.Butaba.MAXITEMS:
+                       if len (self.butaba.objects) >= constants.MAXITEMS:
                                self.status_message = "Cannot pick up item. Inventory full"
                        else:
                                # add item to inventory
@@ -378,7 +405,7 @@ class MainGame:
        def use_object (self, container, obj):
                # if the object is a health potion
                if isinstance (obj, gameobjects.HealthPotion) is True:
-                       if self.butaba.health < butaba.Butaba.MAXHEALTH:
+                       if self.butaba.health < constants.MAXHEALTH:
                                obj.use (self.butaba)
                                container.objects.remove (obj)
                                self.status_message = "You gained health"
@@ -453,8 +480,8 @@ class MainGame:
                self.status_message = None
 
                # first if Butaba is not facing left, make him face left
-               if self.butaba.position <> butaba.Butaba.LEFT:
-                       self.butaba.position = butaba.Butaba.LEFT
+               if self.butaba.position <> constants.LEFT:
+                       self.butaba.position = constants.LEFT
                        return
 
                # if butaba is trying to move off the left edge
@@ -485,8 +512,8 @@ class MainGame:
                self.status_message = None
 
                # First if Butaba is not facing right make him face right
-               if self.butaba.position <> butaba.Butaba.RIGHT:
-                       self.butaba.position = butaba.Butaba.RIGHT
+               if self.butaba.position <> constants.RIGHT:
+                       self.butaba.position = constants.RIGHT
                        return
 
                # if butaba is trying to move off the right edge
@@ -513,14 +540,14 @@ class MainGame:
                                self.butaba.col += 1
 
        def draw_butaba (self):
-               if self.butaba.position == butaba.Butaba.FRONT:
-                       self.screen.blit (self.img_butabafront, (self.butaba.col*48, self.butaba.row*48))
-               elif self.butaba.position == butaba.Butaba.BACK:
-                       self.screen.blit (self.img_butababack, (self.butaba.col*48, self.butaba.row*48))
-               elif self.butaba.position == butaba.Butaba.LEFT:
-                       self.screen.blit (self.img_butabaleft, (self.butaba.col*48, self.butaba.row*48))
-               elif self.butaba.position == butaba.Butaba.RIGHT:
-                       self.screen.blit (self.img_butabaright, (self.butaba.col*48, self.butaba.row*48))
+               if self.butaba.position == constants.FRONT:
+                       self.screen.blit (self.butaba.imagefront, (self.butaba.col*48, self.butaba.row*48))
+               elif self.butaba.position == constants.BACK:
+                       self.screen.blit (self.butaba.imageback, (self.butaba.col*48, self.butaba.row*48))
+               elif self.butaba.position == constants.LEFT:
+                       self.screen.blit (self.butaba.imageleft, (self.butaba.col*48, self.butaba.row*48))
+               elif self.butaba.position == constants.RIGHT:
+                       self.screen.blit (self.butaba.imageright, (self.butaba.col*48, self.butaba.row*48))
 
 
        # Draw the status infodisplay
@@ -549,7 +576,7 @@ class MainGame:
                r = 1
                c = 1
                utility.put_text (self.screen, 490, 170, 16, (255,255 , 0), "Inventory")
-               for i in range (butaba.Butaba.MAXITEMS):
+               for i in range (constants.MAXITEMS):
                        self.screen.blit (self.img_inventory, (440+c*54, 150+r*54))
                        if c % 4 == 0:
                                r += 1
@@ -590,5 +617,113 @@ class MainGame:
        # 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))
+                       # if npc is not dead then move the NPC randomly depending on their
+                       # movement area
+                       if npc.is_dead is False:
+                               # move this turn?
+                               if random.randint (1, 500) < npc.movement_speed:
+                                       # whether to change direction?
+                                       if random.randint (1, 100) < 25:
+                                               npc.position = random.randint (0, 3)
+                                       else:
+                                               # if left movement
+                                               if npc.position == constants.LEFT:
+                                                       # cannot move beyond level and cannot move beyond the
+                                                       # left limit area
+                                                       if npc.col > 0 and npc.col > npc.initcol - npc.leftlimit:
+                                                               # check if there is any background obstacle
+                                                               if self.check_background_obstacle (level, npc.row, npc.col - 1) is False:
+                                                                       # check if there are any objects
+                                                                       objblock = False
+                                                                       npcblock = False
+                                                                       for lobj in level.objects:
+                                                                               if lobj.row == npc.row and lobj.col == npc.col - 1:
+                                                                                       objblock = True
+                                                                                       break
+                                                                       # check if there any any npcs blocking
+                                                                       for lnpc in level.npcs:
+                                                                               if lnpc.row == npc.row and lnpc.col == npc.col - 1:
+                                                                                       npcblock = True
+                                                                                       break
+                                                                       if objblock is False and npcblock is False:
+                                                                               # if butaba is not blocking
+                                                                               if self.butaba.row <> npc.row or self.butaba.col <> npc.col - 1:
+                                                                                       npc.col -= 1
+                                               elif npc.position == constants.RIGHT:
+                                                       # cannot move beyond level and cannot move beyond the
+                                                       # right limit area
+                                                       if npc.col < 9 and npc.col < npc.initcol + npc.rightlimit:
+                                                               # check if there is any background obstacle
+                                                               if self.check_background_obstacle (level, npc.row, npc.col + 1) is False:
+                                                                       # check if there are any objects
+                                                                       objblock = False
+                                                                       npcblock = False
+                                                                       for lobj in level.objects:
+                                                                               if lobj.row == npc.row and lobj.col == npc.col + 1:
+                                                                                       objblock = True
+                                                                                       break
+                                                                       # check if there any any npcs blocking
+                                                                       for lnpc in level.npcs:
+                                                                               if lnpc.row == npc.row and lnpc.col == npc.col + 1:
+                                                                                       npcblock = True
+                                                                                       break
+                                                                       if objblock is False and npcblock is False:
+                                                                               # if butaba is not blocking
+                                                                               if self.butaba.row <> npc.row or self.butaba.col <> npc.col + 1:
+                                                                                       npc.col += 1
+                                               elif npc.position == constants.FRONT:
+                                                       # cannot move beyond level and cannot move beyond the
+                                                       # lower bottom limit area
+                                                       if npc.row < 9 and npc.row < npc.initrow + npc.bottomlimit:
+                                                               # check if there is any background obstacle
+                                                               if self.check_background_obstacle (level, npc.row + 1, npc.col) is False:
+                                                                       # check if there are any objects
+                                                                       objblock = False
+                                                                       npcblock = False
+                                                                       for lobj in level.objects:
+                                                                               if lobj.row == npc.row + 1 and lobj.col == npc.col:
+                                                                                       objblock = True
+                                                                                       break
+                                                                       # check if there any any npcs blocking
+                                                                       for lnpc in level.npcs:
+                                                                               if lnpc.row == npc.row + 1 and lnpc.col == npc.col:
+                                                                                       npcblock = True
+                                                                                       break
+                                                                       if objblock is False and npcblock is False:
+                                                                               # if butaba is not blocking
+                                                                               if self.butaba.row <> npc.row + 1 or self.butaba.col <> npc.col:
+                                                                                       npc.row += 1
+                                               elif npc.position == constants.BACK:
+                                                       # cannot move beyond level and cannot move beyond the
+                                                       # top upper limit area
+                                                       if npc.row > 0 and npc.row > npc.initrow - npc.toplimit:
+                                                               # check if there is any background obstacle
+                                                               if self.check_background_obstacle (level, npc.row - 1, npc.col) is False:
+                                                                       # check if there are any objects
+                                                                       objblock = False
+                                                                       npcblock = False
+                                                                       for lobj in level.objects:
+                                                                               if lobj.row == npc.row - 1 and lobj.col == npc.col:
+                                                                                       objblock = True
+                                                                                       break
+                                                                       # check if there any any npcs blocking
+                                                                       for lnpc in level.npcs:
+                                                                               if lnpc.row == npc.row - 1 and lnpc.col == npc.col:
+                                                                                       npcblock = True
+                                                                                       break
+                                                                       # if no object is blocking
+                                                                       if objblock is False and npcblock is False:
+                                                                               # if butaba is not blocking
+                                                                               if self.butaba.row <> npc.row - 1 or self.butaba.col <> npc.col:
+                                                                                       npc.row -= 1
+
+                       if npc.position == constants.FRONT:
+                               img = npc.imagefront
+                       elif npc.position == constants.BACK:
+                               img = npc.imageback
+                       elif npc.position == constants.LEFT:
+                               img = npc.imageleft
+                       else:
+                               img = npc.imageright
+
+                       self.screen.blit (img, (npc.col*48, npc.row*48))
diff --git a/npcs.py b/npcs.py
index d368833..26732c2 100644 (file)
--- a/npcs.py
+++ b/npcs.py
@@ -6,17 +6,37 @@ import os.path
 
 class NPC:
        # initalize the NPC
-       def __init__ (self, charname, row, col, image=None, portrait=None, dialogues=[],
-                               currentdialog=0, is_dead=False):
+       def __init__ (self, charname, row, col, imageleft, imageright, imagefront,
+                               imageback, portrait, position, movement_area=(0,0,0,0), movement_speed = 10,
+                               dialogues=[], 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
+               # initial row and col - to track the movement area
+               self.initrow = row
+               self.initcol = col
+               # images to represent on level
+               self.imageleft = imageleft
+               self.imageright = imageright
+               self.imagefront = imagefront
+               self.imageback = imageback
+
                # portrait on dialogues
                self.portrait = portrait
+
+               # movement area limits (left, right top, and bottom) in which the NPC can
+               # move around in the level randomly.
+               self.leftlimit, self.rightlimit, self.toplimit, self.bottomlimit = movement_area
+
+               # chance of movement (speed) - that is chance of movement in a turn out of 500 -lower
+               # the value lower the speed
+               self.movement_speed = movement_speed
+
+               # position of the character
+               self.position = position
+
                # dialogue set for NPC
                # each dialogue in the set is a path to an XML file containing the dialogue
                self.dialogues = dialogues
@@ -29,6 +49,9 @@ class NPC:
 
 # Bulisa is Butaba's friend
 class Bulisa (NPC):
-       def __init__ (self, row, col, image, portrait, dialogues=[], currentdialog=0):
-               NPC.__init__ (self, "Bulisa", row, col, image, portrait, dialogues, currentdialog)
+       def __init__ (self, row, col, imageleft, imageright, imagefront,
+                               imageback, portrait, position, movement_area=(0,0,0,0),
+                               dialogues=[], currentdialog=0):
+               NPC.__init__ (self, "Bulisa", row, col, imageleft, imageright, imagefront,
+                               imageback, portrait, position, movement_area, 20, dialogues, currentdialog)
 
diff --git a/sprite/bulisa-back.png b/sprite/bulisa-back.png
new file mode 100644 (file)
index 0000000..e9f8826
Binary files /dev/null and b/sprite/bulisa-back.png differ
diff --git a/sprite/bulisa-front.png b/sprite/bulisa-front.png
new file mode 100644 (file)
index 0000000..21fae0d
Binary files /dev/null and b/sprite/bulisa-front.png differ
diff --git a/sprite/bulisa-left.png b/sprite/bulisa-left.png
new file mode 100644 (file)
index 0000000..e100ebf
Binary files /dev/null and b/sprite/bulisa-left.png differ
diff --git a/sprite/bulisa-right.png b/sprite/bulisa-right.png
new file mode 100644 (file)
index 0000000..d1310f8
Binary files /dev/null and b/sprite/bulisa-right.png differ
diff --git a/sprite/bulisa.png b/sprite/bulisa.png
deleted file mode 100644 (file)
index 9570fea..0000000
Binary files a/sprite/bulisa.png and /dev/null differ