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


Python Rect.contains方法代码示例

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


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

示例1: collision_check

# 需要导入模块: from pygame.rect import Rect [as 别名]
# 或者: from pygame.rect.Rect import contains [as 别名]
    def collision_check(self):
        """ Destroy the rocket if it comes outside of the borders. Also make sure a rocket that collides with a tile
            destroys the tile.
        """
        entity_rect = Rect(self.x, self.y, self.width, self.height)
        window_rect = Rect(0, 0, g.width * c.TILE_SIZE, g.height * c.TILE_SIZE)
        if not window_rect.contains(entity_rect):
            self.delete = True

        # Make sure collision rectangles are up to date
        self.update_collision_rects()
        # Get the tile the entity is standing on
        tile_pos = self.get_tile()
        checked_tile_rects = []
        checked_tiles = []

        # Loop through a 3x3 tile square around the entity, to not check the entire map
        for i in (range(tile_pos[0]-1, tile_pos[0]+2) if self.dir[0] == 0 else [tile_pos[0] + self.dir[0]]):
            for j in (range(tile_pos[1]-1, tile_pos[1]+2) if self.dir[1] == 0 else [tile_pos[1] + self.dir[1]]):
                try:
                    if c.IMAGES[g.map[i][j].type].collides:
                        checked_tiles.append((i, j))
                        checked_tile_rects.append(g.map[i][j].rect())

                except IndexError:  # That index was apparently outside of the map
                    pass

        # Find the tile that collides
        index = self.rects[(-self.dir[0], -self.dir[1])].collidelist(checked_tile_rects)
        if index != -1:
            # Replace that tile.
            g.tile_maker_queue.insert(0, [self.tile,
                                          checked_tiles[index][0],
                                          checked_tiles[index][1]])
            self.delete = True

        return super(LauncherRocket, self).collision_check
开发者ID:ZeeQyu,项目名称:TileGame,代码行数:39,代码来源:units.py


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