《pygame游戏开发实战指南》第五节 绘制几何图形

跟几何图形绘制相关的函数都在pygame.draw模块中,本节将详细介绍该模块中的各个绘制函数。

项目代码下载地址: https://github.com/la-vie-est-belle/pygame_codes


5.1 绘制直线

如果要绘制单条直线,可以使用下面的line()aaline()函数。

line(surface, color, start_pos, end_pos, width=1)
  • surface: 在哪个Surface对象上绘制直线。
  • color: 直线颜色,RGB格式或者RGBA格式。
  • start_pos: 直线开始坐标(x, y)。
  • end_pos: 直线结束坐标(x, y)。
  • width: 直线宽度,默认是1。

aaline(surface, color, start_pos, end_pos)

aaline()函数名字中的aa表示抗锯齿(anti-aliasing),它是一种消除显示器输出的画面中图物边缘出现凹凸锯齿的技术。

  • surface: 在哪个Surface对象上绘制抗锯齿直线。
  • color: 直线颜色,RGB格式或者RGBA格式。
  • start_pos: 直线开始坐标(x, y)。
  • end_pos: 直线结束坐标(x, y)。

注:aaline()函数没有width参数。


如果要绘制多条直线,可以使用lines()aalines()函数。

lines(surface, color, closed, points, width=1)
  • surface: 在哪个Surface对象上绘制直线。
  • color: 直线颜色,RGB格式或者RGBA格式。
  • closed: 如果设置为True,那么会将points中第一个点和最后一个点用直接连接起来使图形封闭起来。
  • points: 坐标点列表,相邻两个坐标点会被直线连接起来。
  • width: 直线宽度,默认是1。

aalines(surface, color, closed, points)
  • surface: 在哪个Surface对象上绘制抗锯齿直线。
  • color: 直线颜色,RGB格式或者RGBA格式。
  • closed: 如果设置为True,那么会将points中第一个点和最后一个点用直接连接起来使图形封闭起来。
  • points: 坐标点列表,相邻两个坐标点会被直线连接起来。

在示例代码5-1中,我们分别通过line()lines()函数绘制了一个三角形。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.line(screen, (255, 0, 0), (100, 100), (50, 150), width=2)       # 1
    pygame.draw.line(screen, (255, 0, 0), (50, 150), (150, 150), width=2)
    pygame.draw.line(screen, (255, 0, 0), (150, 150), (100, 100), width=2)

    pygame.draw.lines(screen, (0, 255, 0), True, [(200, 200), (150, 250), (250, 250)], width=2)  # 2
    pygame.display.flip()

运行结果如下:
示例代码5-1运行结果

代码解释如下:
#1 调用line()函数,绘制三角形的三条边,线条颜色为红色,宽度为2像素。

#2 调用lines()函数,传入三角形的三个点,颜色设置为绿色,宽度设置为2像素,并将closed参数设置为True表示将第一个点和最后一个点用直线连接起来。如果closed参数设置为False,则运行结果如下所示:
将closed设置为False


5.2 绘制矩形

rect(surface, color, rect, width=0, border_radius=0)
  • surface: 在哪个Surface对象上绘制矩形。
  • color: 矩形颜色,RGB格式或者RGBA格式。
  • rect: 所在的矩形区域,可以传入一个Rect对象,也可以直接传入(x, y, width, height)这个整数元组。
  • width: 矩形边框的粗细。默认是0,表示没有没有边框,此时矩形区域会被color参数指定的颜色所填充,相当于调用了pygame.Surface.fill()函数。
  • border_radius: 矩形圆角程度,默认是0,表示没有圆角。该参数接收的值范围为[0, min(height, width/2)]

在示例代码5-2中,我们rect()函数绘制了两个矩形。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (255, 0, 0), rect_area)     # 1
    pygame.draw.rect(screen, (0, 255, 0), (100, 100, 100, 100), width=0, border_radius=10)
    pygame.draw.rect(screen, (0, 0, 255), (200, 200, 100, 100), width=1, border_radius=20)
    pygame.display.flip()

运行结果如下:
示例代码5-2运行结果
代码解释如下:
#1 调用rect()函数绘制了3个矩形,第一个矩形为红色,绘制区域为(0, 0, 100, 100)。边框粗细为0像素,所以矩形被红色所填充。第二个矩形为绿色,圆角值为10像素。第三个矩形为蓝色,边框粗细为1像素,圆角为20像素,矩形没有被颜色所填充。


5.3 绘制多边形

polygon(surface, color, points, width=0)
  • surface: 在哪个Surface对象上绘制多边形。
  • color: 多边形颜色,RGB格式或者RGBA格式。
  • points: 多边形的各个顶点坐标。
  • width: 多边形边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了pygame.Surface.fill()函数。

在示例代码5-3中,我们调用polygon()函数绘制了两个多边形。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.polygon(screen, (255, 0, 0), [(30, 400), (60, 80), (400, 380)])     # 1
    pygame.draw.polygon(screen, (0, 255, 0), [(450, 10), (450, 450), (300, 200), (400, 30)], width=1)
    pygame.display.flip()

运行结果如下:
示例代码5-3运行结果

代码解释如下:
#1 调用polygon()函数绘制了一个三角形和一个四边形。三角形边框粗细为0,所以会被红色填充。


5.4 绘制圆形和椭圆形

circle(surface, color, center, radius, width=0)
  • surface: 在哪个Surface对象上绘制圆形。
  • color: 圆形颜色,RGB格式或者RGBA格式。
  • center: 圆心坐标。
  • radius: 圆半径。
  • width: 圆形边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了pygame.Surface.fill()函数。

ellipse(surface, color, rect, width=0)
  • surface: 在哪个Surface对象上绘制椭圆形。
  • color: 椭圆颜色,RGB格式或者RGBA格式。
  • rect: 椭圆所在的矩形区域。
  • width: 椭圆边框的粗细。默认是0,表示没有没有边框,此时多边形会被color参数指定的颜色所填充,相当于调用了pygame.Surface.fill()函数。

在示例代码5-4中,我们分别调用circle()函数和ellipse()函数绘制了一个圆和一个椭圆。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.circle(screen, (0, 0, 255), (200, 200), 50)     # 1
    pygame.draw.ellipse(screen, (0, 255, 0), (300, 300, 50, 100), width=2)
    pygame.display.flip()

运行结果如下:
示例代码5-4运行结果

代码解释如下:
#1 调用circle()函数时,我们将圆心坐标设置为(200, 200),半径设置为50,边框粗细为0,所以会被蓝色填充。调用ellipse()函数时,我们将它的矩形区域设置为(300, 300, 50, 100),边框粗细设置为2


5.5 绘制弧形

arc(surface, color, rect, start_angle, stop_angle, width=1)
  • surface: 在哪个Surface对象上绘制弧形。
  • color: 弧形颜色,RGB格式或者RGBA格式。
  • rect: 弧形所在矩形区域。
  • start_angle: 初始弧度,可接收float类型值,范围是[0, 2π]
  • stop_angle: 终止弧度,可接收float类型值,范围是[0, 2π]
  • width: 弧形边框的粗细。默认是1像素。

在示例代码5-5中,我们分别调用arc()函数绘制了两个弧形。

import sys
import math
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.arc(screen, (255, 0, 0), (10, 10, 100, 100), 0, math.pi)    # 1
    pygame.draw.arc(screen, (0, 0, 255), (10, 10, 100, 100), math.pi, math.pi*2)
    pygame.display.flip()

运行结果如下:
示例代码5-5运行结果

代码解释如下:
#1 调用arc()函数,绘制了两个半圆,颜色分别为红色和蓝色,边框粗细都为默认的1像素。


5.6 小结

在本节,我们学习了如何使用pygame.draw模块中的函数来绘制各种几何图形。

  • line(): 绘制直线。
  • aaline(): 绘制抗锯齿直线。
  • lines(): 绘制多条直线。
  • aalines(): 绘制多条抗锯齿直线。
  • rect(): 绘制矩形。
  • polygon(): 绘制多边形。
  • circle(): 绘制圆形。
  • ellipse(): 绘制椭圆。
  • arc(): 绘制弧形。

如果你喜欢笔者的这部专栏,可以给笔者一些打赏,或者通过购买一些项目来支持作者,非常感谢!

微信支付   支付宝   微店
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la_vie_est_belle

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值