0

So this is my code:

def is_facing_monster(self, monster_rect):
        dx = monster_rect.centerx - self.player_rect_pistol.centerx
        dy = monster_rect.centery - self.player_rect_pistol.centery
        distance = math.sqrt(dx ** 2 + dy ** 2)

        if distance <= SHOOTING_DISTANCE:
            if self.last_moved == 'left' or self.last_moved == 'left_still' and dx < 0:
                return True
            elif self.last_moved == 'right'or self.last_moved == 'right_still' and dx > 0:
                return True
            if self.last_moved == 'up' or self.last_moved == 'up_still' and dy < 0:
                return True
            elif self.last_moved == 'down' or self.last_moved == 'down_still'and dy > 0:
                return True
            if self.last_moved == 'upleft' or self.last_moved == 'upleft_still' and dx < 0 and dy < 0:
                return True
            elif self.last_moved == 'upright' or self.last_moved == 'upright_still' and dx > 0 and dy < 0:
                return True
            if self.last_moved == 'downleft' or self.last_moved == 'downleft_still' and dx < 0 and dy > 0:
                return True
            elif self.last_moved == 'downright' or self.last_moved == 'downright_still' and dx > 0 and dy > 0:
                return True

        return False

My problem is that when I am facing monster I shoot and hit is not registered correctly.

diagonally, (upleft, downright...) there is no registration completely. if my player is above monster and is facing down, hit is registered, if player is below monster and faces up and is in 'up_still' and shoots does no damage, unless he walks into monster (then it changes to 'uo' sprites) then damage is done shotting right does damage to monster no matter if monster is from the right or from the left and shooting left does damage only when the player is not facing monster and is walking 'left' frames.

How to fix it, maybe someone knows similar solution?

I add photo for reference:

enter image description here

I tried to do this different approach, but it did not work:

def is_facing_monster(self, monster_rect):
        dx = monster_rect.centerx - self.player_rect_pistol.centerx
        dy = monster_rect.centery - self.player_rect_pistol.centery
        distance = math.sqrt(dx**2 + dy**2)
        
        print(dx, dy, self.last_moved, distance)
        if distance <= SHOOTING_DISTANCE:
            angle = math.atan2(dy, dx)  # Calculate the angle between the player and the monster
            angle_deg = math.degrees(angle)

            direction_ranges = {
                'left': (-180, -135),
                'right': (-45, 45),
                'up': (45, 135),
                'down': (-135, -45),
                'upleft': (135, 180),
                'upright': (-135, -90),
                'downleft': (90, 135),
                'downright': (-90, -45)
            }

          if self.last_moved in direction_ranges:
                angle_range = direction_ranges[self.last_moved]
                if angle_range[0] <= angle_deg <= angle_range[1]:
                    return True
  • 1
    There are not enough parts of your code there for anyone to provide you a meaningful answer. Please try to create a sttriped down, stand alone version of your code wher ethe problem happens, and put that in full (a minimum reproducible example) - (for example, just paste rects instead of loading images in that code) – jsbueno Jul 11 '23 at 17:18

0 Answers0