2013-04-23 87 views
0

这是一个更概念性的问题: 如果我想旋转整个棋盘(例如棋盘)90度,我最好旋转单个字段单独围绕其中心点,还是有一种方法让我对场地进行“截图”并将其作为单张图片旋转?我会让代码将动画的实际值与动画分开(动画完成后,我只需重新画出屏幕上的所有内容)。旋转动画板(python,pygame)

我希望我的问题是可以理解的。我刚开始在Python编程几天的时间了,但至今只完成基于文本的游戏,但要移动到基于GUI的游戏,

预先感谢您,您的帮助是非常赞赏, 龙骨船

回答

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),你都完成了)

+3

如果你真的想从pygame切换到pyglet,我建议使用cocos2d而不是纯pyglet。 – sloth 2013-04-23 08:58:38

+1

当然可以。但请注意,您将远离纯pyglet代码的“灵活性”并添加更多依赖关系。在你使用cocos2d之前,你应该问问自己,你是否会想要在这个项目之外进行构建。如果没有,那么去cocos2d,如果你想快速修复:)(cocos2d是一个整洁的项目顺便说一句) – Torxed 2013-04-23 11:44:11

+0

* cocos2d是一个整洁的项目*:这就是低调:-) – sloth 2013-04-23 11:52:13

1

在pygame中,你有一个表面,当你初始化和配置你的显示器时创建。通常,人们会将其他图像直接涂抹到该表面上,然后更新显示器以将图像渲染到屏幕上,但没有理由不能创建另一个表面进行绘制,然后可以将其旋转并绘制到表面由显示器呈现。

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()