### Navigation

- index
- next |
- previous |
- Ren'Py Home |
- Ren'Py Documentation

# Image Gallery and Music Room Actions

## Image Gallery

A image gallery is a screen that allows the player to unlock images, and then view those images. The screen has one or more buttons associated with it, and each button has one or more associated images. Buttons and images also have conditions that determine if they have unlocked.

Image galleries are managed by instances of the Gallery class. A single instance of the gallery class may be shared between multiple image gallery screens.

A gallery has one or more buttons associated with it, a button has one or more images associated with it, and each image has one or more displayables associated with it. Conditions can be assigned to buttons and images. A button is unlocked when all of the conditions associated with it are satisfied and at least one image associated with that button is unlocked. An image is unlocked when all associated conditions are satisfied.

Creating an image gallery consists of the following four steps.

1. Create an instance of Gallery.

2. Add buttons and images to that gallery, along with conditions that determine if the buttons and images they belong to are unlocked. This is also a multi-step process.

   1. Declare a new button by calling `Gallery.button()`.
   2. Optionally, add one or more unlock conditions to the button by calling `Gallery.unlock()` or `Gallery.condition()`.
   3. Declare an image by calling `Gallery.image()` with one or displayables as arguments. Or call the convenience method `Gallery.unlock_image()` instead.
   4. Optionally, call `Gallery.transform()` to associate transforms with the displayables.
   5. Optionally, add one or more unlock conditions to the image by calling `Gallery.unlock()`, `Gallery.condition()`, or `Gallery.allprior()`.

   Additional images can be added to a button by repeating steps 3-5, while additional buttons can be added to the gallery by repeating all five steps.

3. Create an image gallery screen. The screen should display a background, and should contain navigation that allows the user to show other image galleries, or to return to the main or extras menu.

4. Add code to display the image gallery screen to the main or extras menu.

Here's an example of the first three steps:

    init python:

        # Step 1. Create the gallery object.
        g = Gallery()

        # Step 2. Add buttons and images to the gallery.

        # A button that contains an image that automatically unlocks.
        g.button("dawn")
        g.image("dawn1")
        g.unlock("dawn1")

        # This button has multiple images assocated with it. We use unlock_image
        # so we don't have to call both .image and .unlock. We also apply a
        # transform to the first image.
        g.button("dark")
        g.unlock_image("bigbeach1")
        g.transform(slowpan)
        g.unlock_image("beach1 mary")
        g.unlock_image("beach2")
        g.unlock_image("beach3")

        # This button has a condition associated with it, allowing code
        # to choose which images unlock.
        g.button("end1")
        g.condition("persistent.unlock_1")
        g.image("transfer")
        g.image("moonpic")
        g.image("girlpic")
        g.image("nogirlpic")
        g.image("bad_ending")

        g.button("end2")
        g.condition("persistent.unlock_2")
        g.image("library")
        g.image("beach1 nomoon")
        g.image("bad_ending")

        # The last image in this button has an condition associated with it,
        # so it will only unlock if the user gets both endings.
        g.button("end3")
        g.condition("persistent.unlock_3")
        g.image("littlemary2")
        g.image("littlemary")
        g.image("good_ending")
        g.condition("persistent.unlock_3 and persistent.unlock_4")

        g.button("end4")
        g.condition("persistent.unlock_4")
        g.image("hospital1")
        g.image("hospital2")
        g.image("hospital3")
        g.image("heaven")
        g.image("white")
        g.image("good_ending")
        g.condition("persistent.unlock_3 and persistent.unlock_4")

        # The final two buttons contain images that show multiple pictures
        # at the same time. This can be used to compose character art onto
        # a background.
        g.button("dawn mary")
        g.unlock_image("dawn1", "mary dawn wistful")
        g.unlock_image("dawn1", "mary dawn smiling")
        g.unlock_image("dawn1", "mary dawn vhappy")

        g.button("dark mary")
        g.unlock_image("beach2", "mary dark wistful")
        g.unlock_image("beach2", "mary dark smiling")
        g.unlock_image("beach2", "mary dark vhappy")

        # The transition used when switching images.
        g.transition = dissolve

    # Step 3. The gallery screen we use.
    screen gallery:

        # Ensure this replaces the main menu.
        tag menu

        # The background.
        add "beach2"

        # A grid of buttons.
        grid 3 3:

            xfill True
            yfill True

            # Call make_button to show a particular button.
            add g.make_button("dark", "gal-dark.png", xalign=0.5, yalign=0.5)
            add g.make_button("dawn", "gal-dawn.png", xalign=0.5, yalign=0.5)
            add g.make_button("end1", "gal-end1.png", xalign=0.5, yalign=0.5)

            add g.make_button("end2", "gal-end2.png", xalign=0.5, yalign=0.5)
            add g.make_button("end3", "gal-end3.png", xalign=0.5, yalign=0.5)
            add g.make_button("end4", "gal-end4.png", xalign=0.5, yalign=0.5)

            add g.make_button("dark mary", "gal-dark_mary.png", xalign=0.5, yalign=0.5)
            add g.make_button("dawn mary", "gal-dawn_mary.png", xalign=0.5, yalign=0.5)

            # The screen is responsible for returning to the main menu. It could also
            # navigate to other gallery screens.
            textbutton "Return" action Return() xalign 0.5 yalign 0.5

Step 4 will vary based on how your game is structured, but one way of accomplishing it is to add the following line:

    textbutton "Gallery" action ShowMenu("gallery")

to the main menu screen.

class `Gallery`(self)

  This class supports the creation of an image gallery by handling the locking of images, providing an action that can show one or more images, and a providing method that creates buttons that use that action.

  `transition`

    The transition that is used when changing images.

  `locked_button`

    The default displayable used by make_button for a locked button.

  `hover_border`

    The default hover border used by make_button.

  `idle_border`

    The default idle border used by make_button.

  `Action`(name)

    An action that displays the images associated with the given button name.

  `allprior`(self)

    A condition that is true if all prior images associated with the current button have been unlocked.

  `button`(name)

    Creates a new button, named name.

    name
      The name of the button being created.
  `condition`(expression)

    A condition that is satisfied when an expression evaluates to true.

    expression
      A string giving a python expression.
  `display`(*displayables)

    Adds a new image to the current button, where an image consists of one or more displayables.

  `image`(*displayables)

    Adds a new image to the current button, where an image consists of one or more displayables.

  `make_button`(name, unlocked, locked=None, hover_border=None, idle_border=None, **properties)

    This creates a button that displays the images associated with the given button name.

    name
      The name of the button that will be created.
    unlocked
      A displayable that is displayed for this button when it is unlocked.
    locked
      A displayable that is displayed for this button when it is locked. If None, the locked_button field of the gallery object is used instead.
    hover_border
      A displayable that is used to overlay this button when when it is unlocked and has focus. If None, the hover_border field of the gallery object is used.
    idle_border
      A displayable that is used to overlay this button when when it is unlocked but unfocused. If None, the idle_border field of the gallery object is used.

    Additional keyword arguments become style properties of the created button object.

  `transform`(*transforms)

    Applies transforms to the last image registered. This should be called with the same number of transforms as the image has displayables. The transforms are applied to the corresponding displayables.

    If a transform is None, the default transform is used.

  `unlock`(*images)

    A condition that takes one or more image names as argument, and is satisfied when all the named images have been seen by the player. The image names should be given as strings.

  `unlock_image`(*images)

    A convenience method that is equivalent to calling image and unlock with the same parameters. This will cause an image to be displayed if it has been seen before.

    The images should be specified as strings giving image names.

### Music Room

A music room is a screen that allows the user to select and play music tracks from the game. These tracks may start off locked when the user first begins playing a particular game, and will be unlocked as the user listens to the music while playing the game.

A music room is managed by an instance of the MusicRoom class. There can be more than one MusicRoom instance in a game, allowing a game to have multiple music rooms. Creating a music room consists of the following four steps:

1. Create an instance of MusicRoom. The MusicRoom constructor takes parameters to control the channel on which music is played back, and how long it takes to fade music out and back in.

2. Add music files to the instance.

3. Create a screen that uses the MusicRoom instance to create actions for buttons, imagebuttons, or hotspots. These actions can pick a track, the next or previous track, or stop and start the music.

   Note that the actions used are members of a MusicRoom instance, so if the MusicRoom instance is named `mr`, then `mr.Play("track1.ogg")` is how you'd use the play action.

4. Add the music room screen to the main menu, or an extras menu.

Here's an example of the first three steps:

    init python:

        # Step 1. Create a MusicRoom instance.
        mr = MusicRoom(fadeout=1.0)

        # Step 2. Add music files.
        mr.add("track1.ogg", always_unlocked=True)
        mr.add("track2.ogg")
        mr.add("track3.ogg")

    # Step 3. Create the music room screen.
    screen music_room:

        tag menu

        frame:
            has vbox

            # The buttons that play each track.
            textbutton "Track 1" action mr.Play("track1.ogg")
            textbutton "Track 2" action mr.Play("track2.ogg")
            textbutton "Track 3" action mr.Play("track3.ogg")

            null height 20

            # Buttons that let us advance tracks.
            textbutton "Next" action mr.Next()
            textbutton "Previous" action mr.Previous()

            null height 20

            # The button that lets the user exit the music room.
            textbutton "Main Menu" action ShowMenu("main_menu")

        # Start the music playing on entry to the music room.
        on "replace" action mr.Play()

        # Restore the main menu music upon leaving.
        on "replaced" action Play("music", "track1.ogg")

Step 4 will vary based on how your game is structured, but one way of accomplishing it is to add the following line:

    textbutton "Music Room" action ShowMenu("music_room")

to the main menu screen.

Using the `Preferences()` function, especially `Preferences("music volume")`, it's possible to include a volume slider on the music screen.

class `MusicRoom`(channel='music', fadeout=0.0, fadein=0.0)

  A music room that contains a series of songs that can be unlocked by the user, and actions that can play entries from the list in order.

  channel
    The channel that this music room will operate on.
  fadeout
    The number of seconds it takes to fade out the old music when changing tracks.
  fadein
    The number of seconds it takes to fade in the new music when changing tracks.
  `Next`(self)

    An action that causes the music room to play the next unlocked file in the playlist.

  `Play`(filename=None)

    Causes the music room to start playing. If filename is given, that file begins playing. Otherwise, the currently playing file starts over (if it's unlocked), or the first file starts playing.

    If filename is given, buttons with this action will be insensitive while filename is locked, and will be selected when filename is playing.

  `Previous`(self)

    An action that causes the music room to play the previous unlocked file in the playlist.

  `Stop`(self)

    Stops the music.

  `add`(filename, always_unlocked=False)

    Adds the music file filename to this music room. The music room will play unlocked files in the order that they are added to the room.

    always_unlocked
      If true, the music file will be always unlocked. This allows the file to show up in the music room before it has been played in the game.
  `is_unlocked`(filename)

    Returns true if the filename has been unlocked (or is always unlocked), and false if it is still locked.

### Navigation

- index
- next |
- previous |
- Ren'Py Home |
- Ren'Py Documentation