# Final touch up for the boy boundary detection mechanism

Sorry for not posting yesterday as I am terribly sick, although I am still sick today mine condition is a lot more better now. After the previous article we have basically developed a boundary detection mechanism for the player object and in this article, we will do the final touch up for that mechanism. Here are the final rules that we need to apply in order to complete the boundary detection mechanism for the boy.

1. If the boy is halfway up or down when he is insides a ladder then he can either move up or down but not side-way.
2. If the boy has touched the bottom of the ladder then he can move both side or go up the ladder again but not go downward.
3. The boy will not be able to move even further if there is an empty space below his feet to avoid him from falling down.

With the above three rules in place, we will slightly modify the boy object class by introducing a new side boundary list which will prevent the boy from going even further if the bottom of his feet is an empty space.

```class BoyObject(object):

def __init__(self):

self.list_1 = [

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

self.ladder_boundary_1 = [[9 * 64, 8 * 64],
[9 * 64, 7 * 64],
[5 * 64, 6 * 64],
[5 * 64, 5 * 64],
[2 * 64, 3 * 64],
[2 * 64, 4 * 64],
[0 * 64, 1 * 64],
[0 * 64, 2 * 64],
] # the boundary of the ladder

self.boundary_bottom_1 = [[9 * 64, 9 * 64],
[5 * 64, 7 * 64],
[2 * 64, 5 * 64],
[0 * 64, 3 * 64]]  # the bottom boundary of the ladder

self.boundary_side_1 = [[4 * 64, 6 * 64],
[1 * 64, 4 * 64],
[6 * 64, 2 * 64], ]  # the side boundary of the player

self.initial_1 = [0, 8 * 64]

def get_original_position(self, level):

if (level == 1):
return self.initial_1

def get_object_list(self, level):

if(level == 1):
return self.list_1

def get_object_boundary(self, level):

if (level == 1):

def get_object_bottom_boundary(self, level):

if (level == 1):
return self.boundary_bottom_1

def get_object_side_boundary(self, level):

if (level == 1):
return self.boundary_side_1
```

Next, we will need to check the side boundary of the boy to make sure he will not fall down beside checking the ladder boundary and the bottom boundary of the ladder.

```from BoyObject import BoyObject
from pygame import math as mt
from pygame.locals import *
from GameSprite import GameSprite

class BoySprite(object):

def __init__(self, scene, level):

self.row = 10
self.column = 10
self.scene = scene # game scene instance
self.space = 64 # image width and height
self.level = level # current game level
self.frame = 3
self.prepare_boy(self.level)
self.WIDTH = 640

def prepare_boy(self, level):
self.boy_object = BoyObject()
self.boy_object_list = self.boy_object.get_object_list(level)
self.boy_original_position = self.boy_object.get_original_position(level)
self.x = self.boy_original_position[0]
self.y = self.boy_original_position[1]
self.speed = 2
self.boy_image_sprite_right = 'Asset/bright.png'
self.boy_image_sprite_left = 'Asset/bleft.png'
self.boy_sprite = self.boy_image_sprite_right
self.left = False
self.up = False
self.boy_rect = Rect(self.frame * 64, 0, 64, 64)
self.sprite_boy = GameSprite(self.boy_sprite, self.boy_rect)
self.boy_surface = self.sprite_boy.getImage()
self.boy_bottom_boundary_list = self.boy_object.get_object_bottom_boundary(level)
self.boy_side_boundary_list = self.boy_object.get_object_side_boundary(level)
self.initialize()

# set the x and y direction for the player object

def setX(self, x):

return
self.x += x
if (self.up == True and x < 0):
self.frame = 1
self.up = False
elif(self.up == True and x > 0):
self.frame = 0
self.up = False
if(x < 0):
self.left = True
self.frame += 1
if (self.frame > 5):
self.frame = 1
self.boy_sprite = self.boy_image_sprite_left
elif(x > 0):
self.frame += 1
self.left = False
if (self.frame > 4):
self.frame = 0
self.boy_sprite = self.boy_image_sprite_right
elif(x == 0):
if(self.left == True):
self.frame = 2
self.boy_sprite = self.boy_image_sprite_left
elif(self.left == False):
self.frame = 3
self.boy_sprite = self.boy_image_sprite_right

self.boy_rect = Rect(self.frame * 64, 0, 64, 64)
self.sprite_boy = GameSprite(self.boy_sprite, self.boy_rect)
self.boy_surface = self.sprite_boy.getImage()

def setY(self, y):

self.boy_rect = Rect(self.x, self.y+y, 64, 64)
if(self.isUpDown(self.boy_rect, y) == True):
self.y += y
if(y == 0):
self.frame = 6
elif(self.up == False):
self.frame = 6
self.up = True
else:
if(y < 0):
self.frame -= 1
elif(y > 0):
self.frame += 1
if(self.frame > 10):
self.frame = 6
elif(self.frame < 6):
self.frame += 2
self.boy_sprite = self.boy_image_sprite_right
self.boy_rect = Rect(self.frame * 64, 0, 64, 64)
self.sprite_boy = GameSprite(self.boy_sprite, self.boy_rect)
self.boy_surface = self.sprite_boy.getImage()

def checkboundary(self):

if(self.x < 0):
self.x = 0
elif(self.x > self.WIDTH - self.space):
self.x = self.WIDTH - self.space
else:
self.isLeftRight()

def isUpDown(self, rect, y):

boundary_rect = Rect(boundary[0], boundary[1], 64, 64)
if ((boundary_rect.collidepoint(self.x, self.y+y) == True or boundary_rect.collidepoint(self.x, self.y+self.space+y) == True) and (self.x % self.space == 0)):
for boundary_bottom in self.boy_bottom_boundary_list:
boundary_rect = Rect(boundary_bottom[0], boundary_bottom[1], 64, 64)
if (boundary_rect.colliderect(rect) == True):
return False
return True
return False

def isLeftRight(self):

for boundary in self.boy_side_boundary_list:
boundary_rect = Rect(boundary[0], boundary[1], 64, 64)
boy_rect = Rect(self.x, self.y, 64, 64)
if (boundary_rect.colliderect(boy_rect) == True):
if(self.left == True):
self.x += self.speed
else:
self.x -= self.speed

def update(self):
self.checkboundary()
self.move_boy()

def move_boy(self):
self.draw_pos = mt.Vector2(self.x, self.y)  # the position of the image on game scene

def initialize(self):
for row in range(self.row):
for column in range(self.column):

if(self.boy_object_list[row][column] == 1):

self.draw_pos = mt.Vector2(self.x, self.y)  # the position of the image on game scene
self.scene.blit(self.boy_surface, self.draw_pos)  # draw enemy frame

def draw(self):

self.scene.blit(self.boy_surface, self.draw_pos)  # draw enemy frame
```

Before the boy can move even further the pygame program needs to make sure he can go up, down, left or right.