当前位置: 首页>>代码示例>>Python>>正文


Python Rect.collidelistall方法代码示例

本文整理汇总了Python中pygame.rect.Rect.collidelistall方法的典型用法代码示例。如果您正苦于以下问题:Python Rect.collidelistall方法的具体用法?Python Rect.collidelistall怎么用?Python Rect.collidelistall使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygame.rect.Rect的用法示例。


在下文中一共展示了Rect.collidelistall方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: PhysicsComponent

# 需要导入模块: from pygame.rect import Rect [as 别名]
# 或者: from pygame.rect.Rect import collidelistall [as 别名]
class PhysicsComponent(object):
    def __init__(self, hitbox_size):
        self._hitbox = Rect((0, 0), hitbox_size)

    def _move(self, dx, dy, rect, wall_rects):
        """Moves each axis separately, checking for wall collisions twice.

        Returns:
            True iff a collision (in either axis) occurred.
        """
        collided_x = collided_y = False
        if dx != 0:
            collided_x = self._move_single_axis(dx, 0, rect, wall_rects)
        if dy != 0:
            collided_y = self._move_single_axis(0, dy, rect, wall_rects)
        return collided_x or collided_y

    def _move_single_axis(self, dx, dy, rect, wall_rects):
        """Moves the rect in a single axis, checking for collisions.

        Returns:
            True iff a collision with some wall rectangle occurred.
        """
        rect.move_ip(dx, dy)
        self._hitbox.midbottom = rect.midbottom

        indices = self._hitbox.collidelistall(wall_rects)
        collided = bool(indices)
        if collided:
            other_rects = (wall_rects[i] for i in indices)
            if dx > 0:
                self._hitbox.right = min(r.left for r in other_rects)
            if dx < 0:
                self._hitbox.left = max(r.right for r in other_rects)
            if dy > 0:
                self._hitbox.bottom = min(r.top for r in other_rects)
            if dy < 0:
                self._hitbox.top = max(r.bottom for r in other_rects)

        rect.midbottom = self._hitbox.midbottom

        return collided

    def update(self, sprite, world):
        """Updates the sprite's position."""
        (dx, dy) = sprite.velocity
        wall_rects = [w.rect for w in world.wall_tiles]

        # Stop moving if there was a collision.
        if self._move(dx, dy, sprite.rect, wall_rects):
            sprite.velocity = (0, 0)
            sprite.input_comp.next_pos = None
开发者ID:adriano-arce,项目名称:chipmunk-game,代码行数:54,代码来源:physics_component.py

示例2: draw

# 需要导入模块: from pygame.rect import Rect [as 别名]
# 或者: from pygame.rect.Rect import collidelistall [as 别名]
    def draw(self, surface, bgd=None):
        """draw all sprites in the right order onto the passed surface.
        LayeredDirty.draw(surface, bgd=None): return Rect_list

        You can pass the background too. If a background is already set,
        then the bgd argument has no effect.
        """

        # speedups
        _orig_clip = surface.get_clip()
        _clip = self._clip
        if _clip is None:
            _clip = _orig_clip
        
        
        _surf = surface
        _surf_rect = _surf.get_rect()
        _sprites = self._spritelist 
        _old_rect = self.spritedict
        _update = self.lostsprites
        _update_append = _update.append
        _ret = None
        _surf_blit = _surf.blit
        _rect = Rect
        #_collidelistall = _rect.collidelistall
        if bgd is not None:
            self._bgd = bgd
        _bgd = self._bgd
        
        _surf.set_clip(_clip)
        # -------
        # 0. deside if normal render of flip
        start_time = get_ticks()

        if (self._use_update or self._seek_dirties) and not self._first_draw: # dirty rects mode
            # 1. find dirty area on screen and put the rects into _update
            # still not happy with that part
            for spr in _sprites:
                if 0 < spr.dirty:
                    # chose the right rect
                    if spr.source_rect:
                        _union_rect = _rect(spr.rect.topleft, spr.source_rect.size)
                    else:
                        _union_rect = _rect(spr.rect)
        
                    _union_rect_collidelist = _union_rect.collidelist
                    _union_rect_union_ip = _union_rect.union_ip
                    i = _union_rect_collidelist(_update)
                    while -1 < i:
                        _union_rect_union_ip(_update[i])
                        del _update[i]
                        i = _union_rect_collidelist(_update)
                    _update_append(_union_rect.clip(_clip))
        
                    _union_rect = _rect(_old_rect[spr])
                    _union_rect_collidelist = _union_rect.collidelist
                    _union_rect_union_ip = _union_rect.union_ip
                    i = _union_rect_collidelist(_update)
                    while -1 < i:
                        _union_rect_union_ip(_update[i])
                        del _update[i]
                        i = _union_rect_collidelist(_update)
                    _update_append(_union_rect.clip(_clip))
            # can it be done better? because that is an O(n**2) algorithm in
            # worst case
        
            # clear using background
            if _bgd is not None:
                for rec in _update:
                    _surf_blit(_bgd, rec, rec)
        
            # 2. draw
            for spr in _sprites:
                if 1 > spr.dirty:
                    if spr._visible:
                        # sprite not dirty, blit only the intersecting part
                        _spr_rect = spr.rect
                    
                        if spr.source_rect is not None:
                            _spr_rect = Rect(spr.rect.topleft, spr.source_rect.size)
                            _spr_source_rect_left = spr.source_rect.left
                            _spr_source_rect_top = spr.source_rect.top
                        else:
                            _spr_source_rect_left = 0
                            _spr_source_rect_top = 0
                        
                        _spr_rect_clip = _spr_rect.clip
                        for idx in _spr_rect.collidelistall(_update):
                            # clip
                            clip = _spr_rect_clip(_update[idx])
                            #print  (spr.source_rect.left + clip[0]-_spr_rect[0], spr.source_rect.top + clip[1]-_spr_rect[1],
                            #        clip[2], \
                            #        clip[3])
        
                            _surf_blit(spr.image, clip, \
                                       (_spr_source_rect_left + clip[0]-_spr_rect[0],
                                        _spr_source_rect_top + clip[1]-_spr_rect[1],
                                        clip[2], clip[3]),
                                        spr.blendmode)
                else: # dirty sprite
#.........这里部分代码省略.........
开发者ID:MrGecko,项目名称:pyguane,代码行数:103,代码来源:layereddirty2.py

示例3: Unit

# 需要导入模块: from pygame.rect import Rect [as 别名]
# 或者: from pygame.rect.Rect import collidelistall [as 别名]

#.........这里部分代码省略.........
        frames_tall = max(self.orientation_indices) + 1
        frames_wide = self.walking_animations + self.attacking_animations + 1
        anim_row = self.orientation_indices[self.orientation]
        anim_col = self.animation_frame
        return chromographs.obtain_frame(self.animated_chromograph_name, anim_col, anim_row, frames_wide, frames_tall)

    def attack(self, audible=True):
        """ Assume a ferocius aspect """
        if self.attacking or not self.attacking_animations:
            return
        if self.reload_time > time():
            return
        self.temporal_accumulator = 0
        self.attacking = True
        self.reload_time = time() + 5.0 / self.rapidity
        self.animation_frame = self.walking_animations + 1
        self.image = self.obtain_frame()
        if audible:
            phonographs.play(self.attack_phonograph)
        return True

    def animate(self, ms):
        """ Create the illusion of movement """
        self.temporal_accumulator += ms
        if self.temporal_accumulator < self.pace:
            return False  # nothing to be done
        self.temporal_accumulator = 0
        frame = self.animation_frame
        if self.destroyed():
            frame = 0
        elif self.attacking:
            frame += 1
            if frame > self.walking_animations + self.attacking_animations:
                frame = 1
                self.attacking = False
        elif self.walking:
            frame += 1
            if frame > self.walking_animations:
                frame = 1
        self.animation_frame = frame
        self.image = self.obtain_frame()
        return True  # something was changed

    def harm(self, quanta_of_destruction, cause=None):
        """ Deal damage to the unit, possibly rendering it inactive """
        self.damage += quanta_of_destruction
        self.flash = True
        if self.damage >= self.durability:
            self.obstruance = 0
            self.animation_frame = 0
            self.walking = False
            self.attacking = False
            self.image = self.obtain_frame()
            self.killed_by = cause

    def destroyed(self):
        return self.damage >= self.durability

    def things_perceived(self, things):
        """ things that are perceived """
        indices = self.rect_of_awareness.collidelistall(things)
        return [things[i] for i in indices]

    def things_in_range(self, things):
        indices = self.rect_of_attack.collidelistall(things)

    def find_obstacles(self, location, knowledge):
        """ find things that obstruct a rectangle such that this
        unit may not occupy the same space if it were there.
        """
        indices = location.collidelistall(knowledge)
        return [
            knowledge[i] for i in indices if knowledge[i] is not self and (knowledge[i].obstruance & self.exclusion)
        ]

    def find_nearest(self, things):
        """ nearest thing sorted by distance from centre """

        def dist(thing):
            rx, ry = thing.rect.center
            mx, my = self.rect.center
            return (rx - mx) ** 2 + (ry - my) ** 2

        return sorted(things, key=dist)

    def orientation_towards(self, position):
        """ octaclock direction from self centre to position """
        cx, cy = self.rect.center
        px, py = position[:2]
        dx = px - cx
        dy = py - cy
        steep = (dx == 0) or abs(dy / dx) > 3
        shallow = (dy == 0) or abs(dx / dy) > 3
        if steep:
            return 0 if dy < 0 else 4
        if shallow:
            return 2 if dx > 0 else 6
        if dy < 0:
            return 1 if dx > 0 else 7
        return 3 if dx > 0 else 5
开发者ID:scavpy,项目名称:Scav-Threads-PyWeek-Sep-2012,代码行数:104,代码来源:units.py


注:本文中的pygame.rect.Rect.collidelistall方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。