Pygame的draw模块介绍

本文将主要介绍Pygame的draw模块,主要内容翻译自pygame的官方文档 http://www.pygame.org/docs/ref/draw.html

先从整体来看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绘制一系列平滑的线

函数原型:

1、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 同样可以用来绘制填充矩形。
2、pygame.draw.polygon
  原型:pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect
  用途:polygon是多边形,这个函数和rect类似,除了第三个参数。顾名思义,pointlist是一个坐标点的列表,表示多边形的各个顶点。
3、pygame.draw.circle
  原型:pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
  用途:用于绘制圆形。第三个参数pos是圆心的位置坐标,radius指定了圆的半径。
4、pygame.draw.ellipse
  原型:pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect
  用途:ellipse是椭圆形,这个函数在矩形 Rect 内部绘制一个内接椭圆。
5、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弧度的起点是右边的中点处。
6、pygame.draw.line
  原型:pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

用途:绘制直线段,start_pos 和 end_pos 分别表示起始点和终止点,用坐标表示。width为线条宽度,默认为1. 线条两端自然结束,没有明显的端点(如实心黑点)。

7、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.)。

8、pygame.draw.aaline

原型:pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect

用途:绘制一条平滑的(消除锯齿)直线段。

9、pygame.draw.aalines

原型:pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

用途:绘制连续的抗锯齿线段。该函数还有上面的aaline的用法和前两个类似。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#模块导入 import pygame,sys from pygame.locals import* #初始化pygame pygame.init() #设置窗口大小,单位是像素 screen = pygame.display.set_mode((500,400)) #设置背景颜色 screen.fill((0,0,0)) #设置窗口标题 pygame.display.set_caption("你好,我的朋友") # 绘制一条线 pygame.draw.rect(screen, (0,0,0), [0,100,70,40]) #加载图片 img = pygame.image.load("panda.jpg") #初始化图片位置 imgx = 0 imgy = 10 #加载和播放音频 sound = pygame.mixer.Sound('Sound_Of_The_Sea.ogg') sound.play() #加载背景音乐 pygame.mixer.music.load('TEST1.mp3') #播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒) pygame.mixer.music.play(-1, 30.0) #导入文字格式 fontt=pygame.font.Font(None,50) #配置文字 tex=fontt.render("It is boring!!!",True,(0,0,128),(0,255,0)) #显示文字及坐标 texr=tex.get_rect() texr.center=(10,250) #初始化方向 dire = "right" #设置循环 while 1: #绘制文字 screen.blit(tex,texr) screen.fill((0,0,0)) screen.blit(img,(imgx,imgy)) if dire == "right": imgx+=5 if imgx == 380: dire = 'down' elif dire == 'down': imgy += 5 if imgy == 300: dire = 'left' elif dire == 'left': imgx -= 5 if imgx == 10: dire = 'up' elif dire == 'up': imgy -= 5 if imgy == 10: dire = 'right' #获取事件 for ss in pygame.event.get(): #判断事件 if ss.type == QUIT: #退出Pygame pygame.quit() #退出系统 sys.exit() #绘制屏幕内容 pygame.display.update() #设置帧率 pygame.time.delay(10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值