2016-06-13 2066 views
-1

我是Python的初學者,我不清楚函數surface.blit()方法。它有什麼作用?怎麼運行的? 我已經討論瞭如何創建它。python中的surface.blit()函數是什麼?它有什麼作用?怎麼運行的?

  • 創建期望的尺寸
  • 的畫布創建更小的尺寸的包含對象的表面被顯示。
  • 定義曲面的Rect值。
  • BLIT(重疊)在RECT位置

語法在畫布上表面:canvas.blit(表面,surfacerect)

爲什麼只使用矩形可以在是任何其它形狀

+0

也許[該文檔(http://www.pygame.org/docs/ref/surface.html#pygame.Surface。blit)可以有一定的幫助 – Hamms

+0

我已經閱讀過它們......但我需要一個簡單的解釋... –

+0

調用'canvas.blit(surface,surfacerect)'將'surface'繪製到'canvas'的位置由'facerect'的左上角指示。你的具體問題是什麼? – Hamms

回答

2

實質把這可能會幫助,但簡單地把儘可能 - >的blitting是借鑑

通過每一個你所提到的步驟去:

  • 創建所需大小

的畫布這是我們的窗口,由screen = pygame.display.set_mode((width,height))創建。其中screen是畫布名稱。最終,所有東西都需要繪製到這個畫布上,以便我們看到它。

  • 創建更小的尺寸的包含對象的表面將被顯示

這是一個表面,我們將與對象,例如圖像填充。它不需要小於窗口大小,並且可以自由移動。

  • 當您創建使用類似background = pygame.Surface((width,height))表面指定它的大小定義面

的矩形值。圖像或表面上繪製的項目可以是任何形狀或大小,但必須全部包含在由此寬度和高度設置的邊界內。

  • BLIT(重疊)在RECT位置

現在所有重要位在畫布上的表面上。我們需要獲得這個表面(背景)並將其繪製到窗口上。爲此,我們將調用screen.blit(background,(x,y))其中(x,y)是我們希望表面左上角的窗口內的位置。該功能表示將背景表面繪製到屏幕上並將其放置在(x,y)處。

一個簡單的例子:

import pygame 

pygame.init() 

#### Create a canvas on which to display everything #### 
window = (400,400) 
screen = pygame.display.set_mode(window) 
#### Create a canvas on which to display everything #### 

#### Create a surface with the same size as the window #### 
background = pygame.Surface(window) 
#### Create a surface with the same size as the window #### 

#### Populate the surface with objects to be displayed #### 
pygame.draw.rect(background,(0,255,255),(20,20,40,40)) 
pygame.draw.rect(background,(255,0,255),(120,120,50,50)) 
#### Populate the surface with objects to be displayed #### 

#### Blit the surface onto the canvas #### 
screen.blit(background,(0,0)) 
#### Blit the surface onto the canvas #### 

#### Update the the display and wait #### 
pygame.display.flip() 
done = False 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
#### Update the the display and wait #### 

pygame.quit() 
+0

謝謝,這已經使我的概念清晰了...... :-) –

相關問題