Changeset 155:b2e0f1246c38


Ignore:
Timestamp:
Apr 5, 2011, 8:51:14 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Hook up bitmasked based collisions (mask caching may be unnecessary, please comment on whether this effects performance for you).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r152 r155  
    99import pygame.draw
    1010import pygame.sprite
     11from pygame.mask import from_surface
    1112
    1213from . import options
     
    3637
    3738    floor = False  # We special case collisions with ground objects
    38     block = False 
     39    block = False
    3940
    4041    def __init__(self, *args, **kwargs):
     
    4647        self.image = pygame.Surface((10, 10))
    4748        self.image.fill((0, 0, 200))
     49        self._mask_cache = {} # image id -> collision bit mask
    4850
    4951    def init_pos(self):
     
    8284        self.collide_rect.move_ip(delta_pos)
    8385
     86    def _check_mask(self):
     87        image_id = id(self.image)
     88        mask = self._mask_cache.get(image_id, None)
     89        if mask is None:
     90            mask = self._mask_cache[image_id] = from_surface(self.image)
     91        self.mask = mask
     92
    8493    def check_collides(self, other):
    85         return True # default to relying purefly on collision_layer and collides_with
     94        # check bitmasks for collision
     95        self._check_mask()
     96        other._check_mask()
     97        return pygame.sprite.collide_mask(self, other)
    8698
    8799    def collided(self, other):
Note: See TracChangeset for help on using the changeset viewer.