- # Player position in viewpoint coordinates
- check_point = (sprite_pos[0] + TILE_SIZE / 2,
- sprite_pos[1] + TILE_SIZE / 2)
- true_point = self.to_parent(*check_point)
- if not self.included(true_point):
- # Scroll ourselves
- while true_point[0] >= self.view.size[0] - TILE_SIZE:
- self.pos = (self.pos[0] - 1.5 * TILE_SIZE, self.pos[1])
- true_point = self.to_parent(*check_point)
- while true_point[0] < TILE_SIZE:
- self.pos = (self.pos[0] + 1.5 * TILE_SIZE, self.pos[1])
- true_point = self.to_parent(*check_point)
- while true_point[1] >= self.view.size[1] - TILE_SIZE:
- self.pos = (self.pos[0], self.pos[1] - 1.5 * TILE_SIZE)
- true_point = self.to_parent(*check_point)
- while true_point[1] < TILE_SIZE:
- self.pos = (self.pos[0], self.pos[1] + 1.5 * TILE_SIZE)
- true_point = self.to_parent(*check_point)
-
- def included(self, point):
- if point[0] < TILE_SIZE:
+ 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)
+ #print '-x', self.view.scroll_x, self.view.scroll_y
+ elif true_point[0] < 0:
+ self.view.scroll_x -= self.x_scroll_margin
+ true_point = self.to_parent(*check_point)
+ #print '+x', self.view.scroll_x, self.view.scroll_y
+ elif true_point[1] >= self.view.size[1]:
+ self.view.scroll_y += self.y_scroll_margin
+ true_point = self.to_parent(*check_point)
+ #print '+y', self.view.scroll_x, self.view.scroll_y
+ elif true_point[1] < 0:
+ self.view.scroll_y -= self.y_scroll_margin
+ true_point = self.to_parent(*check_point)
+ #print '-y', self.view.scroll_x, self.view.scroll_y
+ #print true_point, self.view.size
+
+ def included(self, point, margin):
+ if point[0] < margin: