+ 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:
+ 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
+
+ def draw_nemesis(self):
+ if not self.nemesis.on_board():
+ return
+ if self.nemesis_tile:
+ self.remove_widget(self.nemesis_tile)
+ sprite_pos = (self.nemesis.pos[0] * TILE_SIZE,
+ self.nemesis.pos[1] * TILE_SIZE)
+ self.nemesis_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
+ pos=sprite_pos)
+ with self.nemesis_tile.canvas:
+ Color(1, 1, 1)
+ Rectangle(pos=sprite_pos, size=self.nemesis_tile.size,
+ texture=self.nemesis.get_texture())
+ self.add_widget(self.nemesis_tile)