+ # 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))