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


Python Camera.translate_to方法代码示例

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


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

示例1: Level

# 需要导入模块: from Camera import Camera [as 别名]
# 或者: from Camera.Camera import translate_to [as 别名]
class Level():
    def __init__(self, w, h, filename, data_dir):
        self.width = w
        self.height = h
        self._data_dir = data_dir
        os.chdir(self._data_dir)

        self.background = pygame.Surface((self.width, self.height))
        self.background = self.background.convert()
        self.background.fill((153, 217, 234))
        self.objects = pygame.sprite.LayeredUpdates()
        self.gravity = .5
        self.camera = Camera(800, 600)
        self._filename = filename
        self._load_objects()
        self.player = self._get_player_from_objects()
        self.input_binds = []
        self.gui_manager = GuiManager()
        self.build_manager = BuildManager(self.spawn_object)

    def get_filename(self):
        return self._filename

    def _get_player_from_objects(self):
        for obj in self.objects:
            if obj.type == PLAYER:
                return obj
        return None

    def get_obj(self, obj_name):
        for obj in self.objects:
            if obj_name == obj.name:
                return obj
        return None

    def _load_objects(self):
        obj_filename = os.path.join('level_data', self._filename)
        parser = configparser.ConfigParser()
        if obj_filename not in parser.read(obj_filename):
            print(obj_filename + 'not found/cant be read')

        object_list = parser.sections()
        for objname in object_list:
            obj_type = parser[objname]['type']
            var_dict = {'name': objname}
            for option in parser[objname]:
                value = parser[objname][option]
                var_dict[option] = to_num(value)
            self.objects.add(ObjFactory(obj_type, var_dict))

    def clear(self):
        while self.objects:
            cur_layer = self.objects.get_top_layer()
            self.objects.remove_sprites_of_layer(cur_layer)
        self.background.fill((153, 217, 234))

    def load(self, new_file=None):
        if new_file:
            self._filename = new_file
        self.clear()
        self._load_objects()
        self.player = self._get_player_from_objects()

    def update(self):
        #update object positions
        for obj in self.objects:
            obj.update()
            if obj.obey_gravity:
                obj.do_gravity(self.gravity)
            if obj.type == BUILD_PROC:
                self.build_manager.check_build(obj)
        self.check_collisions()
        self.camera.update(self.player)

    def draw(self, screen):
        screen.blit(self.background, (0, 0))
        for sprite_layer in self.objects.layers():
            for sprite in self.objects.get_sprites_from_layer(sprite_layer):
                if self.camera.on_screen(sprite.rect) and sprite.visible:
                    translated = self.camera.translate_to(sprite.rect)
                    sprite.draw(screen, translated)

    #calls collide for each object against each object besides itself
    #objects' collide function carries out what it means to collide and how to respond
    def check_collisions(self):
        for a_obj in self.objects:
            for b_obj in self.objects:
                if a_obj == b_obj:
                    continue
                a_obj.collide(b_obj)

    def objects_at(self, x, y):
        col_objs = [obj for obj in self.objects if obj.rect.collidepoint(x, y)]
        if col_objs:
            return col_objs
        else:
            return None

    def spawn_object(self, var_dict):
        self.objects.add(ObjFactory(var_dict['type'], var_dict))
开发者ID:v0ided,项目名称:FirstPyGame,代码行数:102,代码来源:Level.py


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