找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 36|回复: 1

[教程] 视差组件(用在主界面提升时髦值)

[复制链接]
发表于 昨天 03:30 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 Maz马 于 2025-9-12 04:22 编辑

视差组件是一个非常能提升时髦值的东西
无论是用在主界面或是做一些伪3D场景

嗯...实际上这个东西并不是我写的
没找到在哪摸来的...但总之是开源的,而且我对其进行了一些关键的优化,两者作用下因此没有投转载

另外,来自版主的,关于视差的另一个帖子:

用bar制作简单移动视差效果(renpy进阶学习经验五)
https://www.renpy.cn/forum.php?mod=viewthread&tid=1557
(版主的可以看预览,所以我不贴预览图了)

优化的地方我认为可以水点字数(
1.实际就是一个图片跟随鼠标移动功能,原代码
  为了反向移动居然复制了一份,然后就只改了计算...我直接提成参数了

2.原代码没有注意到CDD的渲染锚点是固定在左上角的(0,0)
  pygame取得鼠标的值的计算也是以(0,0)
  而大多数视差的对齐锚点是需要在屏幕中心的,这会导致视差的效果出现不理想的偏移

  锚点其实有两个,但很容易忽略掉其中一个
  第一个是屏幕锚点,通过鼠标取得偏移值
  第二个是图片锚点,图片移动偏移值(一般也是屏幕中心,但这里我写的是图片中心,因为我的虚拟摇杆是这样写的(bushi)

也许有时间我会看看怎么把锚点提取成参数,方便更多需求吧,但暂时就中心偏移了
[RenPy] 纯文本查看 复制代码
### 跟随鼠标移动的视差 ###################################################################
init python:
    import pygame

    # 获取显示对象的实际尺寸,如果无法获取则返回默认尺寸
    def get_displayable_size(displayable, default_size):
        if displayable is None:
            return default_size
        try:
            render = renpy.render(displayable, 0, 0, 0, 0)
            size = render.get_size()
            return size if size[0] > 0 and size[1] > 0 else default_size
        except:
            return default_size

    class TrackCursor(renpy.Displayable):
        def __init__(self,child,paramod=1.0,**kwargs):
            super(TrackCursor, self).__init__()
            # 图片
            self.child = child
            # 移动参数:正数=同向移动,负数=反向移动,绝对值越大移动幅度越小
            self.paramod = paramod

            # 鼠标坐标
            self.mouse_x = 0
            self.mouse_y = 0
            # 渲染坐标
            self.x = 0
            self.y = 0
            # 时间轴
            self.old_time = 0
            self.delta_time = 0
            # 尺寸
            self.child_width = 0
            self.child_height = 0
            # 自适应渲染检查
            self.size_initialized = False
    
        # 事件方法
        def event(self, ev, x, y, st):
            if ev.type == pygame.MOUSEMOTION:
                # 计算鼠标相对于屏幕中心的偏移量
                center_x = config.screen_width / 2
                center_y = config.screen_height / 2
                offset_x = (x - center_x) / self.paramod
                offset_y = (y - center_y) / self.paramod

                self.mouse_x = offset_x
                self.mouse_y = offset_y
    
        # 渲染显示对象
        def render(self, width, height, st, at):
            # 首次渲染时初始化子对象尺寸
            if not self.size_initialized:
                child_render = renpy.render(self.child, width, height, st, at)
                self.child_width, self.child_height = child_render.get_size()
                self.size_initialized = True
            
            render = renpy.Render(width, height)
            
            # 计算渲染间隔
            if self.old_time == 0:
                self.old_time = st
            self.delta_time = st - self.old_time
            self.old_time = st
            
            # 缓动公式
            self.x += (self.mouse_x - self.x) * 1.5 * self.delta_time * abs(self.paramod)
            self.y += (self.mouse_y - self.y) * 1.5 * self.delta_time * abs(self.paramod)
            
            # 计算最终绘制位置:图片中心 + 偏移量
            draw_x = (width - self.child_width) / 2 + self.x
            draw_y = (height - self.child_height) / 2 + self.y
            
            obj_render = renpy.render(self.child, width, height, st, at)
            render.blit(obj_render, (draw_x, draw_y))
            renpy.redraw(self, 0)
            return render

#使用方法:
#两个入参,(图片和偏移系数)
#图片得传入renpy.displayable("xxx")
#偏移系数不能为0,大致来说越靠近0,偏移程度越大。正值跟随鼠标移动,负值反向移动。20-60是一个比较舒适的范围。
#示例:
#image a = TrackCursor(renpy.displayable("background.png"), paramod=-20)
#default b = TrackCursor(renpy.displayable("background.png"), paramod=20)


评分

参与人数 1活力 +300 干货 +1 收起 理由
烈林凤 + 300 + 1 感谢分享!

查看全部评分

发表于 昨天 17:29 | 显示全部楼层
之前那篇帖子已经是一年前写的了啊……时间过的真快(望天)
先前我所采用的方法主要就是为了避免使用过于复杂的python语句,尤其是要用到pygame库,更方便新手用renpy原生界面语句搓出自己想要的界面效果
这篇帖子所使用的方法显然利用率更高,用到了cdd和pygame库,确实更适合renpy糕手们去使用,但因为咱也不会使用cdd,所以只能用用老方法了(
回复 支持 1 抱歉 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-13 09:35 , Processed in 0.052800 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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