本文將主要介紹Pygame的draw模塊,主要內容翻譯自pygame的官方文檔 http://www.pygame.org/docs/ref/draw.html
pygame.draw 模塊用於在Surface上繪制一些簡單的圖形,比如點、直線、矩形、圓、弧等。
下面這段話引自龍昌博客·Pygame學習筆記3:繪圖:pygame.draw中函數的第一個參數總是一個surface,然后是顏色,再后會是一系列的坐標等。稍有些計算機繪圖經驗的人就會知道,計算機里的坐標,(0,0)代表左上角。而返回值是一個Rect對象,包含了繪制的領域,這樣你就可以很方便的更新那個部分了。
先從整體來看pygame.draw有哪些函數:
- pygame.draw.rect: 繪制矩形
- pygame.draw.polygon: 繪制任意邊數的多邊形
- pygame.draw.circle: 繪制圓
- pygame.draw.ellipse: 在矩形內繪制橢圓
- pygame.draw.arc: 繪制圓弧(或者橢圓的一部分)
- pygame.draw.line: 繪制直線(線段)
- pygame.draw.lines: 從一個點列表中連續繪制直線段
- pygame.draw.aaline: 繪制一根平滑的線(反鋸齒)
- pygame.draw.aalines: 繪制一系列平滑的線
大多數函數接受一個width參數表示線條(畫筆)的寬度,如果該值設置為0,則表示填充整個圖形。
所有的繪制函數都會尊重指定的Surface編輯區,而且會限制在這個區域內。函數的返回值是一個Rect,表示的是受影響的Surface區域。(原文:All the drawing functions respect the clip area for the Surface, and will be constrained to that area. The functions return a rectangle representing the bounding area of changed pixels.)
顏色參數通常是一個RGB三元組(R, G, B)。也可以接受RGBA形式的顏色值。
這些繪制函數會臨時鎖定所操作的Surface對象。
原型:pygame.draw.rect(Surface, color, Rect, width=0): return Rect
用途:在Surface上繪制矩形,第二個參數是線條(或填充)的顏色,第三個參數Rect的形式是((x, y), (width, height)),表示的是所繪制矩形的區域,其中第一個元組(x, y)表示的是該矩形左上角的坐標,第二個元組 (width, height)表示的是矩形的寬度和高度。width表示線條的粗細,單位為像素;默認值為0,表示填充矩形內部。
此外,Surface.fill 同樣可以用來繪制填充矩形。
原型:pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect
用途:polygon是多邊形,這個函數和rect類似,除了第三個參數。顧名思義,pointlist是一個坐標點的列表,表示多邊形的各個頂點。
原型:pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
用途:用於繪制圓形。第三個參數pos是圓心的位置坐標,radius指定了圓的半徑。
原型:pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect
用途:ellipse是橢圓形,這個函數在矩形 Rect 內部繪制一個內接橢圓。
原型:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect
用途:繪制一段圓弧,或者其實是上面提到的橢圓的一部分。與ellipse函數相比,多了兩個參數:start_angle是該段圓弧的起始角度,stop_angle是終止角度。這兩個都是用弧度制來表示的,而原點就是矩形Rect的中心。在Rect平面上建立坐標系,原點是中心,簡單示意圖如下。0弧度的起點是右邊的中點處。
原型:pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
用途:繪制直線段,start_pos 和 end_pos 分別表示起始點和終止點,用坐標表示。width為線條寬度,默認為1. 線條兩端自然結束,沒有明顯的端點(如實心黑點)。
原型:pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect
用途:用於繪制一系列直線段。closed是一個布爾變量,如果closed為真,那么表示需要把第一點和最后一點連接起來。這些點來自pointlist,一個包含坐標點的列表。這個函數不會繪制線條的端點,也沒有斜角連接(miter joints),而且角度小和線條粗的連線看起來會有點奇怪( Lines with sharp corners and wide line widths can have improper looking corners.)。
原型:pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect
用途:繪制一條平滑的(消除鋸齒)直線段。
原型:pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect
用途:繪制連續的抗鋸齒線段。該函數還有上面的aaline的用法和前兩個類似。
應用
1 """ drawDemo.py 2 demonstrate using the drawing 3 features in pygame""" 4 5 import pygame, math 6 pygame.init() 7 8 def drawStuff(background): 9 """ given a surface, draws a bunch of things on it """ 10 11 #draw a line from (5, 100) to (100, 100) 12 pygame.draw.line(background, (255, 0, 0), (5, 100), (100, 100)) 13 14 #draw an unfilled square 15 pygame.draw.rect(background, (0, 255, 0), ((200, 5), (100, 100)), 3) 16 17 #draw a filled circle 18 pygame.draw.circle(background, (0, 0, 255), (400, 50), 45) 19 20 #draw an arc 21 pygame.draw.arc(background, (0, 0, 0), ((5, 150), (100, 200)), 0, math.pi/2, 5) 22 23 #draw an ellipse 24 pygame.draw.ellipse(background, (0xCC, 0xCC, 0x00), ((150, 150), (150, 100)), 0) 25 26 #draw lines, 27 points = ( 28 (370, 160), 29 (370, 237), 30 (372, 193), 31 (411, 194), 32 (412, 237), 33 (412, 160), 34 (412, 237), 35 (432, 227), 36 (436, 196), 37 (433, 230) 38 ) 39 pygame.draw.lines(background, (0xFF, 0x00, 0x00), False, points, 3) 40 41 #draw polygon 42 points = ( 43 (137, 372), 44 (232, 319), 45 (383, 335), 46 (442, 389), 47 (347, 432), 48 (259, 379), 49 (220, 439), 50 (132, 392) 51 ) 52 pygame.draw.polygon(background, (0x33, 0xFF, 0x33), points) 53 54 #compare normal and anti-aliased diagonal lines 55 pygame.draw.line(background, (0, 0, 0), (480, 425), (550, 325), 1) 56 pygame.draw.aaline(background, (0, 0, 0), (500, 425), (570, 325), 1) 57 58 def main(): 59 screen = pygame.display.set_mode((640, 480)) 60 pygame.display.set_caption("Drawing commands") 61 62 background = pygame.Surface(screen.get_size()) 63 background = background.convert() 64 background.fill((255, 255, 255)) 65 66 drawStuff(background) 67 68 clock = pygame.time.Clock() 69 keepGoing = True 70 while keepGoing: 71 clock.tick(30) 72 for event in pygame.event.get(): 73 if event.type == pygame.QUIT: 74 keepGoing = False 75 elif event.type == pygame.MOUSEBUTTONUP: 76 print pygame.mouse.get_pos() 77 screen.blit(background, (0, 0)) 78 pygame.display.flip() 79 80 if __name__ == "__main__": 81 main()
運行效果: