本帖最后由 反对司令 于 2026-4-15 13:38 编辑
被诅咒的章鱼 发表于 2026-4-13 10:37
楼主光说“卡顿”, 不发代码,我也没办法知道原因……
不好意思,这就贴上代码
这个是没用矩阵图的版本,但前进到第二句话之后,就会卡住一会,再往前前进,卡顿就更严重了……
[RenPy] 纯文本查看 复制代码 init python:
from renpy.display.layout import Crop
class SpriteSheetAnimator(renpy.Displayable):
def __init__(self, image_path, rows, cols, interval, loop=True, pingpong=False, **kwargs):
super(SpriteSheetAnimator, self).__init__(**kwargs)
self.image_path = image_path
self.full_width, self.full_height = renpy.image_size(image_path)
self.rows = rows
self.cols = cols
self.frame_w = self.full_width // cols
self.frame_h = self.full_height // rows
self.length = rows * cols
self.frames = []
for row in range(rows):
for col in range(cols):
x = col * self.frame_w
y = row * self.frame_h
frame = Crop((x, y, self.frame_w, self.frame_h), renpy.displayable(image_path))
self.frames.append(frame)
self.current_frame = 0
self.last_time = 0
self.interval = interval
self.loop = loop
self.pingpong = pingpong
self.direction = 1 # 1 表示正向,-1 表示反向
def render(self, width, height, st, at):
if st - self.last_time >= self.interval:
self.last_time = st
if self.pingpong:
self.current_frame += self.direction
# 边界检测并反转方向
if self.current_frame >= self.length - 1:
self.current_frame = self.length - 1
self.direction = -1
elif self.current_frame <= 0:
self.current_frame = 0
self.direction = 1
else:
self.current_frame += 1
if self.current_frame >= self.length:
if self.loop:
self.current_frame = 0
else:
self.current_frame = self.length - 1
frame_render = renpy.render(self.frames[self.current_frame], width, height, st, at)
renpy.redraw(self, 0)
return frame_render
# 创建动画实例,启用乒乓模式
ctc_anim = SpriteSheetAnimator(
"gui/ctc/ctc_sheet.png",
rows=5,
cols=5,
interval=0.05,
loop=True,
pingpong=True # 关键:启用往返播放
)
# 将动画实例注册为图像
image ctc_animation = ctc_anim
# 定义 CTC 屏幕
screen ctc():
zorder 100
add "ctc_animation":
xalign 0.98
yalign 0.98
zoom 1
|