用繪圖模組繪圖
Pygame 有一個模組 pygame.draw
,它包含可以直接將形狀繪製到 Surface 的函式。
功能 | 描述 |
---|---|
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 | 繪製連線的抗鋸齒線序列 |
如何使用該模組
要使用該模組,首先需要正確匯入和初始化 pygame,併為顯示設定一個模式。提前定義顏色常量很方便,使你的程式碼更具可讀性和美觀性。所有函式都使用 Surface 繪製,顏色和位置引數可以是 pygame Rect 或 2 元素整數/浮點序列(由於未定義的原因,pygame.draw.circle
只會佔用整數)。
例
下面的程式碼將展示所有不同的功能,它們的使用方式和外觀。我們將在示例之前初始化 pygame 並定義一些常量。
import pygame
from math import pi
pygame.init()
screen = pygame.display.set_mode((100, 100))
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
黑色是 Surface 預設顏色,表示尚未繪製的 Surface 部分。每個功能的引數在下面的引數中解釋。
矩形
size = (50, 50)
rect_border = pygame.Surface(size) # Create a Surface to draw on.
pygame.draw.rect(rect_border, RED, rect_border.get_rect(), 10) # Draw on it.
rect_filled = pygame.Surface(size)
pygame.draw.rect(rect_filled, RED, rect_filled.get_rect())
多邊形
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)] # The corner points of the polygon.
polygon = pygame.Surface(size)
pygame.draw.polygon(polygon, RED, points, 10)
polygon_filled = pygame.Surface(size)
pygame.draw.polygon(polygon_filled, RED, points)
圈
size = (50, 50)
radius = 25
circle = pygame.Surface(size)
pygame.draw.circle(circle, RED, (radius, radius), radius, 10) # Position is the center of the circle.
circle_filled = pygame.Surface(size)
pygame.draw.circle(circle_filled, RED, (radius, radius), radius)
這些漏洞是 pygame 繪圖演算法的一個不幸結果。
橢圓
size = (50, 25) # Minimize it's height so it doesn't look like a circle.
ellipse = pygame.Surface(size)
pygame.draw.ellipse(ellipse, RED, ellipse.get_rect(), 5)
ellipse_filled = pygame.Surface(size)
pygame.draw.ellipse(ellipse_filled, RED, ellipse.get_rect())
這些漏洞是 pygame 繪圖演算法的一個不幸結果。
弧
size = (50, 50)
arc = pygame.Surface(size)
pygame.draw.arc(arc, RED, arc.get_rect(), 0, pi) # 0 to pi is 180° creating a half circle.
線
size = (50, 50)
line = pygame.Surface(size)
pygame.draw.line(line, RED, (0, 0), (50, 50)) # Start at topleft and ends at bottomright.
行
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)]
lines = pygame.Surface(size)
pygame.draw.lines(lines, RED, False, points)
lines_closed = pygame.Surface(size)
pygame.draw.lines(lines_closed, RED, True, points)
抗鋸齒線
size = (50, 50)
antialiased_line = pygame.Surface(size)
pygame.draw.aaline(antialiased_line, RED, (0, 0), (50, 50))
抗鋸齒線
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)]
antialiased_lines = pygame.Surface(size)
pygame.draw.aalines(antialiased_lines, RED, False, points)
antialiased_lines_closed = pygame.Surface(size)
pygame.draw.aalines(antialiased_lines_closed, RED, True, points)
試試看
要自己試一試:將上面的一個程式碼片段和下面的程式碼複製到一個空檔案中,將名稱影象更改為要進行 blit 和實驗的 Surface 的名稱。
import pygame
from math import pi
pygame.init()
screen = pygame.display.set_mode((100, 100))
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
# But code snippet here
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.blit(image, (25, 25))
pygame.display.update()