Skip to content
Advertisement

Pygame Line of Sight from Fixed Position

I am currently working on a 2D game in which the player has to sneak up on a still person within a certain amount of time. There are various crates in the way (depending on which level it is), and I would like to make it so that the player can hide behind crates to sneak up on the still person.


enter image description here

I thought that I could use a cone-type vision for the person looking, but I’m not exactly sure how I would accomplish that. The player doesn’t have to see the vision cone of the person looking either.

A similar effect to what I would like is in this sample code on github.

NOTE: The player cannot pass through the crates, and the people and crates are sprites.

Advertisement

Answer

You have to calculate the if the player is in line with the person, if it is you can check for every box if the 3 objects are ate the same position, if not you are in vision field person_looking. concidere player and person a list with coords.

def isInLine(player, person):
    deltaX = person[0] - player[0]
    deltaY = person[1] - player[1]

    if (person[0] == player[0]) or (person[1] == player[1]) or (abs(deltaX) == abs(deltaY)):
       return true

Like in a chess game, imagine you ahve to check if the king is in check by a queen. Its the same logic here.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement