找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5|回复: 0

[经验] 对于简单粒子效果代码的更新

[复制链接]
发表于 1 小时前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Ren'Py引擎从入门到放弃(支线3)——简单粒子效果
https://www.renpy.cn/forum.php?mod=viewthread&tid=296
(出处: RenPy中文空间)

原帖是这个. 但是年代有些久远, 并不能拿来直接用. 研究了一下, 现在的代码可以直接拿来用了.
比较重要的更新点有三个.
1. 在screen 里面使用, 而不是在label里面使用.
2. 遍历字典的方式改为for d in list(self.shown.keys()): 也就是先把字典所有的key值保存为一个list然后再遍历, 不然在遍历的过程中改变字典的大小会报错.
3. render.blit(d.render(width, height, st, at),(self.around[0] + d.radius * math.cos(d.angle), d.around[1] + d.radius * math.sin(d.angle))), 原本是读取(d.xpos, d.ypos), 也就是直接读取粒子的pos, 但是因为用的是around, 所以好像读取不到, 就暴力算了.

[RenPy] 纯文本查看 复制代码
transform particle(d, delay = 0.0,  angle = 0, radius=200, keep_time= 1.0, current_around = (config.screen_width/2,config.screen_height/2), init_zoom = 1.0, target_zoom = 1.0):
    # 这个效果是, 射出去泡泡, 泡泡会变大, 并且逐渐的渐隐. 
    d
    alpha 1.0
    subpixel True
    around current_around
    radius 0
    angle angle
    zoom init_zoom
    alpha 1.0
    linear keep_time radius radius zoom target_zoom alpha 0.0

init python:
    class BubbleShoot(renpy.Displayable):
        def __init__(self, displayable, interval = (0.02,0.04), keep_time=(0.15,0.3), around=(config.screen_width/2, config.screen_height/2),angle=(0,360),radius=(50,75),particles=None, zoom_init_range=(1.0,1.5), zoom_end_range=(1.5,2.5),**kwargs):
            super(BubbleShoot, self).__init__(**kwargs)

            self.d = [renpy.easy.displayable(d) for d in displayable] if isinstance(displayable, (set, list, tuple)) else [renpy.easy.displayable(displayable)]
            self.interval = interval
            self.keep_time = keep_time
            self.around = around
            self.angle = angle
            self.radius = radius
            self.particles = particles
            self.zoom_init_range = zoom_init_range
            self.zoom_end_range = zoom_end_range
            self.next = 0
            self.particle = 0
            self.shown = {}

        def render(self, width, height, st, at):
 
            rp = store.renpy
 
            render = rp.Render(width, height)
 
            if not (self.particles and self.particle >= self.particles) and self.next <= st:

                keep_time = rp.random.uniform(self.keep_time[0], self.keep_time[1])
                
                angle = rp.random.randrange(self.angle[0], self.angle[1])

                radius = rp.random.randrange(self.radius[0], self.radius[1])

                zoom_init = rp.random.uniform(self.zoom_init_range[0], self.zoom_init_range[1])
                
                zoom_end = rp.random.uniform(self.zoom_end_range[0], self.zoom_end_range[1])

                self.shown[st + keep_time] = particle(rp.random.choice(self.d), st, angle, radius, keep_time, self.around, zoom_init,zoom_end)

                self.next = st + rp.random.uniform(self.interval[0], self.interval[1])
                
                if self.particles:
                    self.particle = self.particle + 1
 
            for d in list(self.shown.keys()):
                if d < st:
                    del(self.shown[d])
                else:
                    d = self.shown[d]
                    # xpos + cos(radius)
                    # ypos + sin(radius)
                    render.blit(d.render(width, height, st, at),(self.around[0] + d.radius * math.cos(d.angle), d.around[1] + d.radius * math.sin(d.angle)))
 
            rp.redraw(self, 0)
 
            return render

        def visit(self):
            return self.d

image bubble_pic = Transform("bubble.png",zoom=0.1)

init python:
    def bubble_func():
        return BubbleShoot("bubble_pic",interval=(0.03,0.05), keep_time = (3.0,5.0), around = (960,540), angle=(0,360), radius=(200,250), particles=30)

screen bubble_button_v2:
    predict False
    default show_sm = False

    if show_sm:
        add bubble_func()

    # 按钮(放在屏幕下方中央)
    textbutton "✨ 点我冒泡泡":
        xalign 0.5
        yalign 0.5
        text_align (0.5,0.5)
        action SetScreenVariable("show_sm",False),SetScreenVariable("show_sm",True)
        background Solid("#4CAF50", xsize=200, ysize=60)
        text_color "#fff"
        text_size 24
        hover_background Solid("#1a28a3", xsize=200, ysize=60)

label bubbleV2:
    show screen bubble_button_v2
    "点击屏幕上方的按钮,看看泡泡特效!"

    "1"
    hide screen bubble_button_v2
    return




评分

参与人数 1活力 +300 干货 +3 收起 理由
被诅咒的章鱼 + 300 + 3 感谢分享!

查看全部评分

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号 )

GMT+8, 2026-9-8 14:00 , Processed in 0.027781 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表