# Continue with the boy boundary detection mechanism

In the previous article, we have successfully made the boy climbing up the ladder but the boy will continue climbing even though there is no more ladder for him to climb. In this article, we will solve the previous problem by introducing the following rules.

1. When the boy is climbing the ladder he can only move in either the upward or the downward direction but not side-way.
2. There will be no return for the boy which means he can only move upward or downward but not going back again from where he comes from once he is insides the ladder.
3. We have introduced the bottom boundary of the ladder which means if the boy steps on the bottom boundary of that ladder he cannot move further downward.
4. We will also make sure that the x position of the boy is lining nicely with the top left corner of the ladder which is always situated on the left of the ladder block.

With all that being said we will include the bottom boundary list for the first ladder in the game scene in the boy object class.

```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.boundary_1 = [[9 * 64, 8 * 64],
[9 * 64, 7 * 64]] # the boundary of the ladder

self.boundary_bottom = [[9 * 64, 9 * 64]]  # the bottom boundary of the ladder

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):
return self.boundary_1

def get_object_bottom_boundary(self, level):

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

Next we will link up the above lists with the boy sprite class.

```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
self.y = self.boy_original_position
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_boundary_list = self.boy_object.get_object_boundary(level)
self.boy_bottom_boundary_list = self.boy_object.get_object_bottom_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

def isUpDown(self, rect, y):

for boundary in self.boy_boundary_list:
boundary_rect = Rect(boundary, boundary, 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, boundary_bottom, 64, 64)
if (boundary_rect.colliderect(rect) == True):
return False
return True
return False

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

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

```

By applying the above-mentioned rules to the boy sprite class we have now completed the boy’s boundary detection mechanism and the rest is just like homework where we will fill in the blank by completing the entire boy boundary’s list inside the boy object class in the next chapter. Actually, it is possible to allow the boy to move in the left or right direction once he is insides the ladder but there is no reason to do that because once he is insides the ladder his only target is to move to the next floor and then win the game!

Hope you like this article and don’t forget to share the above tweet.