2013-04-23 86 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()