【问题标题】:rotate animation board (python, pygame)旋转动画板(python,pygame)
【发布时间】:2013-04-23 21:45:59
【问题描述】:

这更像是一个概念性问题: 如果我想将整个棋盘(例如棋盘)旋转 90 度,我最好将棋盘的各个区域单独围绕其中心点旋转,还是有办法让我拍摄“截图”字段并将其旋转为单张图片?我会让代码将板的实际值与动画分开旋转(一旦动画完成,我将重新绘制屏幕,​​现在所有内容都在正确的位置)。

我希望我的问题可以理解。几天前我刚开始用 python 编程,到目前为止只做过基于文本的游戏,但想转向基于 GUI 的游戏,

提前感谢您,非常感谢您的帮助, 龙骨船

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您应该完全旋转电路板。 截取屏幕截图并旋转几乎与旋转棋盘对象一样费力。批处理对象并旋转它们将是一个解决方案。

    编辑:我刚刚意识到 Pygame 是少数可能会错过批处理渲染的库之一。 Pygame 不错,适合入门学习曲线,但你最好使用其他库(这只是一个友好的建议)

    友情建议


    如果我真的想做一些很酷的事情(包括你规模的游戏开发),我会选择 Pyglet。 它是跨平台的,不依赖于 Python 版本,就像所有其他版本一样,您可以直接连接到 OpenGL 库,使其速度非常快。而且它实际上很容易使用。

    这里是拖放示例:

    #!/usr/bin/python
    import pyglet
    from time import time, sleep
    
    class Window(pyglet.window.Window):
        def __init__(self, refreshrate):
            super(Window, self).__init__(vsync = False)
            self.frames = 0
            self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
            self.last = time()
            self.alive = 1
            self.refreshrate = refreshrate
            self.click = None
            self.drag = False
    
        def on_draw(self):
            self.render()
    
        def on_mouse_press(self, x, y, button, modifiers):
            self.click = x,y
    
        def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
            if self.click:
                self.drag = True
                print 'Drag offset:',(dx,dy)
    
        def on_mouse_release(self, x, y, button, modifiers):
            if not self.drag and self.click:
                print 'You clicked here', self.click, 'Relese point:',(x,y)
            else:
                print 'You draged from', self.click, 'to:',(x,y)
            self.click = None
            self.drag = False
    
        def render(self):
            self.clear()
            if time() - self.last >= 1:
                self.framerate.text = str(self.frames)
                self.frames = 0
                self.last = time()
            else:
                self.frames += 1
            self.framerate.draw()
            self.flip()
    
        def on_close(self):
            self.alive = 0
    
        def run(self):
            while self.alive:
                self.render()
                # ----> Note: <----
                #  Without self.dispatc_events() the screen will freeze
                #  due to the fact that i don't call pyglet.app.run(),
                #  because i like to have the control when and what locks
                #  the application, since pyglet.app.run() is a locking call.
                event = self.dispatch_events()
                sleep(1.0/self.refreshrate)
    
    win = Window(23) # set the fps
    win.run()
    

    Pyglet 还使您能够进行批量渲染(这意味着您可以将指令以大块的形式发送到 GPU,而不是逐项发送,这样可以轻松快速、轻松地完成复杂的任务。您也可以这样做 @987654322 @你就完成了)

    【讨论】:

    • 如果你真的想从 pygame 切换到 pyglet,我建议使用 cocos2d 而不是纯 pyglet。
    • 当然可以。但请注意,您将摆脱纯 pyglet 代码的“灵活性”并添加更多依赖项。在你使用 cocos2d 之前,你应该问问自己,你是否想要在这个项目之外进行构建。如果没有,那么如果你想快速修复,那就去 cocos2d :) (顺便说一句,cocos2d 是一个整洁的项目)
    • cocos2d 是一个不错的项目:这太低调了 :-)
    【解决方案2】:

    在 Pygame 中,您有一个在初始化和配置显示器时创建的表面。通常,人们会将其他图像直接blit到这个表面,然后更新显示以将图像渲染到屏幕上,但没有理由不能创建另一个要绘制的表面,然后可以旋转并绘制到表面由显示器渲染。

    screen = pygame.display.set_mode((500,500))
    middle_man = pygame.Surface((500,500))
    
    # draw stuff to middle_man here
    ....
    
    # rotate middle_man
    # note that this creates a padded surface if not rotating in 90 degree increments
    rotated_middle_man = pygame.transform.rotate(middle_man, 45.0)
    
    # draw middle_man back to the screen surface
    crop_rect = pygame.Rect(rotated_middle_man.get_width() / 2 - screen.get_width() / 2,
                            rotated_middle_man.get_height() / 2 - screen.get_height() / 2,
                            screen.get_width(), screen.get_height())
    screen.blit(rotated_middle_man, (0,0), crop_rect)
    
    # update the screen
    pygame.display.update()
    

    【讨论】:

      猜你喜欢
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2011-05-20
      • 2013-05-22
      相关资源
      最近更新 更多