Detect the player’s boundary

In this article, we will start to create the boundary detection mechanism which can be used to help the boy moving around the canvas. We will go slowly where this topic will take a few chapters to complete. In this chapter, we will focus on below issues.

  1. The boy will not be able to move past the horizontal boundary of either 0 or 576 pixels which is the physical boundary for the boy sprite.
  2. The boy will be able to move upward or downward but not side-way when he is inside the ladder.
  3. When the boy is inside the ladder his horizontal movement will be disabled so the boy can either move up or down only, we will enable the boy’s horizontal movement again when the boy reaches the top of the ladder in the next chapter.

In order to achieve the above outcomes, we will edit two files. The boy object file will now contain the ladder boundary which will be used to detect whether the boy is inside the ladder or not. Finally, we will edit the boy sprite to link up those mechanisms that we are talking about.

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.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
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.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.initialize()

    # set the x and y direction for the player object

    def setX(self, x):
        if(self.up == False):
            self.x += x
            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):
        if(self.up == True):
            self.y += y
            if(y == 0 or self.up == False):
                self.frame = 6
            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:
            for boundary in self.boy_boundary_list:
                boundary_rect = Rect(boundary[0], boundary[1], 64, 64)
                if(boundary_rect.collidepoint(self.x, self.y) == True):
                    self.up = True

    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

If you run the program again this is what you will see.

Alright, in the next chapter we will continue to modify the boundary detection mechanism for the boy.

Leave a Reply

Your email address will not be published. Required fields are marked *