Pygame模块之pygame.draw

  本文将主要介绍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

  原型: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

  原型: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 """ 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()

 

运行效果:

posted @ 2012-07-01 20:49  李林克斯  阅读(33714)  评论(0编辑  收藏  举报