Paul_was_taken
Paul_was_taken

Reputation: 51

How may I determine a type of an object at the screen(pygame)?

The point is I need to find out what type of object is at (x, y)

def choose_target(self, screen):
    for y in range(-tower_range, tower_range + 1):
        for x in range(-tower_range, tower_range + 1):
            if screen[self.x + x, self.y + y] is Enemy:
                self.target = screen[self.x + x, self.y + y]

I thought it should be like this but it turned up I can't get information from the screen. Also how may make my tower remember a target? Now, I am not sure about this self.target = screen[self.x + x, self.y + y] as well.

Upvotes: 0

Views: 155

Answers (1)

Jonathon Ogden
Jonathon Ogden

Reputation: 1582

Rather than trying to pinpoint an object within the screen space (a common way to do that is 'ray casting'), check for a collision against objects in the game world.

To do that, loop through the objects in the game world and check if the (x, y) point collides with the objects bounding boxes (rects) like so: object.rect.collidepoint(pos).

If true, you can then check if the object is of type Enemy.

Upvotes: 1

Related Questions