From 8235496b3edb4e951c39649c9663a17d94159898 Mon Sep 17 00:00:00 2001 From: Lily Seabreeze Date: Thu, 18 Aug 2016 17:26:55 -0500 Subject: [PATCH] simpler and actually works properly! fix #95 fix #95 --- demo/demo.py | 5 ++-- sappho/collide.py | 76 +++++++++++++++++++---------------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/demo/demo.py b/demo/demo.py index 0cb41ed..2bb80bb 100644 --- a/demo/demo.py +++ b/demo/demo.py @@ -68,8 +68,8 @@ def update(self, camera, wall_collision_group, layer_size, timedelta): return None # move - new_coord = [self.x_speed + self.collider.rect.topleft[0], - self.y_speed + self.collider.rect.topleft[1]] + new_coord = (self.x_speed + self.collider.rect.topleft[0], + self.y_speed + self.collider.rect.topleft[1]) dummy_future_rect = self.collider.rect.copy() dummy_future_rect.topleft = new_coord @@ -77,6 +77,7 @@ def update(self, camera, wall_collision_group, layer_size, timedelta): if wrapped_coord != new_coord: # we wrapped around the screen pacman style + print([wrapped_coord, new_coord]) oldie = self.collider.rect.topleft self.collider.rect.topleft = wrapped_coord collided_with = self.collider.collides_rect_mask(wall_collision_group) diff --git a/sappho/collide.py b/sappho/collide.py index 290d4d5..ca18456 100644 --- a/sappho/collide.py +++ b/sappho/collide.py @@ -102,11 +102,9 @@ def collides_rect_mask(self, sprite_group): and (pygame.sprite .collide_mask(sprite_whose_rect_collides, self))): - print("mask collision") return sprite_whose_rect_collides elif (not hasattr(self, 'mask')) and (not hasattr(self, 'mask')): - print("rect collision") return sprite_whose_rect_collides else: @@ -146,60 +144,48 @@ def move_as_close_as_possible(self, destination, sprite_group): """ - position_difference_x = self.rect.topleft[0] - destination[0] - position_difference_y = self.rect.topleft[1] - destination[1] + # first figure out the x and y increments + goal_x, goal_y = destination + current_x, current_y = self.rect.topleft - if position_difference_y > 0: - y_modifier = -1 - elif position_difference_y < 0: - y_modifier = 1 - elif position_difference_y == 0: - y_modifer = 0 + if goal_x > self.rect.left: + x_increment = 1 + elif goal_x < self.rect.left: + x_increment = -1 + else: + x_increment = 0 + + if goal_y > self.rect.top: + y_increment = 1 + elif goal_y < self.rect.top: + y_increment = -1 + else: + y_increment = 0 - if position_difference_x > 0: - x_modifier = -1 - elif position_difference_x < 0: - x_modifier = 1 - elif position_difference_x == 0: - x_modifer = 0 + # ... + colliding_with = None + distance_to_goal_x = goal_x - current_x + distance_to_goal_y = goal_y - current_y - # will not check the last position - while not (position_difference_x == 0 and position_difference_y == 0): + while True: + last_safe_topleft = self.rect.topleft - if position_difference_x != 0: - position_difference_x += x_modifier + if not self.rect.top == goal_y: + self.rect.top += y_increment - if position_difference_y != 0: - position_difference_y += y_modifier + if not self.rect.left == goal_x: + self.rect.left += x_increment - old_coord = self.rect.topleft - new_coord = [position_difference_x + self.rect.topleft[0], - position_difference_y + self.rect.topleft[1]] - self.rect.topleft = new_coord colliding_with = self.collides_rect_mask(sprite_group) if colliding_with: - self.rect.topleft = old_coord + print("is colliding somehow") + self.rect.topleft = last_safe_topleft return colliding_with + elif self.rect.topleft == (goal_x, goal_y): + break - # one last check for the last point - if position_difference_x != 0: - position_difference_x += x_modifier - - if position_difference_y != 0: - position_difference_y += y_modifier - - old_topleft = self.rect.topleft - new_coord = [position_difference_x + self.rect.topleft[0], - position_difference_y + self.rect.topleft[1]] - self.rect.topleft = new_coord - colliding_with = self.collides_rect_mask(sprite_group) - - if colliding_with: - self.rect.topleft = old_coord - return colliding_with - else: - return None + return None # TODO: what if I want diagonal!? def sprites_in_path(self, new_coord, sprite_group):