pygame飞机大战用精灵组层编写敌机系列(四)B样条曲线的实现实例

在pygame的飞机大战游戏中,通过在enemyplane.py中使用B样条曲线来创建敌机的运行轨迹。更新了path_points点,并在update()函数中调整点的传递方式。同时在enemyunit.py中新增了一个展示8字轨迹的类。在main.py的初始化函数中进行了相应设置,运行游戏后可以看到敌机沿着B样条曲线的8字轨迹飞行。完整代码和资源可在指定链接找到。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结合前面的贝塞尔曲线的实现方式,在enemyplane.py文件里添加

    def set_BSpline_path(self,points):
        self.path_type = 'BSpline'
        self.points = points
        data = np.array(points)
        tck,u = interpolate.splprep(data.transpose(), s=0)
        t = 1 / self.max_point
        unew = np.arange(0, 1.01, t)
        out = interpolate.splev(unew, tck)
        self.path_points=out

使得self.path_points的点为运算后的点,值得注意的是,out的数组形式是out[0]保存的是一组x值,out[1]保存的是一组y值。

所以在update()函数里,点的传递方式为out[0][],out[1][]

       elif self.path_type == 'BSpline':
            if self.current_point == self.max_point:
                self.current_point = 0

            self.rect.x = self.path_points[0][self.current_point]
            self.rect.y = self.path_points[1][self.current_point]
            self.current_point += 1<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值