跟几何图形绘制相关的函数都在pygame.draw
模块中,本节将详细介绍该模块中的各个绘制函数。
项目代码下载地址: https://github.com/la-vie-est-belle/pygame_codes
5.1 绘制直线
如果要绘制单条直线,可以使用下面的line()
和aaline()
函数。
line(surface, color, start_pos, end_pos, width=1)
- surface: 在哪个
Surface
对象上绘制直线。 - color: 直线颜色,RGB格式或者RGBA格式。
- start_pos: 直线开始坐标(x, y)。
- end_pos: 直线结束坐标(x, y)。
- width: 直线宽度,默认是1。
aaline(surface, color, start_pos, end_pos)
aaline()
函数名字中的aa表示抗锯齿(anti-aliasing),它是一种消除显示器输出的画面中图物边缘出现凹凸锯齿的技术。
- surface: 在哪个
Surface
对象上绘制抗锯齿直线。 - color: 直线颜色,RGB格式或者RGBA格式。
- start_pos: 直线开始坐标(x, y)。
- end_pos: 直线结束坐标(x, y)。
注:aaline()函数没有width参数。
如果要绘制多条直线,可以使用lines()
和aalines()
函数。
lines(surface, color, closed, points, width=1)
- surface: 在哪个
Surface
对象上绘制直线。 - color: 直线颜色,RGB格式或者RGBA格式。
- closed: 如果设置为True,那么会将points中第一个点和最后一个点用直接连接起来使图形封闭起来。
- points: 坐标点列表,相邻两个坐标点会被直线连接起来。
- width: 直线宽度,默认是1。
aalines(surface, color, closed, points)
- surface: 在哪个
Surface
对象上绘制抗锯齿直线。 - color: 直线颜色,RGB格式或者RGBA格式。
- closed: 如果设置为True,那么会将points中第一个点和最后一个点用直接连接起来使图形封闭起来。
- points: 坐标点列表,相邻两个坐标点会被直线连接起来。
在示例代码5-1中,我们分别通过line()
和lines()
函数绘制了一个三角形。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.draw.line(screen, (255, 0, 0), (100, 100), (50, 150), width=2) # 1
pygame.draw.line(screen, (255, 0, 0), (50, 150), (150, 150), width=2)
pygame.draw.line(screen, (255, 0, 0), (150, 150), (100, 100), width=2)
pygame.draw.lines(screen, (0, 255, 0), True, [(200, 200), (150, 250), (250, 250)], width=2) # 2
pygame.display.flip()
运行结果如下:
代码解释如下:
#1 调用line()
函数,绘制三角形的三条边,线条颜色为红色,宽度为2
像素。
#2 调用lines()
函数,传入三角形的三个点,颜色设置为绿色,宽度设置为2
像素,并将closed
参数设置为True表示将第一个点和最后一个点用直线连接起来。如果closed
参数设置为False,则运行结果如下所示:
5.2 绘制矩形
rect(surface, color, rect, width=0, border_radius=0)
- surface: 在哪个
Surface
对象上绘制矩形。 - color: 矩形颜色,RGB格式或者RGBA格式。
- rect: 所在的矩形区域,可以传入一个
Rect
对象,也可以直接传入(x, y, width, height)
这个整数元组。 - width: 矩形边框的粗细。默认是0,表示没有没有边框,此时矩形区域会被color参数指定的颜色所填充,相当于调用了
pygame.Surface.fill()
函数。 - border_radius: 矩形圆角程度,默认是0,表示没有圆角。该参数接收的值范围为
[0, min(height, width/2)]
。
在示例代码5-2中,我们rect()
函数绘制了两个矩形。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')
rect_area = pygame.Rect(0, 0, 100, 100)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.draw.rect(screen, (255, 0, 0), rect_area) # 1
pygame.draw.rect(screen, (0, 255, 0), (100, 100, 100, 100), width=0, border_radius=10)
pygame.draw.rect(screen, (0, 0, 255), (200, 200, 100, 100), width=1, border_radius=20)
pygame.display.flip()
运行结果如下:
代码解释如下:
#1 调用rect()
函数绘制了3个矩形,第一个矩形为红色,绘制区域为(0, 0, 100, 100)
。边框粗细为0
像素,所以矩形被红色所填充。第二个矩形为绿色,圆角值为10
像素。第三个矩形为蓝色,边框粗细为1
像素,圆角为20
像素,矩形没有被颜色所填充。
5.3 绘制多边形
polygon(surface, color, points, width=0)
- surface: 在哪个
Surface
对象上绘制多边形。 - color: 多边形颜色,RGB格式或者RGBA格式。
- points: 多边形的各个顶点坐标。
- width: 多边形边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了
pygame.Surface.fill()
函数。
在示例代码5-3中,我们调用polygon()
函数绘制了两个多边形。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')
rect_area = pygame.Rect(0, 0, 100, 100)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.draw.polygon(screen, (255, 0, 0), [(30, 400), (60, 80), (400, 380)]) # 1
pygame.draw.polygon(screen, (0, 255, 0), [(450, 10), (450, 450), (300, 200), (400, 30)], width=1)
pygame.display.flip()
运行结果如下:
代码解释如下:
#1 调用polygon()
函数绘制了一个三角形和一个四边形。三角形边框粗细为0,所以会被红色填充。
5.4 绘制圆形和椭圆形
circle(surface, color, center, radius, width=0)
- surface: 在哪个
Surface
对象上绘制圆形。 - color: 圆形颜色,RGB格式或者RGBA格式。
- center: 圆心坐标。
- radius: 圆半径。
- width: 圆形边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了
pygame.Surface.fill()
函数。
ellipse(surface, color, rect, width=0)
- surface: 在哪个
Surface
对象上绘制椭圆形。 - color: 椭圆颜色,RGB格式或者RGBA格式。
- rect: 椭圆所在的矩形区域。
- width: 椭圆边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了
pygame.Surface.fill()
函数。
在示例代码5-4中,我们分别调用circle()
函数和ellipse()
函数绘制了一个圆和一个椭圆。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')
rect_area = pygame.Rect(0, 0, 100, 100)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (0, 0, 255), (200, 200), 50) # 1
pygame.draw.ellipse(screen, (0, 255, 0), (300, 300, 50, 100), width=2)
pygame.display.flip()
运行结果如下:
代码解释如下:
#1 调用circle()
函数时,我们将圆心坐标设置为(200, 200)
,半径设置为50
,边框粗细为0
,所以会被蓝色填充。调用ellipse()
函数时,我们将它的矩形区域设置为(300, 300, 50, 100)
,边框粗细设置为2
。
5.5 绘制弧形
arc(surface, color, rect, start_angle, stop_angle, width=1)
- surface: 在哪个
Surface
对象上绘制弧形。 - color: 弧形颜色,RGB格式或者RGBA格式。
- rect: 弧形所在矩形区域。
- start_angle: 初始弧度,可接收float类型值,范围是
[0, 2π]
。 - stop_angle: 终止弧度,可接收float类型值,范围是
[0, 2π]
。 - width: 弧形边框的粗细。默认是1像素。
在示例代码5-5中,我们分别调用arc()
函数绘制了两个弧形。
import sys
import math
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')
rect_area = pygame.Rect(0, 0, 100, 100)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.draw.arc(screen, (255, 0, 0), (10, 10, 100, 100), 0, math.pi) # 1
pygame.draw.arc(screen, (0, 0, 255), (10, 10, 100, 100), math.pi, math.pi*2)
pygame.display.flip()
运行结果如下:
代码解释如下:
#1 调用arc()
函数,绘制了两个半圆,颜色分别为红色和蓝色,边框粗细都为默认的1
像素。
5.6 小结
在本节,我们学习了如何使用pygame.draw
模块中的函数来绘制各种几何图形。
- line(): 绘制直线。
- aaline(): 绘制抗锯齿直线。
- lines(): 绘制多条直线。
- aalines(): 绘制多条抗锯齿直线。
- rect(): 绘制矩形。
- polygon(): 绘制多边形。
- circle(): 绘制圆形。
- ellipse(): 绘制椭圆。
- arc(): 绘制弧形。
如果你喜欢笔者的这部专栏,可以给笔者一些打赏,或者通过购买一些项目来支持作者,非常感谢!