python pygame.draw模块

一、pygame.draw模块

1.1、pygame.draw.aaline() 画直线

解释:绘制一条平直的抗锯齿线
语法:
aaline(surface, color, start_pos, end_pos) -> Rect
aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
参数:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
start_pos:开始位置
end_pos:结束位置
#代码:
import pygame
from pygame.locals import *
from sys import exit

def main():
    pygame.init()
    screen=pygame.display.set_mode((600,400))  # 设置显示屏幕返回为surface对象
    pygame.display.set_caption('学习pygame库') # 设置标题
    pygame.display.set_icon(pygame.image.load('img/boom/boom01.png')) # 设置标题的图标
    while True:
        for event in pygame.event.get():  #pygame.event.get()返回Eventlist列表
            if event.type ==pygame.QUIT:  #
                pygame.quit()   # 退出pygame
                exit()          # 退出python解释器
        pygame.draw.aaline(screen,(255,255,255),(20,20),(100,100))  # draw模块的aaline方法
        pygame.display.update()  # 刷新
 
if __name__ == '__main__':
    main()

显示结果:
在这里插入图片描述

1.2、pygame.draw.aalines() 画多段线

解释:绘制多个连续的抗锯齿直线段
语法:
aalines(surface, color, closed, points) -> Rect
aalines(surface, color, closed, points, blend=1) -> Rect
参数解释:
surface:要绘制的surface 
color: 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
closed:若为True ,则在 points 序列的第一个点和最后一个点之间绘制一条附加线段
points : 一个含有2个或更多 (x, y) 坐标的序列(例如:[(x,y)(x1,y1)..(xn,yn)])
blend (int) -- (可选)如果非零(默认值),线条将与Surface的现有像素阴影混合,否则将覆盖它们
#代码:
 while True:
        for event in pygame.event.get():  #pygame.event.get()返回Eventlist列表
            if event.type ==pygame.QUIT:  #
                pygame.quit()   # 退出pygame
                exit()          # 退出python解释器
pygame.draw.aalines(screen, (255, 255, 255),True, [(20, 20), (60, 80),(100, 100)])
pygame.display.update()  # 刷新

输出结果:
在这里插入图片描述

1.3、pygame.draw.rect() 画矩形

解释:绘制矩形
语法:
rect(surface, color, rect) -> Rect
rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1) -> Rect
参数解释:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
rect:坐标tuple(left,top,width,height)或者((left,top),(width,height))
width=0:绘制矩形线条的宽度
border_radius=0:边界半径
border_top_left_radius=-1:
border_top_right_radius=-1:
border_bottom_left_radius=-1:
border_bottom_right_radius=-1:

代码;

    while True:
        for event in pygame.event.get(): 
            if event.type ==pygame.QUIT: 
                pygame.quit()  
                exit()    
		pygame.draw.rect(screen, (255, 0, 255),(20,20,60,80),1)
        pygame.draw.rect(screen, (255, 0, 0),(10,10,80,100),5)
        pygame.display.update()  # 刷新

显示结果:
在这里插入图片描述

1.4、pygame.draw.polygon() 画多边形

解释:绘制多边形
语法:
polygon(surface, color, points) -> Rect
polygon(surface, color, points, width=0) -> Rect
参数解释:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
points:点的列表[(X1,Y1)(X2,Y2)(X3Y3)...(X4,Y5)];列表元素大于2,几个点就表示几边形
width:可选参数,线条宽度

代码:

pygame.draw.polygon(screen, (255, 0, 0),[(20, 20),(200, 350),(500, 20)],5) #绘制三角形
pygame.draw.polygon(screen, (255, 0, 0),[(20, 20),(200, 150),(200, 350),(500, 20)],2)  #绘制不规则四边形
pygame.display.update()

在这里插入图片描述

1.5、pygame.draw.lines() 画多段线

解释:绘制多条连续直线段
语法:
lines(surface, color, closed, points) -> Rect
lines(surface, color, closed, points, width=1) -> Rect

与aalines()相同

1.6、pygame.draw.line() 画直线

解释:绘制一条直线
语法:
line(surface, color, start_pos, end_pos, width) -> Rect
line(surface, color, start_pos, end_pos, width=1) -> Rect

与aaline()相同

1.7、pygame.draw.ellipse() 画椭圆

解释:绘制椭圆
语法:
ellipse(surface, color, rect) -> Rect
ellipse(surface, color, rect, width=0) -> Rect
参数解释:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
rect:矩形矢量((x,y),(width,height))或者(x,y,width,height)

代码:

pygame.draw.ellipse(screen, (255, 255, 255), ((80, 80), (100, 200)),10)  # 绘制椭圆形
pygame.draw.ellipse(screen, (0, 0, 255), ((300, 80), (100, 100)),3) # 当width与height相等就是园
pygame.draw.rect(screen, (255, 0,0), ((80, 80), (100, 200)),1) #绘制的矩形
pygame.display.update() 

结果显示:
在这里插入图片描述

1.8、pygame.draw.circle() 画圆

解释:绘制圆
语法:
circle(surface, color, center, radius) -> Rect
circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None) -> Rect
参数解释:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
center:中心点,也就是圆心点坐标
radius:园的半径

代码:

        pygame.draw.circle(screen,(0, 255, 0),(400,200),100)
        pygame.display.update()

结果显示:
在这里插入图片描述

1.9、pygame.draw.arc() 画圆弧

解释:绘制椭圆弧
语法:
arc(surface, color, rect, start_angle, stop_angle) -> Rect
arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
参数解释:
surface:在什么surface对象上显示
color:颜色值(tuple(255,255,255) 或者 颜色名称“red”等)
rect:矩形矢量((x,y),(width,height))或者(x,y,width,height)
start_angle:起始弧度,不是int类型,可用math.radians(10)把int转换为弧度
stop_angle:结束弧度,不是int类型,可用math.radians(10)把int转换为弧度
import  math
start = math.radians(10)
end = math.radians(210)
pygame.draw.arc(screen,(0, 255, 0),(200,100,200,100),start,end,2)
pygame.display.update()

结果显示:
在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值