Lock Combination Puzzle

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Lock Combination Puzzle

#1 Post by Fatimah »

Hello,

Is there a way I can define a list of images in Renpy and then go through these images one by one by clicking on an image button?

I'm trying to build a lock puzzle where the user have to find the right combination to open the door. So I need multiple buttons to control each part (or number ) of the lock.
I'm trying to build it with screens and image buttons but I seem to just go from one error to another.
Last edited by Fatimah on Tue Nov 22, 2016 2:09 am, edited 1 time in total.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Image List

#2 Post by Kia »

yes, that's what I use for my gallery code. search for image gallery code in the cookbook.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Image List

#3 Post by Fatimah »

Kia wrote:yes, that's what I use for my gallery code. search for image gallery code in the cookbook.
Thank you for the suggestion.
I tried it and I've got a question if you can help me:
I need the image and the button to be displayed at the same time.
Right now, I get a screen with button only and when I click on it, it goes to another screen with the image only.

Code: Select all

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

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

    # 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("partA")
    g.unlock_image("partA1.png")
    g.unlock_image("partA2.png")
    
    g.transition = dissolve

Code: Select all

screen lockPuzzle:

    # The background.
    add "blackBG"

    # A grid of buttons.
    grid 1 1:

        xfill True
        yfill True

        # Call make_button to show a particular button.
        add g.make_button("partA", "next_arrow.png", xalign=0.5, yalign=0.5)
Thank you

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Image List

#4 Post by Kia »

that code looks incomplete to me, I can't say if it should work or not.
I'm afraid the cookbook is not something I can help with since I never tried anything that's in it, all I can say is: as long as your image and buttons are in the same screen they should show fine unless the image is covering the buttons.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Image List

#5 Post by Fatimah »

Kia wrote:that code looks incomplete to me, I can't say if it should work or not.
I'm afraid the cookbook is not something I can help with since I never tried anything that's in it, all I can say is: as long as your image and buttons are in the same screen they should show fine unless the image is covering the buttons.
Thank you. I myself don't know if the image gallery is suitable for what I'm trying to achieve or not, but I have ran out of ideas so I thought I would give your suggestion a shot.

I'm trying to build a lock puzzle where the user have to find the right combination to open the door. So I need multiple buttons to control each part (or number ) of the lock.
I'm trying to build it with screens and image buttons but I seem to just go from one error to another.

This is the last thing that I have:

Code: Select all

screen lockPuzzle:
    add "blackBG"
    
    $ partNumber = 1
    $ string_num = "%d" % partNumber
    
    add "partA" + string_num + ".png"
    
    imagebutton idle "next_arrow.png" hover "next_arrow.png" xpos 1000 ypos 300 clicked [Hide("part1"), SetVariable("partNumber", partNumber + 1), SetVariable("string_num", "%d" % partNumber),Show("part1")]

    
    
screen part1():

    # The background.
    #add "blackBG"
    
    #$ num = 1
    #$ string_num = "%d" % partNumber
    
    add "partA" + string_num + ".png"

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Image List

#6 Post by nyaatrap »

https://www.renpy.org/doc/html/displayi ... -functions
renpy.get_available_image_tags and renpy.get_available_image_attributes can search what images are defined.
You can also use renpy.list_files to get all files in your game folder.
Though I suggest to use normal buttons instead of imagesbuttons. Imagebuttons are syntax sugar with less function than normal buttons. So when you want to do complex works, it'd not be always worked.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Image List

#7 Post by Fatimah »

nyaatrap wrote:https://www.renpy.org/doc/html/displayi ... -functions
renpy.get_available_image_tags and renpy.get_available_image_attributes can search what images are defined.
You can also use renpy.list_files to get all files in your game folder.
Though I suggest to use normal buttons instead of imagesbuttons. Imagebuttons are syntax sugar with less function than normal buttons. So when you want to do complex works, it'd not be always worked.
Thank you for the suggestion.
I think where I'm stuck is related more with how to handle the show/hide of the images when the buttons are clicked.
I figured a way to get the images' names but I just can't figure how to handle the required interaction.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Lock Combination Puzzle

#8 Post by Fatimah »

I tried to use renpy.restart_interaction() to reload the screen and update the values but this froze the game.

Code: Select all

screen part1:

    # The background.
    add "blackBG"
    
    #$ num = 1
    $ string_num = "%d" % partNumber
    
    add "partA" + string_num + ".png"
    
    imagebutton idle "next_arrow.png" hover "next_arrow.png" xpos 1000 ypos 300 clicked [SetVariable("partNumber", partNumber + 1), renpy.restart_interaction()]
Does anyone know why?

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Lock Combination Puzzle

#9 Post by philat »

I'm assuming by combination you mean something like this? https://upload.wikimedia.org/wikipedia/ ... n_lock.jpg

I can't quite tell what you're trying to do from the code snippets you posted, so the following assumes a 4 digit lock, with 10 possible answers for each digit (0~9), and that you have images named lock_1.png, lock_2.png, etc. in the appropriate folder. Haven't tested the dynamic images, but they should work, or I've made a dumb syntax mistake somewhere -- the concept should be fine.

Code: Select all

default d1=0
default d2=0
default d3=0
default d4=0

image lock1 = "lock_[d1].png"
image lock2 = "lock_[d2].png"
image lock3 = "lock_[d3].png"
image lock4 = "lock_[d4].png"

screen lockpuzzle():
    vbox:
        hbox:
            add "lock1"
            add "lock2"
            add "lock3"
            add "lock4"

        textbutton "1 ↑" action If(d1<9, SetVariable("d1", d1+1), SetVariable("d1", 0))
        textbutton "1 ↓" action If(d1>0, SetVariable("d1", d1-1), SetVariable("d1", 9))
        # duplicate the buttons for all four digits
        # use the condition below to check for the right answer
        textbutton "Open" action If(d1==1 and d2==2 and d3==3 and d4==4, Jump("yes"), Jump("no"))

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Lock Combination Puzzle

#10 Post by Fatimah »

philat wrote:I'm assuming by combination you mean something like this? https://upload.wikimedia.org/wikipedia/ ... n_lock.jpg

I can't quite tell what you're trying to do from the code snippets you posted, so the following assumes a 4 digit lock, with 10 possible answers for each digit (0~9), and that you have images named lock_1.png, lock_2.png, etc. in the appropriate folder. Haven't tested the dynamic images, but they should work, or I've made a dumb syntax mistake somewhere -- the concept should be fine.

Code: Select all

default d1=0
default d2=0
default d3=0
default d4=0

image lock1 = "lock_[d1].png"
image lock2 = "lock_[d2].png"
image lock3 = "lock_[d3].png"
image lock4 = "lock_[d4].png"

screen lockpuzzle():
    vbox:
        hbox:
            add "lock1"
            add "lock2"
            add "lock3"
            add "lock4"

        textbutton "1 ↑" action If(d1<9, SetVariable("d1", d1+1), SetVariable("d1", 0))
        textbutton "1 ↓" action If(d1>0, SetVariable("d1", d1-1), SetVariable("d1", 9))
        # duplicate the buttons for all four digits
        # use the condition below to check for the right answer
        textbutton "Open" action If(d1==1 and d2==2 and d3==3 and d4==4, Jump("yes"), Jump("no"))
Thank you, I think dynamic images is what I have been looking for.
Question though and sorry if this sounds stupid.
I tried your code without any changes and I got the error "NameError: name 'd1' is not defined".
I'm assuming this error has to do with the definition of the variable but I could not get it to work.

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Lock Combination Puzzle

#11 Post by philat »

Shouldn't happen if you used default correctly. The four lines up top are the initial definition of the variable.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Lock Combination Puzzle

#12 Post by Fatimah »

philat wrote:Shouldn't happen if you used default correctly. The four lines up top are the initial definition of the variable.
I still get the error & have no idea why. I have put these four lines in so many places but I have no idea what the problem is.

If I don't get the NameError, I get the "IOError: Couldn't find file 'lock_[d1].png'."

I even started a new project just to test this part of code.

Code: Select all

# You can place the script of your game in this file.

# Declare images below this line, using the image statement.
# eg. image eileen happy = "eileen_happy.png"
default d1=0
default d2=0
default d3=0
default d4=0

image lock1 = "lock_[d1].png"
#image lock2 = "lock_[d2].png"
#image lock3 = "lock_[d3].png"
#image lock4 = "lock_[d4].png"

screen lockpuzzle():
    vbox:
        hbox:
            add "lock1"
            #add "lock2"
            #add "lock3"
            #add "lock4"

        textbutton "1 ↑" action If(d1<1, SetVariable("d1", d1+1), SetVariable("d1", 0))
        textbutton "1 ↓" action If(d1>0, SetVariable("d1", d1-1), SetVariable("d1", 1))
        # duplicate the buttons for all four digits
        # use the condition below to check for the right answer
        textbutton "Open" action If(d1==1 and d2==2 and d3==3 and d4==4, Jump("yes"), Jump("no"))
# Declare characters used by this game.
define e = Character('إيلين', color="#c8ffc8")

init python:
    if not persistent.default_language_set:
        persistent.default_language_set = True
        _preferences.language = "arabic"

# The game starts here.
label start:

    e "لقد بدأت حكاية رينباي جديدة."

    e "كل ما عليك فعله هو إضافة نصوص و صور و إرسالها للعالم!"
    
    call screen lockpuzzle

    return
I have looked up the documentation & the syntax looks OK so I got no idea why I keep getting these errors.

If anyone can help, I'll really appreciate it.
Thank you

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Lock Combination Puzzle

#13 Post by philat »

No clue. Only thing I can think is either 1) you misnamed the image files, or 2) maybe you're using an outdated version of renpy, but 6.99.7 was a WHILE back...

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Lock Combination Puzzle

#14 Post by Fatimah »

philat wrote:No clue. Only thing I can think is either 1) you misnamed the image files, or 2) maybe you're using an outdated version of renpy, but 6.99.7 was a WHILE back...
No, I had my images named like this "lock_1.png" and no luck.
I also had the thought that may be it had something to do with Renpy version but I have the latest one so it is not that.

I renamed one image file like this "lock_[d1].png" and it got displayed meaning that the code is not translating the variable as a dynamic image but rather it reads it as a static name which is baffling me.

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Lock Combination Puzzle

#15 Post by philat »

Well, honestly can't help you, because it's working fine for me. I guess you can try using image lock1 = DynamicImage("lock_[d1].png") to force it to DynamicImage, but it really shouldn't be necessary (and is working for me with just "lock_[d1].png" on 6.99.11).

Post Reply

Who is online

Users browsing this forum: No registered users