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


Python Sprite.image_to_sprite方法代码示例

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


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

示例1: SpriteEditor

# 需要导入模块: from sprite import Sprite [as 别名]
# 或者: from sprite.Sprite import image_to_sprite [as 别名]

#.........这里部分代码省略.........
                               'title': 'Export as image'}

        self.import_options = {'defaultextension': '.jpg',
                               'filetypes': [('jpeg files', '.jpg'), ('png files', '.png'), ('all files', '.*')],
                               'initialdir': 'C:\\',
                               'initialfile': 'default.jpg',
                               'title': 'Import Browser'}

        info_panel = View(540, 0, 295, 520, pygame.Color('0xffffff'))
        save_button = Button(5, 375, 65, 20, "Save", self.font)
        save_button.on_clicked.connect(self.save_sprite)
        load_button = Button(75, 375, 65, 20, "Load", self.font)
        load_button.on_clicked.connect(self.load_sprite)
        import_button = Button(145, 375, 65, 20, "Import", self.font)
        import_button.on_clicked.connect(self.import_sprite)
        export_button = Button(215, 375, 65, 20, "Export", self.font)
        export_button.on_clicked.connect(self.export_sprite)
        self.color_box = ColorPalette(0, 0, 295, 280, self.font)
        self.add(info_panel)
        info_panel.add(save_button)
        info_panel.add(load_button)
        info_panel.add(import_button)
        info_panel.add(export_button)
        info_panel.add(self.color_box)
        self.sprite.set_color_box(self.color_box)
        self.add(self.sprite)

        self.main_loop()

    def main_loop(self):
        controlled_view = None
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.MOUSEBUTTONUP:
                    mouse_pos = pygame.mouse.get_pos()
                    hit_view = self.hit(mouse_pos)
                    hit_view.mouse_up(event.button, mouse_pos)
                    controlled_view = None
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_pos = pygame.mouse.get_pos()
                    hit_view = self.hit(mouse_pos)
                    controlled_view = hit_view
                    hit_view.mouse_down(event.button, mouse_pos)
                elif event.type == pygame.MOUSEMOTION:
                    mouse_pos = pygame.mouse.get_pos()
                    hit_view = self.hit(mouse_pos)
                    if controlled_view and controlled_view.draggable:
                        controlled_view.mouse_drag(controlled_view, mouse_pos, event)
                    else:
                        hit_view.mouse_drag(controlled_view, mouse_pos, event)
            self.flip()
            self.fps_clock.tick(120)

    def load_sprite(self):
        """Loads a .spr Sprite file as the current_sprite and is used by the Load Button."""
        filename = tkFileDialog.askopenfilename(**self.options)
        if not filename == '':
            self.color_box.reset()
            session = shelve.open(filename)
            self.sprite.sprite_size = session['dimension']
            self.sprite.pixel_size = session['size']
            for index in range(len(self.sprite.pixels)):
                color, alpha = session['%d' % index]
                self.sprite.pixels[index].change_color(color)
                self.sprite.pixels[index].set_alpha(alpha)
                self.sprite.color_box.add_to_history(color)
            session.close()
            pygame.event.pump()

    def save_sprite(self):
        """Saves the current_sprite as a .spr Sprite file and is used by the Save Button."""
        filename = tkFileDialog.asksaveasfilename(**self.options)
        if not filename == '':
            session = shelve.open(filename)
            session['dimension'] = self.sprite.sprite_size()
            session['size'] = self.sprite.pixel_size()
            for index in range(len(self.sprite.pixels)):
                color = self.sprite.pixels[index].get_color()
                alpha = self.sprite.pixels[index].get_alpha()
                session['%d' % index] = (color, alpha)
            session.close()
            pygame.event.pump()

    def export_sprite(self):
        """Exports the current_sprite as an image and is used by the Export Button."""
        filename = tkFileDialog.asksaveasfilename(**self.export_options)
        if not filename == '':
            pygame.image.save(self.sprite.make_image(), filename)
        pygame.event.pump()

    def import_sprite(self):
        """Imports an image file to the current_sprite and is used by the Import Button."""
        filename = tkFileDialog.askopenfilename(**self.import_options)
        if not filename == '':
            image = Image.open(filename).resize(self.sprite.sprite_size(), Image.BICUBIC)
            self.sprite.image_to_sprite(image)
            self.sprite.render(self.window)
            pygame.event.pump()
开发者ID:Daustoe,项目名称:nostalgia,代码行数:104,代码来源:main.py


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