视频
Pygame模块之pygame.draw
本文将主要介绍Pygame的draw模块,主要内容翻译自pygame的官方文档
pygame.draw 模块用于在Surface上绘制一些简单的图形,比如点、直线、矩形、圆、弧等。
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
原型:pygame.draw.rect(Surface, color, Rect, width=0): return Rect
用途:在Surface上绘制矩形,color
参数是线条(或填充)的颜色,参数Rect的形式是((x, y), (width, height)),表示的是所绘制矩形的区域,其中第一个元组(x, y)表示的是该矩形左上角的坐标,第二个元组 (width, height)表示的是矩形的宽度和高度。width表示线条的粗细,单位为像素;默认值为0,表示填充矩形内部。
此外,Surface.fill 同样可以用来绘制填充矩形。
pygame.draw.polygon
原型:pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect
用途:polygon是多边形,这个函数和rect类似,除了第三个参数。顾名思义,pointlist是一个坐标点的列表,表示多边形的各个顶点。
pygame.draw.circle
原型:pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
用途:用于绘制圆形。第三个参数pos是圆心的位置坐标,radius指定了圆的半径。
pygame.draw.ellipse
原型:pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect
用途:ellipse是椭圆形,这个函数在矩形 Rect 内部绘制一个内接椭圆。
pygame.draw.arc
原型: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
原型:pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
用途:绘制直线段,start_pos 和 end_pos 分别表示起始点和终止点,用坐标表示。width为线条宽度,默认为1. 线条两端自然结束,没有明显的端点(如实心黑点)。
pygame.draw.lines
原型: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
原型:pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect
用途:绘制一条平滑的(消除锯齿)直线段。
pygame.draw.aalines
原型:pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect
用途:绘制连续的抗锯齿线段。该函数还有上面的aaline的用法和前两个类似。
示例1
绘制图形时,函数使用方法举例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import pygame from math import pi #初始化 pygame.init() # 设置主屏幕大小 size = ( 500 , 450 ) screen = pygame.display.set_mode(size) #设置标题 pygame.display.set_caption( "小小工坊" ) # 设置一个控制主循环的变量 done = False #创建时钟对象 clock = pygame.time.Clock() while not done: # 设置游戏的fps clock.tick( 10 ) for event in pygame.event.get(): if event. type = = pygame.QUIT: done = True # 若检测到关闭窗口,则将done置为True # 绘制一条宽度为 3 的红色对角线 pygame.draw.line(screen, ( 0 , 255 , 0 ), [ 0 , 0 ], ( 500 , 450 ), 3 ) # 绘制多条蓝色的直线(连续直线,非抗锯齿),False 表示首尾不相连 pygame.draw.lines(screen, ( 0 , 0 , 255 ), False , [[ 0 , 80 ], [ 50 , 90 ], [ 200 , 80 ], [ 220 , 30 ]], 1 ) # 绘制一个灰色的矩形区域,以灰色填充区域 pygame.draw.rect(screen, ( 155 , 155 , 155 ), ( 75 , 10 , 50 , 20 ), 0 ) # 绘制一个线框宽度为2的矩形区域 pygame.draw.rect(screen, ( 0 , 0 , 0 ), [ 150 , 10 , 50 , 20 ], 2 ) # 绘制一个椭圆形,其线宽为2 pygame.draw.ellipse(screen, ( 255 , 0 , 0 ), ( 225 , 10 , 50 , 20 ), 2 ) # 绘制一个实心的红色椭圆形 pygame.draw.ellipse(screen, ( 255 , 0 , 0 ), ( 300 , 10 , 50 , 20 )) # 绘制一个绿色边框(宽度为2)三角形 pygame.draw.polygon(screen, ( 100 , 200 , 45 ), [[ 100 , 100 ], [ 0 , 200 ], [ 200 , 200 ]], 2 ) # 绘制一个蓝色实心的圆形,其中[60,250]表示圆心的位置,40为半径,width默认为0 pygame.draw.circle(screen, ( 0 , 0 , 255 ), [ 60 , 250 ], 40 ) # 绘制一个圆弧,其中0表示弧线的开始位置,pi/2表示弧线的结束位置,2表示线宽 pygame.draw.arc(screen, ( 255 , 10 , 0 ), ( 210 , 75 , 150 , 125 ), 0 , pi / 2 , 2 ) # 刷新显示屏幕 pygame.display.flip() # 点击关闭,退出pygame程序 pygame.quit() |
示例2
绘制脸部图形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import pygame, sys from math import pi pygame.init() screen = pygame.display.set_mode(( 600 , 400 )) pygame.display.set_caption( "pygame 绘图" ) GOLD = 255 , 251 , 0 RED = pygame.Color( 'red' ) WHITE = 255 , 255 , 255 GREEN = pygame.Color( 'green' ) e1rect = pygame.draw.ellipse(screen,GREEN, ( 50 , 50 , 500 , 300 ), 3 ) c1rect = pygame.draw.circle(screen, GOLD, ( 200 , 180 ), 30 , 5 ) c2rect = pygame.draw.circle(screen, GOLD,( 400 , 180 ), 30 , 5 ) r1rect = pygame.draw.rect(screen, RED, ( 170 , 130 , 60 , 10 ), 3 ) r2rect = pygame.draw.rect(screen, RED, ( 370 , 130 , 60 , 10 )) plist = [( 295 , 170 ), ( 285 , 250 ),( 260 , 280 ),( 340 , 280 ),( 315 , 250 ),( 305 , 170 )] l1rect = pygame.draw.lines(screen,GOLD, True ,plist, 2 ) a1rect = pygame.draw.arc(screen,RED,( 200 , 220 , 200 , 100 ), 1.4 * pi, 1.9 * pi, 3 ) while True : for e in pygame.event.get(): if e. type = = pygame.QUIT: pygame.quit() sys.exit() pygame.display.update() |
到此这篇关于Python使用draw类绘制图形示例讲解的文章就介绍到这了,更多相关Python draw类内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://acktomas.blog.csdn.net/article/details/125856587