前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python之pygame学习绘制基本图形(3)

Python之pygame学习绘制基本图形(3)

作者头像
萌海无涯
发布2019-08-14 20:27:39
4K0
发布2019-08-14 20:27:39
举报
文章被收录于专栏:萌海无涯萌海无涯

pygame绘制

?

这次来说下怎么绘制基本的图形,如矩形,圆,直线等等

这图片的代码在最最最下面!!

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,则将填充形状(实线)。

所有绘图功能都遵循表面的剪辑区域,并将限制在该区域。这些函数返回一个矩形,表示已更改像素的边界区域。此边界矩形是包含受影响区域的“最小”边界框。

绘制矩形

代码语言:javascript
复制
rect(表面,颜色,矩形) - > Rect
rect(surface,color,rect,width = 0) - > Rect

参数说明:

  • surface(Surface)
    • 绘制表面
  • color(Color或int 或tuple(int ,int ,int ,[ int])
    • 要绘制的颜色,使用元组(上篇讲过背景色相同),或者使用英文单词的如red之类的可用单词
  • rect(Rect) - 要绘制的矩形,位置和尺寸
    • 要绘制的矩形,位置和尺寸
  • width(int)
    • (可选)用于线条粗细或表示要填充矩形(不要与rect参数的宽度值混淆)
    • 如果 width == 0 (默认)则填充内部
    • 如果 width > 0 则表示线条粗细
    • 如果使用width,边框的宽度不是很好控制
  • 返回:一个矩形边界变化的像素,如果没有绘制任何东西,返回宽高为0代码演示:
代码语言:javascript
复制
  pygame.draw.rect(screen,clock,(20,20,50,20),1)

绘制一个多边形

代码语言:javascript
复制
polygon(surface,color,points) - > Rect
polygon(surface,color,points,width = 0) - > Rect

参数:

  • surface(Surface)
  • 绘制表面
  • color(Color或int 或tuple(int ,int ,int ,[ int])
    • 要绘制的颜色,使用元组(r,g,b)或者英文单词red之类可用的单词
  • points(元组(坐标)或列表(坐标))
    • 构成多边形顶点的3个或更多(x,y)坐标的序列,序列中的每个坐标必须是元组/列表/
  • width(int)
    • (可选)用于线条粗细或表示要填充矩形(不要与rect参数的宽度值混淆)
    • 如果 width == 0 (默认)则填充内部是实心的内部全部填充颜色
    • 如果 width > 0 则表示线条粗细,空心的代表线条宽度
    • 如果使用width,边框的宽度不是很好控制
  • 返回:一个矩形边界变化的像素,如果没有绘制任何东西,返回宽高为0代码演示:
代码语言:javascript
复制
    # 绘制一个多边形
    pygame.draw.polygon(screen, clock, [(100, 100), (150, 100), (125,150),(100, 100)], 0)

绘制一个园

代码语言:javascript
复制
圆(表面,颜色,中心,半径) - >矩形

圆(表面,颜色,中心,半径,宽度= 0) - >矩形

参数:

  • 表面:与矩形相同
  • 颜色:与矩形相同
  • 中心:元祖或列表指定一个点作为园的中心
  • 半径:圆的半径,半径为0则显示一个像素
  • 宽度:与矩形相同
  • 返回:与矩形相同代码:
代码语言:javascript
复制
# 画一个正圆
pygame.draw.circle(screen,clock,(200,100),30)

绘制一个椭圆

代码语言:javascript
复制
绘制一个椭圆
椭圆(表面,颜色,矩形) - > Rect
ellipse(surface,color,rect,width = 0) - > Rect

参数:

  • 表面:与矩形相同
  • 颜色:与矩形相同
  • 矩形:用于指定椭圆的位置和尺寸的矩形,矩形限制椭圆的大小
  • 宽度:与矩形相同
  • 返回:与矩形相同
代码语言:javascript
复制
# 绘制一个椭圆
pygame.draw.ellipse(screen,clock,(150,150,100,50),1)

画一个椭圆弧

代码语言:javascript
复制
画一个椭圆弧
arc(surface,color,rect,start_angle,stop_angle) - > Rect
arc(surface,color,rect,start_angle,stop_angle,width = 1) - > Rect

参数:

  • 表面:与矩形相同
  • 颜色:与矩形相同
  • 矩形:绘制的位置和尺寸,椭圆将在矩形内居中
  • 起始角度:以弧度为单位的弧起始角度
  • 停止角度:以弧度为单位的弧的停止角度
    • 弧从哪顺时针方向绘制到结束方向
    • 起始角度小时停止角度,或超过起始角度360度后不会绘制内容
    • 如果起始起始角度等于停止则什么
代码语言:javascript
复制
    # 画一个椭圆弧
    # 这个我没看懂!
    # 官方代码!
from math import pi
pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2)
pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2)
pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2)
pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)

画一条直线

代码语言:javascript
复制
line(surface,color,start_pos,end_pos,width) - > Rect
line(surface,color,start_pos,end_pos,width = 1) - > Rect

在给定曲面上绘制一条直线。没有端盖。对于粗线,末端是方形的。参数:

  • 表面:同矩形
  • 颜色:同矩形
  • 起始点:指定一个起始位置 元组或列表
  • 终止点:指定一个终止位置 元组或列表
  • 宽度:同矩形
  • 返回:同矩形
代码语言:javascript
复制
# # 画一条直线pygame.draw.line(screen,clock,(100,200),(100,300))

绘制多个连续的直线段

代码语言:javascript
复制
线条(表面,颜色,封闭,点) - > Rect
线条(表面,颜色,闭合,点,宽度= 1) - > Rect

在给定曲面上绘制一系列连续的直线。没有端盖或斜接接头。对于粗线,末端是方形的。用尖角绘制粗线会产生不希望的效果。参数:

  • 表面:同矩形
  • 颜色:同矩形
  • 封闭:是否闭合,起点与结束点
  • 点:元组,或列表绘制一个个的点,如果闭合为真,则起点与终点自动闭合
  • 宽度:同矩形返回:同矩形
代码语言:javascript
复制
# 绘制多个连续的直线段
points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)]
pygame.draw.lines(screen,clock,1,points,1)

绘制抗锯齿线段

代码语言:javascript
复制
aaline(surface,color,start_pos,end_pos) - > Rect
aaline(surface,color,start_pos,end_pos,blend = 1) - > Rect

在给定曲面上绘制直线抗锯齿线。所有参数同绘制直线

代码语言:javascript
复制
# 绘制直线抗锯齿线
pygame.draw.aaline(screen,clock,(300,300),(500,500))

绘制多个连续的直线抗锯齿线段

代码语言:javascript
复制
aalines(表面,颜色,封闭,点) - > Rect
aalines(surface,color,closed,points,blend = 1) - > Rect

在给定曲面上绘制一系列连续的直线抗锯齿线所有参数同绘制多个连续的直线段

代码语言:javascript
复制
# 绘制多个连续的直线抗锯齿线段
points1= [(500, 75), (600, 25), (700, 75), (750, 25), (750, 125), (700, 75), (600, 125)]
pygame.draw.aalines(screen,clock,1,points1)

我测试的代码:

代码语言:javascript
复制
import pygamefrom math import pi
def rungame():    # 分辨率 800 * 600    win = (800, 600)


    # 一般情况我们都只是设置一个固定大小的窗口    screen = pygame.display.set_mode(win)    # 设置当前窗口标题    pygame.display.set_caption('绘制基础图像!')    # 设置窗口的背景色    screen.fill((0, 0, 0))    clock = (255,255,255)    # 绘制矩形    # 显示窗口,(颜色),(左,顶,宽,高)    pygame.draw.rect(screen,clock,(20,20,50,20),1)    # 或者((左,上),(宽,高))    pygame.draw.rect(screen,clock,((40,60),(20,20)),0)    # 绘制一个多边形    pygame.draw.polygon(screen, clock, [(100, 100), (150, 100), (125,150),(100, 100)], 0)    # 画一个圆圈    pygame.draw.circle(screen,clock,(200,100),30)    # 绘制一个椭圆    pygame.draw.ellipse(screen,clock,(150,150,100,50),1)    # 画一个椭圆弧    # pygame.draw.arc(screen,clock,(580,700,100,300),0,2 / pi,3)    # 画一条直线    pygame.draw.line(screen,clock,(100,200),(100,300))    # # 绘制多个连续的直线段    points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)]    pygame.draw.lines(screen,clock,1,points,1)    # 绘制直线抗锯齿线    pygame.draw.aaline(screen,clock,(300,300),(500,500))    # 绘制多个连续的直线抗锯齿线段    points1= [(500, 75), (600, 25), (700, 75), (750, 25), (750, 125), (700, 75), (600, 125)]    pygame.draw.aalines(screen,clock,1,points1)    # 开启一个事件循环处理发生的事件    while True:        #clock.tick(60)        # 从消息队列中获取事件并对事件进行处理        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                exit(0)        # 重绘背景

        pygame.display.update()

def main():    # 初始化导入的pygame中的模块    pygame.init()    rungame()

if __name__ == "__main__":    main()

官方给的演示代码:

代码语言:javascript
复制
# Import a library of functions called 'pygame'import pygamefrom math import pi
# Initialize the game enginepygame.init()
# Define the colors we will use in RGB formatBLACK = (0, 0, 0)WHITE = (255, 255, 255)BLUE = (0, 0, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)
# Set the height and width of the screensize = [400, 300]screen = pygame.display.set_mode(size)
pygame.display.set_caption("Example code for the draw module")
# Loop until the user clicks the close button.done = Falseclock = pygame.time.Clock()
while not done:
    # This limits the while loop to a max of 10 times per second.    # Leave this out and we will use all CPU we can.    clock.tick(10)
    for event in pygame.event.get():  # User did something        if event.type == pygame.QUIT:  # If user clicked close            done = True  # Flag that we are done so we exit this loop
    # All drawing code happens after the for loop and but    # inside the main while done==False loop.
    # Clear the screen and set the screen background    screen.fill(WHITE)
    # Draw on the screen a GREEN line from (0,0) to (50.75)    # 5 pixels wide.    pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5)
    # Draw on the screen a GREEN line from (0,0) to (50.75)    # 5 pixels wide.    pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
    # Draw on the screen a GREEN line from (0,0) to (50.75)    # 5 pixels wide.    pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True)
    # Draw a rectangle outline    pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
    # Draw a solid rectangle    pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
    # Draw an ellipse outline, using a rectangle as the outside boundaries    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
    # Draw an solid ellipse, using a rectangle as the outside boundaries    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])
    # This draws a triangle using the polygon command    pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
    # Draw an arc as part of an ellipse.    # Use radians to determine what angle to draw.    pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2)    pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2)    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2)    pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)
    # Draw a circle    pygame.draw.circle(screen, BLUE, [60, 250], 40)
    # Go ahead and update the screen with what we've drawn.    # This MUST happen after all the other drawing commands.    pygame.display.flip()
# Be IDLE friendlypygame.quit()
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-08,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 萌海无涯 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 绘制矩形
  • 绘制一个多边形
  • 绘制一个园
  • 绘制一个椭圆
  • 画一个椭圆弧
  • 画一条直线
  • 绘制多个连续的直线段
  • 绘制抗锯齿线段
  • 绘制多个连续的直线抗锯齿线段
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com