+ for offset in [(TILE_SIZE - 1, TILE_SIZE - 1),
+ (-TILE_SIZE + 1, TILE_SIZE - 1),
+ (TILE_SIZE - 1, -TILE_SIZE + 1),
+ (-TILE_SIZE + 1, -TILE_SIZE + 1),
+ (0, 2 * TILE_SIZE - 2),
+ (-2 * TILE_SIZE + 2, 0),
+ (2 * TILE_SIZE - 2, 0),
+ (0, -2 * TILE_SIZE + 2),
+ (0, 0)]:
+ # Aim is to ensure a 'neighbourhood' around the player
+ # is visible if possible
+ check_point = (sprite_pos[0] + offset[0] + TILE_SIZE / 2,
+ sprite_pos[1] + offset[1] + TILE_SIZE / 2)
+ true_point = self.to_parent(*check_point)
+ if check_point[0] < 0:
+ continue
+ if check_point[1] < 0:
+ continue
+ if check_point[0] >= self.size[0]:
+ continue
+ if check_point[1] >= self.size[1]:
+ continue
+ while not self.included(true_point, 0):
+ # Scroll ourselves
+ if true_point[0] >= self.view.size[0]:
+ self.view.scroll_x += self.x_scroll_margin
+ true_point = self.to_parent(*check_point)
+ # Avoid an infinite loop that can happen we
+ # changing screens
+ if self.view.scroll_x > 0.99:
+ return
+ elif true_point[0] < 0:
+ self.view.scroll_x -= self.x_scroll_margin
+ true_point = self.to_parent(*check_point)
+ # See above
+ if self.view.scroll_x < 0.01:
+ return
+ elif true_point[1] >= self.view.size[1]:
+ self.view.scroll_y += self.y_scroll_margin
+ true_point = self.to_parent(*check_point)
+ # See above
+ if self.view.scroll_y > 0.99:
+ return
+ elif true_point[1] < 0:
+ self.view.scroll_y -= self.y_scroll_margin
+ true_point = self.to_parent(*check_point)
+ # See above
+ if self.view.scroll_y < 0.01:
+ return
+
+ def included(self, point, margin):
+ if point[0] < margin:
+ return False
+ if point[0] >= self.view.size[0] - margin:
+ return False
+ if point[1] < margin:
+ return False
+ if point[1] >= self.view.size[1] - margin:
+ return False
+ return True