Ren'Py Code for gallery

Aitha

Newbie
Jul 7, 2017
20
7
I am super dumb in coding.
I just want to add gallery in my game. I searched internet but no method is working. Maybe they outdated.
If anyone know code for adding gallery in game. Please help me by giving code. I will be grateful.

I want just displaying of my gallery images no need to put any locked. No locking just click on image to see it.
Please help me. Have a nice day.
 

Jon_Sleeper

Member
Nov 14, 2018
284
363
I made my own gallery a few weeks back using that, so it is working. Maybe send me your code file and I will take a look
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,559
2,176
Do what all the greats do... just borrow someone else's.

I would suggest finding a game with a gallery similar to what you are looking for and then look at the code it uses.
If the code looks too complex for your understanding of RenPy... ignore it and go looking for a different game that might have simpler code.

If the game you are look at doesn't have .rpy files (either rpc or rpa files) because author has only released the compressed/compiled code, then use UnRen to unpack them to get access to the rpy files.

Keep searching until you find some code you are comfortable cut-and-pasting into your own game. I would highly recommend looking out for games that have a "gallery.rpy" file, since they tend to be the easiest to borrow from - as the code is more or less in one place.

If after looking at half a dozen games with galleries you still haven't found code simple enough for your coding level yet... perhaps accept that a gallery isn't absolutely necessary right now and come back to it later.

Most galleries will feature the "unlock" feature you don't want... but that will almost certainly be based on a . In most cases, if you exchange the seen_image with True that will remove the condition that the image has been seen once by the player and instead just always (because it's true) show the image.

One game you might consider looking at is Family, Friends and Strangers. I found that gallery pretty easy to follow and well written. I hope Rich doesn't mind me suggesting you "borrow" his code.
 

Rich

Old Fart
Modder
Respected User
Donor
Game Developer
Jun 25, 2017
2,466
6,934
One game you might consider looking at is Family, Friends and Strangers. I found that gallery pretty easy to follow and well written.
Thank you. The gallery in FF&S is directly based on the Renpy gallery code, so PyTom did most of the heavy lifting. All I had to do was set up the gallery buttons using his system, and then implement a screen with paging to display the buttons.

I hope Rich doesn't mind me suggesting you "borrow" his code.
Not a bit - I've learned a lot by "snooping" on the code of others, so I can hardly complain...

My gallery is in two parts. In the first part, I set up a bunch of "slots" (my name for it) using PyTom's code:

Code:
init python:
    def gallery_thumbnail(thumbnail_name):
        return im.FactorScale(thumbnail_name, 0.2)

    image_gallery = Gallery()
    image_gallery.transition = dissolve
    image_gallery.locked_button = "gallery_locked_button"

    image_gallery.button("slot01")
    image_gallery.condition('renpy.seen_image("ch0b_12")')
    image_gallery.unlock_image("ch0b_09")
    image_gallery.unlock_image("ch0b_10")
    image_gallery.unlock_image("ch0b_11_movie")
    image_gallery.unlock_image("ch0b_12")

    image_gallery.button("slot02")
    image_gallery.condition('renpy.seen_image("ch0d_27")')
    image_gallery.unlock_image("ch0d_24")
    image_gallery.unlock_image("ch0d_25")
    image_gallery.unlock_image("ch0d_26_movie")
    image_gallery.unlock_image("ch0d_27")
The "condition" specifies what has to have happened for this particular gallery slot to be unlocked. All those "ch0d_27" type things are image names of mine.

If you just want them all unlocked without having to do anything, you can probably just omit that. Or maybe use
Code:
image_gallery.condition('True')
The "unlock_image" part basically tells Renpy to go ahead and show that image. As you can see, you can put a sequence of images into the gallery button.

The next part is that I set up a Python object that configured each page of my gallery. (This is still inside the Python block above):

Code:
    image_gallery_buttons_pages = [
        [
            ( "slot01", "Ch 1: Brad and Emily",     "ch0b/ch0b_09.jpg" ),
            ( "slot02", "Ch 1: Parker and Becky",   "ch0d/ch0d_24.jpg" ),
            ( "slot03", "Ch 2: Melanie\u2019s Bra", "ch1a/ch1a_03.jpg" ),
            ( "slot04", "Ch 2: Melissa Downblouse", "ch1b/ch1b_09.jpg" ),
            ( "slot05", "Ch 2: License Grab",       "ch1c/ch1c_16_0.jpg" ),
            ( "slot06", "Ch 2: The Search, Part 1", "ch1e/ch1e_09a.jpg" )
        ],
        [
            ( "slot07", "Ch 2: The Search, Part 2", "ch1e/ch1e_15.jpg" ),
            ( "slot08", "Ch 2: The Search, Part 3", "ch1e/ch1e_19a.jpg" ),
            ( "slot09", "Ch 2: The Search, Part 4", "animations/ch1e_25_00.jpg" ),
            ( "slot10", "Ch 3: A revealing time",   "ch3a/ch3a_06.jpg" ),
            ( "slot11", "Ch 3: Let\u2019s have the top", "ch3c/ch3c_07.jpg" ),
            ( "slot12", "Ch 3: And the rest",       "ch3c/ch3c_27.jpg" ),
        ],
        [
            ( "slot13", "Ch 3: Exposed",             "ch3c/ch3c_32.jpg" ),
            ( "slot14", "Ch 3: Full Search, Part 1", "ch3c/ch3c_37.jpg" ),
            ( "slot15", "Ch 3: Full Search, Part 2", "ch3c/ch3c_40.jpg" ),
            ( "slot16", "Ch 3: Dressing",           "ch3c/ch3c_46.jpg" ),
            ( "slot17", "Ch 3: Tits are tits",      "ch3d/ch3d_17.jpg" ),
            ( None,     None,                       None)
        ]
    ]
For those of you not horribly Pythonista, image_gallery_buttons_pages is an array. It has three members, each of which are also arrays, and which correspond to the gallery pages themselves. Each of those arrays contains 6 tuples (the parts in parentheses), in which the first element is the slot name, the second is the title for the gallery button and the third is the thumbnail image for the gallery button. The code requires there to be six tuples in each page, but if the buttons don't come out evenly with the number of pages, you just put None's in, as you can see for the last one.

Finally, there's the gallery screen itself:
Code:
screen gallery_buttons(data):
    for (name, title, thumbnail) in data:
        if name:
            vbox:
                $ the_button = image_gallery.make_button(name, gallery_thumbnail(thumbnail))
                add the_button
                if image_gallery.buttons[name].check_unlock():
                    text title
                else:
                    text ""
        else:
            text ""

screen gallery():
    default page = 1
    tag menu

    use game_menu(_("Gallery"), scroll="viewport"):

        style_prefix "gallery"

        vbox:
            xsize 940
            ysize 500
            label "Page [page]":
                xalign 0.5
                bottom_margin 10

            grid 3 2:
                style_prefix "gallery_slot"

                xalign 0.5
                yalign 0.5

                xspacing 30
                yspacing 20

                use gallery_buttons(image_gallery_buttons_pages[page-1])

            hbox:
                style_prefix "page"

                anchor (0.5, 0.0)
                pos(0.5, 1.0)

                spacing gui.page_spacing

                if page > 1:
                    textbutton _("<") action SetScreenVariable("page", page - 1)
                else:
                    textbutton  _("<") action NullAction()

                for i in range(1, len(image_gallery_buttons_pages) + 1):
                    textbutton "[i]" action SetScreenVariable("page", i)

                if page < len(image_gallery_buttons_pages):
                    textbutton _(">") action SetScreenVariable("page", page + 1)
                else:
                    textbutton  _(">") action NullAction()

style gallery_slot_text:
    size 18
    xalign 0.5
    yalign 0.0

style gallery_button is button:
    hover_background "#fff"

style gallery_button_text:
    size 24
    bold True
    color "#fff"
    hover_color "#000"
    underline True

style gallery_button_current is button
style gallery_button_current_text is gallery_button_text:
    underline False
    hover_color "#fff"
gallery_buttons is a "subscreen" that will format the stuff for a single gallery button, then gallery represents the gallery screen itself, and implements the pagination and outputs the gallery buttons based on the data in image_gallery_buttons_pages.

So, there's your sample code. Adapt as you see fit. :)
 

Aitha

Newbie
Jul 7, 2017
20
7
Thank you. The gallery in FF&S is directly based on the Renpy gallery code, so PyTom did most of the heavy lifting. All I had to do was set up the gallery buttons using his system, and then implement a screen with paging to display the buttons.



Not a bit - I've learned a lot by "snooping" on the code of others, so I can hardly complain...

My gallery is in two parts. In the first part, I set up a bunch of "slots" (my name for it) using PyTom's code:

Code:
init python:
    def gallery_thumbnail(thumbnail_name):
        return im.FactorScale(thumbnail_name, 0.2)

    image_gallery = Gallery()
    image_gallery.transition = dissolve
    image_gallery.locked_button = "gallery_locked_button"

    image_gallery.button("slot01")
    image_gallery.condition('renpy.seen_image("ch0b_12")')
    image_gallery.unlock_image("ch0b_09")
    image_gallery.unlock_image("ch0b_10")
    image_gallery.unlock_image("ch0b_11_movie")
    image_gallery.unlock_image("ch0b_12")

    image_gallery.button("slot02")
    image_gallery.condition('renpy.seen_image("ch0d_27")')
    image_gallery.unlock_image("ch0d_24")
    image_gallery.unlock_image("ch0d_25")
    image_gallery.unlock_image("ch0d_26_movie")
    image_gallery.unlock_image("ch0d_27")
The "condition" specifies what has to have happened for this particular gallery slot to be unlocked. All those "ch0d_27" type things are image names of mine.

If you just want them all unlocked without having to do anything, you can probably just omit that. Or maybe use
Code:
image_gallery.condition('True')
The "unlock_image" part basically tells Renpy to go ahead and show that image. As you can see, you can put a sequence of images into the gallery button.

The next part is that I set up a Python object that configured each page of my gallery. (This is still inside the Python block above):

Code:
    image_gallery_buttons_pages = [
        [
            ( "slot01", "Ch 1: Brad and Emily",     "ch0b/ch0b_09.jpg" ),
            ( "slot02", "Ch 1: Parker and Becky",   "ch0d/ch0d_24.jpg" ),
            ( "slot03", "Ch 2: Melanie\u2019s Bra", "ch1a/ch1a_03.jpg" ),
            ( "slot04", "Ch 2: Melissa Downblouse", "ch1b/ch1b_09.jpg" ),
            ( "slot05", "Ch 2: License Grab",       "ch1c/ch1c_16_0.jpg" ),
            ( "slot06", "Ch 2: The Search, Part 1", "ch1e/ch1e_09a.jpg" )
        ],
        [
            ( "slot07", "Ch 2: The Search, Part 2", "ch1e/ch1e_15.jpg" ),
            ( "slot08", "Ch 2: The Search, Part 3", "ch1e/ch1e_19a.jpg" ),
            ( "slot09", "Ch 2: The Search, Part 4", "animations/ch1e_25_00.jpg" ),
            ( "slot10", "Ch 3: A revealing time",   "ch3a/ch3a_06.jpg" ),
            ( "slot11", "Ch 3: Let\u2019s have the top", "ch3c/ch3c_07.jpg" ),
            ( "slot12", "Ch 3: And the rest",       "ch3c/ch3c_27.jpg" ),
        ],
        [
            ( "slot13", "Ch 3: Exposed",             "ch3c/ch3c_32.jpg" ),
            ( "slot14", "Ch 3: Full Search, Part 1", "ch3c/ch3c_37.jpg" ),
            ( "slot15", "Ch 3: Full Search, Part 2", "ch3c/ch3c_40.jpg" ),
            ( "slot16", "Ch 3: Dressing",           "ch3c/ch3c_46.jpg" ),
            ( "slot17", "Ch 3: Tits are tits",      "ch3d/ch3d_17.jpg" ),
            ( None,     None,                       None)
        ]
    ]
For those of you not horribly Pythonista, image_gallery_buttons_pages is an array. It has three members, each of which are also arrays, and which correspond to the gallery pages themselves. Each of those arrays contains 6 tuples (the parts in parentheses), in which the first element is the slot name, the second is the title for the gallery button and the third is the thumbnail image for the gallery button. The code requires there to be six tuples in each page, but if the buttons don't come out evenly with the number of pages, you just put None's in, as you can see for the last one.

Finally, there's the gallery screen itself:
Code:
screen gallery_buttons(data):
    for (name, title, thumbnail) in data:
        if name:
            vbox:
                $ the_button = image_gallery.make_button(name, gallery_thumbnail(thumbnail))
                add the_button
                if image_gallery.buttons[name].check_unlock():
                    text title
                else:
                    text ""
        else:
            text ""

screen gallery():
    default page = 1
    tag menu

    use game_menu(_("Gallery"), scroll="viewport"):

        style_prefix "gallery"

        vbox:
            xsize 940
            ysize 500
            label "Page [page]":
                xalign 0.5
                bottom_margin 10

            grid 3 2:
                style_prefix "gallery_slot"

                xalign 0.5
                yalign 0.5

                xspacing 30
                yspacing 20

                use gallery_buttons(image_gallery_buttons_pages[page-1])

            hbox:
                style_prefix "page"

                anchor (0.5, 0.0)
                pos(0.5, 1.0)

                spacing gui.page_spacing

                if page > 1:
                    textbutton _("<") action SetScreenVariable("page", page - 1)
                else:
                    textbutton  _("<") action NullAction()

                for i in range(1, len(image_gallery_buttons_pages) + 1):
                    textbutton "[i]" action SetScreenVariable("page", i)

                if page < len(image_gallery_buttons_pages):
                    textbutton _(">") action SetScreenVariable("page", page + 1)
                else:
                    textbutton  _(">") action NullAction()

style gallery_slot_text:
    size 18
    xalign 0.5
    yalign 0.0

style gallery_button is button:
    hover_background "#fff"

style gallery_button_text:
    size 24
    bold True
    color "#fff"
    hover_color "#000"
    underline True

style gallery_button_current is button
style gallery_button_current_text is gallery_button_text:
    underline False
    hover_color "#fff"
gallery_buttons is a "subscreen" that will format the stuff for a single gallery button, then gallery represents the gallery screen itself, and implements the pagination and outputs the gallery buttons based on the data in image_gallery_buttons_pages.

So, there's your sample code. Adapt as you see fit. :)
"Rich" you are the man of the day.
Your code is working. I am so grateful to you.
I am going to give you credit in my game at about section.
Thanks man, your rich in heart also.
And I love f95 Community, such a friendly and helpful Community.
Thank you guys.
 
  • Like
Reactions: Basilicata

Rich

Old Fart
Modder
Respected User
Donor
Game Developer
Jun 25, 2017
2,466
6,934
"Rich" you are the man of the day.
You're completely welcome!

Your code is working. I am so grateful to you.
Well, gee, I hope so... ROTFL

I am going to give you credit in my game at about section.
Thanks man, your rich in heart also.
Unexpected, but thank you!

And I love f95 Community, such a friendly and helpful Community.
Thank you guys.
It is. I've learned a ton here, so this is one way of paying it back.
 

Rich

Old Fart
Modder
Respected User
Donor
Game Developer
Jun 25, 2017
2,466
6,934
I cannot display images which are not seen in game.
Like I want to display images for users which are not seen in game play (Not played in game).
Can you help me?
Well, I can try. I preface this by saying that I've never written code to do this, so I'm basing my advice on the documentation for the Ren'py image gallery logic, which can be found here:

It would probably be worth your time to read through that section just to understand what the functions do.

Having said that, let's walk through a gallery button step by step:
Code:
    image_gallery.button("slot01")
    image_gallery.condition('renpy.seen_image("ch0b_12")')
    image_gallery.unlock_image("ch0b_09")
    image_gallery.unlock_image("ch0b_10")
    image_gallery.unlock_image("ch0b_11_movie")
    image_gallery.unlock_image("ch0b_12")
The first line
Code:
image_gallery.button("slot01")
creates a button definition inside the gallery object, and assigns it a name. You obviously want a different name for each button. Note that this doesn't actually create a button - it just sets up logic for the button when it's actually created later.

I use these names later in the screen when I do:
Code:
$ the_button = image_gallery.make_button(name, gallery_thumbnail(thumbnail))
add the_button
This is the part that actually creates the real button from the button definition identified by "name" and then adds it to my gallery screen. (Essentially, PyTom's gallery logic creates a button and attaches a whole bunch of "here's what to display" logic to it when you call make_button so that you don't have to implement all that logic yourself.)

Going back to the gallery block, the line:
Code:
image_gallery.condition('renpy.seen_image("ch0b_12")')
basically says "when you create this gallery button later, set it up to be enabled only if the condition 'renpy.seen_image("ch0b_12")' is True".

I haven't tried this, but if you don't want any condition on the gallery button itself (in other words, if the gallery button should always be enabled), then just remove that line. If that doesn't work, try:
Code:
image_gallery.condition('True')
but I suspect just removing the condition will suffice.

The lines like
Code:
image_gallery.unlock_image("ch0b_12")
basically say "add this image to the button, and unlock it once its been seen." Note the difference - the gallery button can be unlocked (by the "condition" statement) but it's still possible for images attached to the gallery button (in other words, the ones that get displayed when you click on the gallery button) to be locked.

I suspect that if you want images to be able to be displayed without going through the "have to see the image itself to unlock it" then you'd replace that line with simply
Code:
image_gallery.image("ch0b_12")
which should add the image to the button without an unlock condition.

As before, I'm using the "unlock it only if seen" variant in my own code, so I haven't tested this, but based on the documentation, that appears to be what you'd want to do.

As always, when in doubt:
  1. Read the documentation
  2. Experiment
  3. If all else fails, ask questions here or . There is a LOT of sample code in the . Chances are if you have a question, somebody has already posted a sample there...
 

DarkTl

Member
Game Developer
Oct 8, 2017
111
189
Tbh, the built-in renpy gallery is needlessly complicated and bulky, especially for beginners. So I can see why it gives you the trouble.
It's faster to build your own gallery if you know enough about screen language.
 

Aitha

Newbie
Jul 7, 2017
20
7
As always, when in doubt:
  1. Read the documentation
  2. Experiment
  3. If all else fails, ask questions here or . There is a LOT of sample code in the . Chances are if you have a question, somebody has already posted a sample there...
Thanks rich, now its working. You saved me man. Have a nice day
 

KiaAzad

Member
Feb 27, 2019
276
205
looks like I'm late to the party. I would (and will) give you my gallery code for free if you have reached out to me. It would be free soon anyways.
My plan was to put it on GitHub weeks ago, but I decided to update and clean up the code first. it's an old work and in desperate need of attention.
 

Aitha

Newbie
Jul 7, 2017
20
7
looks like I'm late to the party. I would (and will) give you my gallery code for free if you have reached out to me. It would be free soon anyways.
My plan was to put it on GitHub weeks ago, but I decided to update and clean up the code first. it's an old work and in desperate need of attention.
Good idea! Even I want give my gallery but it's a copy from 'rich' gallery. So no use of mine. But thanks for your idea. Let's help people. Have a nice day.
 

lydcreations

Member
Game Developer
May 6, 2019
300
392
Sorry to reawaken an old thread. I used Rich's code to add a gallery, but I can't get the formatting for the gallery page correct. I haven't been able to determine what part of the code that I need to adjust to correct this. Can someone let me know how to make this adjustment? Thanks in advance.

gallery.png