0

Walls do block me but if i keep walking into them i teleport through. I've tried a number of things and cant seem to figure it out. Things that make it difficult is that my character rotates based on where my mouse position is. self.level_terrain is a sprite.group. update is called from my state manager.

    def update(self, clock):
    self.dT = clock.get_time()
    pressed = pg.key.get_pressed()
    mousePos = pg.mouse.get_pos()
    self.player.update(mousePos, pressed, self.dT, self.level_terrain)


def get_angle(origin, destination):
    """Returns angle in radians from origin to destination.
        This is the angle that you would get if the points were
        on a cartesian grid. Arguments of (0,0), (1, -1)
        return pi/4 (45 deg) rather than  7/4.
        """
    x_dist = destination[0] - origin[0]
    y_dist = destination[1] - origin[1]
    return atan2(-y_dist, x_dist) % (2 * pi)

class Player(pg.sprite.Sprite):
    def __init__(self, center_pos):
        super(Player, self).__init__()
        self.original_image = pg.image.load("data/resources/images/playerImage.png").convert_alpha()
        self.facing_angle = 90
        self.start_angle = 90
        self.pos = center_pos
        self.velocity = [0, 0]
        self.set_image()


    self.vx = 0
    self.vy = 0


    self.keys_dict = {
            pg.K_w: (0, -1),
            pg.K_s: (0, 1),
            pg.K_a: (-1, 0),
            pg.K_d: (1, 0)}
    self.speed = .2

def set_image(self):
    angle = self.facing_angle - self.start_angle
    self.image = pg.transform.rotate(self.original_image, angle)
    self.rect = self.image.get_rect()

def update(self, mouse_pos, pressed, dt, obstacles):

    self.facing_angle = degrees(get_angle(self.pos, mouse_pos))
    self.set_image()
    last = self.rect.copy()
    for k in self.keys_dict:
        if pressed[k]:
            x, y = self.keys_dict[k]
            self.vx = x * self.speed * dt
            self.vy = y * self.speed * dt
            self.pos = (self.pos[0] + (self.vx),
                            self.pos[1] + (self.vy))
    self.rect.center = self.pos
    current = self.rect  # Just for readability we 'rename' the objects rect attribute to 'current'.
    for wall in pg.sprite.spritecollide(self, obstacles, dokill=False):
        wall = wall.rect  # Just for readability we 'rename' the wall's rect attribute to just 'wall'.
        if last.left >= wall.right > current.left:  # Collided left side.
            current.left = wall.right
        elif last.right <= wall.left < current.right:  # Collided right side.
            current.right = wall.left
        elif last.top >= wall.bottom > current.top:  # Collided from above.
            current.top = wall.bottom
        elif last.bottom <= wall.top < current.bottom:  # Collided from below.
            current.bottom = wall.top

def draw(self, surface):
    surface.blit(self.image, self.rect)
Lewis
  • 1
  • use `print()` in `for wall` loop to see how it changes values - maybe this way you find clue to resolve problem. It is not so easy to resolve this problem without running code. – furas Oct 24 '16 at 12:08
  • btw: you can collide with one element at the same time `"on left/right"` and `"on top/bottom"` but after you check `"on left/right"` you don't check `"on top/bottom"` - you have to change one `elif` into `if` – furas Oct 24 '16 at 12:13

0 Answers0