• pygame实现物体运动拖尾尾迹-渐隐版


    在这里插入图片描述

    解说文案:

    视频讲解:https://www.bilibili.com/video/BV14w411S74E/

    上一集我们实现了物体本体形式的拖尾,这一集我们基于上一集来实现物体逐渐变透明到消失的拖尾。

    上集我们也说过,逐渐变透明到消失的物体拖尾只不过是给物体添加一个透明度,然后透明度逐渐变化的效果。

    那么我们只需要掌握透明度物体绘制方法即可。

    如果我们去问gpt,如何在pygame中画一个半透明的圆,它会给你这样的答案,但你真正使用的时候,会发现它不是半透明的。在网上查阅后,我发现原来pygame的屏幕不支持显示rgba颜色。

    但是我们可以曲线救国实现这一效果,实现方式如下:我们可以先创建一块支持rgba颜色的区域a,将图形绘制在a上面,再将a绘制在主窗口上。

    其实实现起来并不难,我们只需要封装一个支持rgba颜色的绘制函数即可。这里我们给出一个绘制圆的函数,其他图形同理,我会在笔记中给出其他的代码。

    我们来看这个用于绘制带有透明度的圆形函数的实现,首先计算一个包含圆形的矩形区域,然后为矩形区域创建带有透明度的Surface,其次是在Surface上绘制圆形,最后将将Surface绘制到主窗口上。
    看完这个函数后,我们再来看完整的代码。

    如果你看了上一期的视频,那么完整的代码理解起来非常的简单,它与上期代码的唯一不同就在于在记录圆的数组中也记录了每个圆的透明度,绘制圆函数也加了一个透明度的数值,然后在每次绘制的时候去减少圆的透明度,最后我们删掉透明度小于0的物体以防止物体数量的无限增长。

    就这样便实现了视频开始的效果,我们来运行一下看看。

    同上集一样,我们依旧可以通过改变代码里的参数来实现改变拖尾效果。比如这里我们增大透明度减少的速度,这样拖尾就会消失的更快,这样就相当于缩短了拖尾。

    我们就可以看到这样的效果。

    其他的参数我们不一一举例,举一反三是编程的必备思维,我们应该去锻炼这种思维。好了,本集内容到此结束。后面还有各种炫酷效果,关注up主不迷路呦~

    代码

    import pygame
    import sys
    
    # 初始化,创建屏幕,设置窗口的标题,即游戏名称
    pygame.init()
    screen = pygame.display.set_mode((600, 400))
    pygame.display.set_icon(pygame.image.load('logo.png').convert())
    pygame.display.set_caption('编程启航')
    
    # 小球属性
    ball_radius = 20
    ball_color = (0, 0, 255)
    
    # 小球的位置和速度
    ball_pos = [*screen.get_size()]
    ball_velocity = [0, 0]
    
    # 拖尾效果所需的轨迹点
    trail_points = []
    
    # 定义一个函数,用于绘制带有透明度的圆形
    def draw_circle_alpha(surface, color, center, radius):
        # 计算一个包含圆形的矩形区域
        target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
        # 创建带有透明度的Surface
        shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
        # 在Surface上绘制圆形
        pygame.draw.circle(shape_surf, color, (radius, radius), radius)
        # 将Surface绘制到指定位置
        surface.blit(shape_surf, target_rect)
    
    # 游戏循环
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        screen.fill((255,255,255))
    
        # 获取鼠标位置
        mouse_pos = pygame.mouse.get_pos()
    
        # 更新小球速度
        ball_velocity[0] = (mouse_pos[0] - ball_pos[0]) * 0.1
        ball_velocity[1] = (mouse_pos[1] - ball_pos[1]) * 0.1
    
        # 更新小球位置
        ball_pos[0] += int(ball_velocity[0])
        ball_pos[1] += int(ball_velocity[1])
    
        # 添加当前位置到轨迹点列表
        trail_points.append((ball_pos.copy(), ball_color,255))  # 初始透明度为255
    
        # 更新和绘制轨迹点
        for i, (point, color,a) in enumerate(trail_points):
            draw_circle_alpha(screen, (*color,a), point, 20)
            # 尾迹长
            # trail_points[i]=(point, color,a-5)
            # 尾迹短
            trail_points[i]=(point, color,a-50)
    
        # 删除透明度为0的轨迹点
        trail_points = [p for p in trail_points if p[2] > 0]
    
        # 更新屏幕
        pygame.display.flip()
    
        # 控制帧率
        pygame.time.Clock().tick(60)
    
    pygame.quit()
    sys.exit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    其他图案封装函数

    import pygame  # 导入pygame库
    
    # 定义一个函数,用于绘制带有透明度的矩形
    def draw_rect_alpha(surface, color, rect):
        shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)  # 创建带有透明度的Surface
        pygame.draw.rect(shape_surf, color, shape_surf.get_rect())  # 在Surface上绘制矩形
        surface.blit(shape_surf, rect)  # 将Surface绘制到指定位置
    
    # 定义一个函数,用于绘制带有透明度的圆形
    def draw_circle_alpha(surface, color, center, radius):
        target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))  # 创建一个包含圆形的矩形区域
        shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)  # 创建带有透明度的Surface
        pygame.draw.circle(shape_surf, color, (radius, radius), radius)  # 在Surface上绘制圆形
        surface.blit(shape_surf, target_rect)  # 将Surface绘制到指定位置
    
    # 定义一个函数,用于绘制带有透明度的多边形
    def draw_polygon_alpha(surface, color, points):
        lx, ly = zip(*points)  # 获取多边形顶点的x和y坐标
        min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)  # 计算多边形的外接矩形
        target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)  # 创建一个包含多边形的矩形区域
        shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)  # 创建带有透明度的Surface
        pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])  # 在Surface上绘制多边形
        surface.blit(shape_surf, target_rect)  # 将Surface绘制到指定位置
    
    pygame.init()  # 初始化pygame库
    window = pygame.display.set_mode((250, 250))  # 创建窗口大小为250x250的显示窗口
    clock = pygame.time.Clock()  # 创建时钟对象,用于控制帧率
    
    background = pygame.Surface(window.get_size())  # 创建一个大小与窗口相同的Surface
    ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
    
    # 创建棋盘格状的背景
    tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
    for rect, color in tiles:
        pygame.draw.rect(background, color, rect)
    
    run = True
    while run:
        clock.tick(60)  # 控制帧率为60帧每秒
        for event in pygame.event.get():  # 获取事件
            if event.type == pygame.QUIT:  # 如果事件类型为QUIT,即关闭窗口事件
                run = False  # 设置循环标志为False,退出循环
    
        window.blit(background, (0, 0))  # 将背景绘制到窗口上
    
        # 使用定义的函数绘制带有透明度的矩形、圆形和多边形
        draw_rect_alpha(window, (0, 0, 255, 127), (55, 90, 140, 140))
        draw_circle_alpha(window, (255, 0, 0, 127), (150, 100), 80)
        draw_polygon_alpha(window, (255, 255, 0, 127),
            [(100, 10), (100 + 0.8660 * 90, 145), (100 - 0.8660 * 90, 145)])
    
        pygame.display.flip()  # 更新显示
    
    pygame.quit()  # 退出pygame库
    exit()  # 退出程序
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    总结

    🐋 🐬 🐶 🐳 🐰 🦀☝️ ⭐ 👉 👀

    如果你对这篇文章感兴趣,欢迎在评论区留言,分享你的想法和建议。如果你喜欢我的博客,请记得点赞、收藏和关注我,我会持续更新更多有用的网页技巧和教程。谢谢大家!


    更多宝藏

    🍇🍉🍊🍏🍋🍅🥝🥥🫒🫕🥗
    视频推送看这里🤤:
    https://space.bilibili.com/1909782963
    项目仓库看这里🤗:
    https://github.com/w-x-x-w
    https://gitee.com/w-_-x
    公众号名称😮:编程启航
    博客文章看这里🤭:
    https://blog.csdn.net/weixin_62650212

  • 相关阅读:
    【Hello Algorithm】滑动窗口内最大值最小值
    零基础想自学编程,不知道学前端还是后端还是其他,也不知道学哪种编程语言?
    汉字风格迁移篇---数据集制作---ttf转png格式
    为什么重写 equals() 就一定要重写 hashCode() 方法
    30 华三华为STP
    小程序游戏对接广告收益微信小游戏抖音游戏软件
    大数据-hadoop
    Docker数据集与自定义镜像:构建高效容器的关键要素
    MySQL基本操作之创建数据表
    已解决Python向数据库插入数据的字符串中含有单引号或双引号报错
  • 原文地址:https://blog.csdn.net/weixin_62650212/article/details/132612699