Gallery for Custom Characters

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
cuterobot
Newbie
Posts: 3
Joined: Wed May 29, 2019 11:55 pm
Projects: I ordered a cute child robot
Contact:

Gallery for Custom Characters

#1 Post by cuterobot » Thu May 30, 2019 12:59 am

I came up with this gallery code for a game with customizable characters. If you get a good ending, the character is added to the gallery. Feel free to modify/use it. I took the code to read tab-separated files from a few examples PyTom posted.

The game has six images to make up a character, an eye offset to match the eyes (and optional glasses) properly to the body, and a few text variables, for a total of eleven fields. The gallery displays each character in correct order for layering.

screens.rpy

In menu area:

Code: Select all

if renpy.loadable("gallery.txt"):
    textbutton _("Gallery") action ShowMenu("gallery")
On its own:

Code: Select all

init python:
    def parse_csv(filename):
        f = file(config.gamedir + "/" + filename)
        rv = [ ]

        for l in f:
             l = l[:-1]
             rv.append(l.split('\t'))

        f.close()
        return rv


gallery.rpy

Code: Select all

default gallery_current = 1

screen gallery:
    tag menu
    add "background.jpg"
    $ pictures = parse_csv("gallery.txt")
    $ gallery_last = len(pictures)
    $ gallery_first = 1

    $ aname = pictures[gallery_current - 1][0]
    $ alast = pictures[gallery_current - 1][1]
    $ job = pictures[gallery_current - 1][2]
    $ body = pictures[gallery_current - 1][3]
    $ eye_offset = int(pictures[gallery_current -1][4])
    $ normal = pictures[gallery_current -1][5]
    $ hair = pictures[gallery_current -1][6]
    $ bhair = pictures[gallery_current -1][7]
    $ suit = pictures[gallery_current -1][8]
    $ glasses = pictures[gallery_current -1][9]
    $ ending = pictures[gallery_current -1][10]

    add bhair at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add body at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add normal at Position(xpos=300, ypos=0, xanchor=0, yanchor=eye_offset)
    if glasses != "None":
        add glasses at Position(xpos=300, ypos=0, xanchor=0, yanchor=eye_offset)
    add suit at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add hair at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)

    text "[aname]" xpos 720 ypos 210 size 50
    if alast != "None":
        text "[alast]" xpos 720 ypos 275 size 50
    text "[job]" xpos 720 ypos 345 size 40
    text "[ending]" xpos 720 ypos 400 size 40
    text "[gallery_current] / [gallery_last]" xalign 0.1 yalign 0.08 size 40

    if gallery_current > 1:
        textbutton "Previous":
            text_size 50
            action SetVariable("gallery_current", gallery_current - 1)
            xalign 0.1
            yalign 0.92

    if gallery_current < gallery_last:
        textbutton "Next":
            text_size 50
            action SetVariable("gallery_current", gallery_current + 1)
            xalign 0.9
            yalign 0.92

    textbutton "Return":
        text_size 50
        action Return()
        xalign 0.5
        yalign 0.92

script.rpy

just before the credits:

Code: Select all

if ending_bad == 0:
    $ output = str(aname) + "\t" + str(alast) + "\t" + str(job) + "\t" + str(body) + "\t" + str(eye_offset) + "\t" + str(normal) + "\t" + str(hair) + "\t" + str(bhair) + "\t"+ str(suit) + "\t" + str(glasses) + "\t" + str(ending) + "\n"
    python:
        with open(config.gamedir + '/' + 'gallery.txt', 'a') as gal:
            gal.write(output)
Example screenshot from game:
gall.png

Post Reply

Who is online

Users browsing this forum: No registered users