Pygame直线绘制

文章目录

pygame.draw中有4个绘制直线的函数,列表如下

一条线段 多条线段
正常 line lines
抗锯齿 aaline aalines

一条和多条线段的输入参数如下

  • line(surface, color, start_pos, end_pos, width=1)
  • lines(surface, color, closed, points, width=1)

lines

下面演示一下多条线段的用法,实现下面这种动感的随机直线生成窗口

代码如下

python 复制代码
import time
import numpy as np
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 320))

while True:
    if pygame.QUIT in [e.type for e in pygame.event.get()]:
        pygame.quit()
        break
    time.sleep(0.1)
    pts = (np.random.rand(10,2) * (640,320)).astype(int)
    c = (np.random.rand(3)*255).astype(int)
    screen.fill("black")
    pygame.draw.lines(screen, c, True, pts, 1)
    pygame.display.flip()

上面的代码中,lines用于生成随机直线,其5个参数中

  • screen可理解为绘制直线的画板
  • c即随机生成的三元组,表示颜色
  • True对应closed参数,表示生成的直线最后要封闭
  • pts即随机生成二元点集
  • 最后,1表示直线的宽度。

光线反射

有了直线工具,可以做一个光线反射动画,比如现有一点 x 0 , y 0 x_0, y_0 x0,y0,其出射角度为 θ \theta θ,则射线方程可写为

x = x 0 + k x t k x = cos ⁡ θ y = y 0 + k y t k x = sin ⁡ θ x=x_0+k_x t\quad k_x=\cos\theta\\ y=y_0+k_y t\quad k_x=\sin\theta x=x0+kxtkx=cosθy=y0+kytkx=sinθ

这个直线将于4个墙壁产生交点,根据 θ \theta θ的值,可判断具体的交点,具体代码如下

python 复制代码
def cross(x0, y0, kx, ky, w, h):
    pL = (0, y0-ky/kx*x0)
    pD = (x0-kx/ky*y0, 0)
    pR = (w, y0+ky/kx*(w-x0))
    pT = (x0+kx/ky*(h-y0), h)
    if kx>0 and ky>0:
        return pR if pR[1]<h else pT
    if kx>0 and ky<0:
        return pR if pR[1]>0  else pD
    if kx<0 and ky>0:
        return pL if pL[1]<h else pT
    if kx<0 and ky<0:
        return pL if pL[1]>0 else pD

在有了交点之后,可以得到新的角度。如果是在上下壁反射,则 k x k_x kx变号,否则 k y k_y ky变号。

python 复制代码
def getNewK(kx, ky, x1, w):
    flag = x1==0 or x1==w
    return (-kx, ky) if flag else (kx, -ky)

最后,是绘图逻辑

python 复制代码
pygame.init()
w, h = 640, 320
screen = pygame.display.set_mode((w, h))

pts = [np.random.rand(2)*(w, h)]
th = np.random.rand()*np.pi
kx, ky = np.cos(th), np.sin(th)

while True:
    if pygame.QUIT in [e.type for e in pygame.event.get()]:
        pygame.quit()
        break
    time.sleep(0.1)
    x,y = pts[-1]
    pt = cross(x,y, kx, ky, w, h)
    pts.append(pt)
    kx, ky = getNewK(kx, ky, pt[0], w)
    c = (np.random.rand(3)*255).astype(int)
    screen.fill("black")
    pygame.draw.lines(screen, c, False, pts, 1)
    pygame.display.flip()

效果如下

相关推荐
databook8 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar9 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805110 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_10 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机16 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机17 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机17 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机17 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i18 小时前
drf初步梳理
python·django
每日AI新事件18 小时前
python的异步函数
python