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


Python Sprite.set_color_box方法代码示例

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


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

示例1: SpriteEditor

# 需要导入模块: from sprite import Sprite [as 别名]
# 或者: from sprite.Sprite import set_color_box [as 别名]
class SpriteEditor(Console):
    def __init__(self):
        super(SpriteEditor, self).__init__(835, 520)
        self.sprite = Sprite(0, 0, 540, 520)
        self.font = pygame.font.Font("resources/Munro.ttf", 18)
        self.font.set_bold(True)
        self.set_caption("Sprite Editor")
        Tk().withdraw()
        self.fps_clock = pygame.time.Clock()
        self.options = {'defaultextension': '.spr',
                        'filetypes': [('sprite files', '.spr'), ('all files', '.*')],
                        'initialdir': 'C:\\',
                        'initialfile': 'default.spr',
                        'title': 'File Browser'}

        self.export_options = {'defaultextension': '.png',
                               'initialdir': 'C:\\',
                               'initialfile': 'default.png',
                               'filetypes': [('PNG files', '.png'), ('all files', '.*')],
                               '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()
#.........这里部分代码省略.........
开发者ID:Daustoe,项目名称:nostalgia,代码行数:103,代码来源:main.py


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