找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1463|回复: 2

[原创] [教程]简易画廊 (附带github工程文件)

[复制链接]
发表于 2022-10-24 11:51:42 | 显示全部楼层 |阅读模式

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

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

×
看经常有人问画廊的问题,我也研究了下,写的比较简易:


代码:
https://github.com/red-moon-tea-party/renpy/tree/main/cg_gallery/game


script.rpy
[RenPy] 纯文本查看 复制代码
## 定义画廊里按钮的高度和宽度。
define gui.gallery_slot_height = 100
define gui.gallery_slot_width = 200
define gui.gallery_slot_cols = 3
define gui.gallery_slot_rows = 2

init python:
    if persistent.unlock_1 == None:
        persistent.unlock_1 = False

    if persistent.unlock_2 == None:
        persistent.unlock_2 = False

    if persistent.unlock_3 == None:
        persistent.unlock_3 = False

    if persistent.unlock_4 == None:
        persistent.unlock_4 = False

# 游戏在此开始。

label start:
    scene beach3
    "测试"
    scene bigbeach1
    "测试"
    show beach1 mary
    "测试"
    menu:
        "结局1":
            jump b_1

        "结局2":
            jump b_2

        "结局3":
            jump b_3

        "结局4":
            jump b_4
    return

label b_1:
    $ persistent.unlock_1 = True
    scene transfer
    "测试"
    scene moonpic
    "测试"
    scene girlpic
    "测试"
    "解锁结局1"
    return

label b_2:
    $ persistent.unlock_2 = True
    scene p1
    "unlock_2"
    return

label b_3:
    $ persistent.unlock_3 = True
    scene p2
    "unlock_3"
    return

label b_4:
    $ persistent.unlock_4 = True
    scene p3
    "unlock_4"
    return


gallery handler
[RenPy] 纯文本查看 复制代码
init -99 python:
    class GalleryHandler:
        SLOT_PER_PAGE = 6
        def __init__(self, buttons):
            ## 画廊处于第几页
            self.index = 0
            self.buttons = buttons

        ## 初始化画廊设置
        def init_gallery(self, gallery):
            for b in buttons:
                gallery.button(b.name)
                if b.condition:
                    gallery.condition(b.condition)

                if b.transform:
                    gallery.transform(b.transform)

                for cg in b.images:
                    gallery.unlock_image(cg)

        ## 拿到当前页面的画廊按钮
        def get(self):
            start = self.index * GalleryHandler.SLOT_PER_PAGE
            end = start + GalleryHandler.SLOT_PER_PAGE
            return self.buttons[start:end]

        def get_total_pages(self):
            return (len(self.buttons) + GalleryHandler.SLOT_PER_PAGE - 1) // GalleryHandler.SLOT_PER_PAGE

    class GalleryButtonEntry:
        SLOT_HEIGHT=150
        SLOT_WIDTH=300
        def __init__(self, name, images, condition=None,
            transform=None, thumbnail=None):

            ## 按钮的名字
            self.name = name
            ## 可能有一张或者多张cg
            self.images = images
            ## 解锁条件,可以没有
            self.condition = condition
            ## 多张图的转场效果,可以没有
            self.transform = transform

            ## 按钮的缩略图,可以没有,会缺省用第一张图的缩略图
            if thumbnail:
                self.thumbnail = thumbnail
            else:
                self.thumbnail = im.Scale("images/{:s}.jpg".format(self.images[0]),
                    GalleryButtonEntry.SLOT_WIDTH,
                    GalleryButtonEntry.SLOT_HEIGHT)


gallery
[RenPy] 纯文本查看 复制代码
init python:

    buttons = [
        ## 只有一张图的cg
        GalleryButtonEntry(name="dawn", images=["dawn1"]),

        ## 多张cg,有转场效果
        GalleryButtonEntry(name="dark", images=["bigbeach1",
            "beach1 mary", "beach2", "beach3"], transform=slideawayleft),

        ## 需要unlock_1是true才会解锁
        GalleryButtonEntry(name="end1",
        images=["transfer", "moonpic", "girlpic"],
        condition="persistent.unlock_1"),

        ## 需要unlock_2是true才会解锁
        GalleryButtonEntry(name="end2",
        images=["transfer1", "moonpic", "girlpic"],
        condition="persistent.unlock_2"),

        ## 需要unlock_3是true才会解锁
        GalleryButtonEntry(name="end3",
        images=["transfer", "moonpic", "girlpic"],
        condition="persistent.unlock_3"),

        ## 需要unlock_4是true才会解锁
        GalleryButtonEntry(name="end4",
        images=["transfer", "moonpic", "girlpic"],
        condition="persistent.unlock_4"),

        ## 单一图的cg
        GalleryButtonEntry(name="p1",
        images=["p1"]),

        GalleryButtonEntry(name="p2",
        images=["p2"]),

        GalleryButtonEntry(name="p3",
        images=["p3"]),

        GalleryButtonEntry(name="p4",
        images=["p3"]),

        GalleryButtonEntry(name="p5",
        images=["p3"]),

        GalleryButtonEntry(name="p6",
        images=["p3"]),

        GalleryButtonEntry(name="p7",
        images=["p3"]),

        GalleryButtonEntry(name="p8",
        images=["p3"]),
    ]

    ## 创建Gallery对象。
    g = Gallery()

    ## 设置没有解锁的图片
    g.locked_button = Composite(
        (GalleryButtonEntry.SLOT_WIDTH,
        GalleryButtonEntry.SLOT_HEIGHT),
        (0, 0), Solid("#000"),
        (50,30), Text("未解锁", size=30, color="#fff"))

    g.button("beach3")
    g.unlock_image("beach3")

    # 用于图像切换使用的转场(transition)。
    g.transition = dissolve

    gh = GalleryHandler(buttons)
    gh.init_gallery(g)

# 我们使用的画廊界面。
screen gallery:

    # 确保画廊界面替换主菜单。
    tag menu

    # 背景图。
    add "gui/gallery/bg.jpg"

    text _("画廊"):
        size 30
        color "#fff"
        align (0.1, 0.1)

    use gallery_slots(gh.get())

    ## 翻页逻辑
    hbox:
        xalign 0.5 yalign 0.8
        frame:
            textbutton _("上一页"):
                sensitive gh.index > 0
                action SetField(gh, 'index', gh.index-1)

        for i in range(gh.get_total_pages()):
            frame:
                textbutton str(i):
                    action SetField(gh, 'index', i)

        frame:
            textbutton _("下一页"):
                sensitive gh.index < gh.get_total_pages()-1
                action SetField(gh, 'index', gh.index+1)

    # 用于响应后返回主菜单的界面。
    # 也能用于导航到其他画廊界面。
    textbutton "Return" action Return() xalign 0.1 yalign 0.8


screen gallery_slots(buttons):
    # 按钮网格(grid)。
    grid gui.gallery_slot_cols gui.gallery_slot_rows:
        align (0.5, 0.5)
        xysize (1600, 1200)
        xspacing 30
        yspacing 30

        for b in buttons:
            frame:
                foreground Frame("gui/gallery/idle.png")
                xysize (GalleryButtonEntry.SLOT_WIDTH, GalleryButtonEntry.SLOT_HEIGHT)

                # 调用make_button显示具体的按钮。
                add g.make_button(name=b.name, unlocked=b.thumbnail,
                    xalign=0.5, yalign=0.5)

        ## 如果格子不满,需要补上空白
        for i in range(gh.SLOT_PER_PAGE - len(buttons)):
            null


screens
[RenPy] 纯文本查看 复制代码
textbutton _("画廊"):
            text_idle_color gui.idle_color
            text_hover_color gui.hover_color
            action [
            ShowMenu("gallery"), SetField(gh, 'index', 0)]

评分

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

查看全部评分

发表于 2023-1-19 11:22:58 | 显示全部楼层
感觉国内代码传gitee 仓库 比较方便,和github 差不多
github 裸连有时候比较慢...
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2023-1-20 09:37:41 | 显示全部楼层
fattiger 发表于 2023-1-19 11:22
感觉国内代码传gitee 仓库 比较方便,和github 差不多
github 裸连有时候比较慢... ...

我授权你可以搬运一份,只要保留我署名和b站链接就可以了。

主要我没有精力发的到处都是。
回复 支持 抱歉

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 17:08 , Processed in 0.080698 second(s), 13 queries , File On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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