Clamp x and y so that they fall within range of the tilemap.

def clamp_within_range(self, x, y):
        """
        Clamp x and y so that they fall within range of the tilemap.
        """
        x = int(x)
        y = int(y)

        if x < 0:
            x = 0
        if y < 0:
            y = 0
        if x > self.size_in_tiles.X:
            x = self.size_in_tiles.X
        if y > self.size_in_tiles.Y:
            y = self.size_in_tiles.Y

        return x, y