2014-01-29 45 views
1

我正在使用PyGame進行圖形測試來模擬正在展開的Dragon Curve。我已經制作了一個成功的版本,它們可以跟蹤彼此旋轉時的所有點,但很明顯,經過幾次迭代後,它會開始顯着減慢。爲了加快速度,我想簡單地將繪製的片段存儲到一個圖像變量中,並不斷地將一段屏幕保存到一個變量中,並繪製出移動的軌跡,而不是跟蹤很多點。我如何做以下任一項?如何繪製到PyGame中的屏幕外顯示

  • 繪製到隨後被繪製到屏幕在正確的位置
  • 保存可見顯示的部分爲可變

我試圖通過一些讀取圖像的閉屏圖像變的PyGame文檔,但我沒有取得任何成功。

謝謝!

回答

2

創建一個額外的表面對象,並繪製到它是解決方案。然後可以將該表面對象繪製到顯示器的表面對象上,如下所示。

的pygame的表面對象的更多信息可以發現here

import pygame, sys 

SCREEN_SIZE = (600, 400) 
BG_COLOR = (0, 0, 0) 
LINE_COLOR = (0, 255, 0) 
pygame.init() 
clock = pygame.time.Clock() # to keep the framerate down 

image1 = pygame.Surface((50, 50)) 
image2 = pygame.Surface((50, 50)) 
image1.set_colorkey((0, 0, 0)) # The default background color is black 
image2.set_colorkey((0, 0, 0)) # and I want drawings with transparency 

screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32) 
screen.fill(BG_COLOR) 

# Draw to two different images off-screen 
pygame.draw.line(image1, LINE_COLOR, (0, 0), (49, 49)) 
pygame.draw.line(image2, LINE_COLOR, (49, 0), (0, 49)) 

# Optimize the images after they're drawn 
image1.convert() 
image2.convert() 

# Get the area in the middle of the visible screen where our images would fit 
draw_area = image1.get_rect().move(SCREEN_SIZE[0]/2 - 25, 
            SCREEN_SIZE[1]/2 - 25) 

# Draw our two off-screen images to the visible screen 
screen.blit(image1, draw_area) 
screen.blit(image2, draw_area) 

# Display changes to the visible screen 
pygame.display.flip() 

# Keep the window from closing as soon as it's finished drawing 
# Close the window gracefully upon hitting the close button 
while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit(0) 
    clock.tick(30)