pygame collision detection question

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 4604 Views - Last Post: 01 April 2013 - 12:18 AM Rate Topic: -----

#31 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 07 March 2013 - 02:43 PM

ok just a question on my new project. It's a top veiw survival game(or at least thats what I hope its going to be).
Whats the best way of having the screen centered on the player? is there a way of centering the screen, or do you need to move everything else?

Slidon
Was This Post Helpful? 0
  • +
  • -

#32 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: pygame collision detection question

Posted 07 March 2013 - 04:37 PM

If I understand you correctly, you will have to move the background, not the player. Usually for this type of thing, as long as the player is not near an edge of the entire map area, he stays in the center and you move the map, otherwise you move the player.

Think about how basic Mario-style sidescrollers work. Mario is centered in the screen when he is running through the level, but when he approaches the end of an area, his sprite moves off center.

-Mek
Was This Post Helpful? 0
  • +
  • -

#33 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 27 March 2013 - 10:07 AM

ok fixed, thanks
Was This Post Helpful? 0
  • +
  • -

#34 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 29 March 2013 - 04:20 AM

hi again, I have another problem. I think I probably should have got the hang of collision detection by now but it seems I haven't, in my new game I am trying to detect collision between the player and a list of pond objects.
def pond_collide(self, ponds):
        for pond in ponds:
            if self.mask.overlap_area(puddle.mask, (0, 0)):
                print "detected"

all the objects in ponds have rects and masks and everything. When they move they are moved with there rects, everything looks right.
but when the program is run, it prints "detected" non stop. Even when the player is nowhere near a pond.

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#35 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: pygame collision detection question

Posted 29 March 2013 - 06:20 AM

The second argument is supposed to be the offset of the two masks. You are testing collision if both masks are at (0,0)... so... they always collide.

Try something like this maybe:
offset = (-self.rect.x+item.rect.x,-self.rect.y+item.rect.y)
if self.mask.overlap_area(item.mask,offset):
    print("collision detected correctly?")


-Mek
Was This Post Helpful? 0
  • +
  • -

#36 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 29 March 2013 - 07:57 AM

ok, I dont quite get it. I would have thought that if the offset was (10, 10) it would be set off when the item was 10 pixels away, so if it was (0, 0) it would only get set off if they were touching. anyway I changed the line and now it never sets off. I will do some searching now I know what the problem is.
Was This Post Helpful? 0
  • +
  • -

#37 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: pygame collision detection question

Posted 29 March 2013 - 08:35 AM

The offset is the coordinate distance that the two masks are from each other... it has nothing to do with when it "sets-off". Your masks don't have any sense of where they are; they have to be told. It has to be a function of the rectangle coordinates that the two sprites currently have. Do your ponds each have individual masks or does the background as a whole have a mask?

If it is the latter then you can leave off the item.rect elements as that mask would be at (0,0).

-Mek
Was This Post Helpful? 0
  • +
  • -

#38 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 29 March 2013 - 09:03 AM

my ponds have individiual masks, would the offset then be the masks location?
Was This Post Helpful? 0
  • +
  • -

#39 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: pygame collision detection question

Posted 29 March 2013 - 09:20 AM

Offset is the distance between the rectangles of the sprites represented by the masks.

You have a Pond class that has a mask and also has a rect set to its current location.
You have a Player class that has a mask and also has a rect set to its current location.

You can write the function two ways:
offset = (self.rect.x-Pond.rect.x,self.rect.y-Pond.rect.y)
if Pond.mask.overlap_area(self.mask,offset):
    print("Do stuff")
or:
offset = (Pond.rect.x-self.rect.x,Pond.rect.y-self.rect.y)
if self.mask.overlap_area(Pond.mask,offset):
    print("Do stuff")

If neither of these work then either your masks or rectangles are not set-up correctly. If you could put together a concise example I'll take a look.

(Edit: In my previous code samples from the platforming examples, the offset was always just the location of the player. But this was only because the background's "rect" was the screen itself and was always located at (0,0).)

-Mek

This post has been edited by Mekire: 29 March 2013 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#40 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 29 March 2013 - 09:45 AM

no... its not working. My player is set up like this
class Player:
    def __init__(self):
        self.image = pygame.image.load('player_up.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        self.direction = "up"
        self.rect.topleft = [320, 240]
        self.speed = [0, 0]

and it doesn't move. My pond is set up like this:
class Pond:
    def __init__(self, location, admin):
        self.image = pygame.image.load('pond.png').convert_alpha()
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.topleft = location
        self.spawning = True
        if admin:
            self.ponds = []
            self.pond_surf = pygame.Surface((1500, 1500)).convert_alpha()
            self.pond_rect = self.pond_surf.get_rect()
            self.pond_surf.fill((0, 0, 0, 0))
            self.ponds_mask = pygame.mask.from_surface(self.pond_surf)

the only time it moves is here:
def update_image(self, Surface, distance):
        self.pond_rect.x += distance[0]
        self.pond_rect.y += distance[1]
        Surface.blit(self.pond_surf, self.pond_rect)

hope you can do something with this
Was This Post Helpful? 0
  • +
  • -

#41 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: pygame collision detection question

Posted 29 March 2013 - 11:23 AM

Give this a try and see if you can do something similar.

It isn't particularly good but it should help you figure out how to use collision.
Arrow keys for movement, escape to quit:
Spoiler


Cheers,
-Mek
Was This Post Helpful? 0
  • +
  • -

#42 slidon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-February 13

Re: pygame collision detection question

Posted 01 April 2013 - 12:18 AM

thanks, I have re written most of my code and everything is now working.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3