0

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

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

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

4

2 回答 2

2

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

编辑:我刚刚意识到 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,而不是逐项发送,这样可以轻松快速轻松地完成复杂任务。您也可以batch.rotate(90)做到完毕)

于 2013-04-23T07:42:58.683 回答
1

在 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-04-23T13:45:59.007 回答