[SOLVED] Randomising the location of an object

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.
Post Reply
Message
Author
User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

[SOLVED] Randomising the location of an object

#1 Post by Kinmoku »

Hi all,

I have a point and click mini-game where you only have 3 clicks to find a key to open the door.

Originally, I had the key in the "coatpocket" (which was called "key") but I'd like to randomise the location of the key so it's different each time. The key could hide in the "coatpocket", "pottable", "baghook" or "bowltable".

Here's my code:

Code: Select all

label hallway_search:
    if doorunlocked == True:
        call screen hallway_unlocked_imagemap
        
    else:
        call screen hallway_imagemap
        
    $ result = _return

    if result == "pot":
        if "pottable" in efound:
            t "..."

        else:
            $ efound.append("pottable")
            
            t "I wonder if there's anything in this pot?"
            t "Bah, nothing!"
        
    elif result == "handbag":
        if "baghook" in efound:
            t "I checked everything in here.\nNothing."

        else:
            $ efound.append("baghook")
            
            pause 1
            
            t "A wallet...\nSome make-up...\nJunk mail..."
            t "Ok.\nThere's nothing in here."
        
    elif result == "bowl":
        if "bowltable" in efound:
            t "..."

        else:
            $ efound.append("bowltable")

            t "Damn. Nothing in here but paper-clips and other useless stuff."
        
    elif result == "lock":
        if "doorlock" in efound:
            t "I've already flicked this."
            
            menu:
                t "Should I flick it again?"
                
                "Yes":
                    if doorunlocked == False:
                        $ doorunlocked = True
                                         
                        show unlocked
                        
                    elif doorunlocked == True:
                        $ doorunlocked = False
                        
                        hide unlocked
                    
                "No, leave it alone.":
                    pause 0.5
                    
        else:
            $ efound.append("doorlock")

            t "Perhaps I should flick this lock.\nMaybe that will open the door?"
            
            $ doorunlocked = True
            
            show unlocked
            
            pause 0.5
            
            if doorkey == True:
                
                t "Yes!"
                
                jump ending
                
            else:
                t "Nope. It still won't open."
            
    elif result == "keyhole":
        if "key" in efound:
            t "Maybe the key I found will fit here?"

            $ doorkey = True
            
            t "Bingo!\nSeems like it unlocked."
        
        else:
            t "There's no key here."
        
    elif result == "pocket":
        if "coatpocket" in efound:
            t "I've already searched this."

        else:
            $ efound.append("coatpocket")
            
            t "Let me just see if there's anything in here..."
            t "A key!\nGreat, I wonder if it can open this door?"
        
    elif result == "handle":
        if doorkey == True:
            if doorunlocked == True:
                jump ending
            
            else:
                play audio "sounds/door_locked.ogg"
                
                t "Hmm. The door still won't budge.\nIt feels looser at the bottom, though."
        
        else:
            if doorunlocked == True:
                t "It's still not opening?\nI wonder if it's completely unlocked?"
                
            else:
                t "No good.\nThe door won't budge."
    
    if len(efound) <= 3:
        jump hallway_search
        
    else:
        jump ending_caught
If my code is a little confusing... There are 2 locks on the door, one with a switch you can flick on and off, and the one that needs the key.

I'm not sure what to add here to change the location of the "key" each time.
Last edited by Kinmoku on Thu Aug 11, 2016 12:29 pm, edited 1 time in total.

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: Randomising the location of an object

#2 Post by Iylae »

Code: Select all

$ key_location = renpy.random.choice(["coatpocket", "pottable", "baghook", "bowltable"])
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Randomising the location of an object

#3 Post by Kinmoku »

Iylae wrote:

Code: Select all

$ key_location = renpy.random.choice(["coatpocket", "pottable", "baghook", "bowltable"])
Cool! Do I put this in every item or before them all?

To create a line of dialogue that says "You have found the key!", would I add something like...

Code: Select all

if key_location == True:
    "You have found the key!"
?

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: Randomising the location of an object

#4 Post by Iylae »

Not quite, you're wanting to ask if the key_location is equal to the location you're at. e.g.

Code: Select all

    elif result == "bowl":
        if "bowltable" in efound:
            t "..."

        else:
            $ efound.append("bowltable")

            if key_location == "bowltable":   #this is the if statement you're looking for
                t "You found a key!"
            else:
                t "Damn. Nothing in here but paper-clips and other useless stuff."
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Randomising the location of an object

#5 Post by Kinmoku »

Iylae wrote:Not quite, you're wanting to ask if the key_location is equal to the location you're at. e.g.

Code: Select all

    elif result == "bowl":
        if "bowltable" in efound:
            t "..."

        else:
            $ efound.append("bowltable")

            if key_location == "bowltable":   #this is the if statement you're looking for
                t "You found a key!"
            else:
                t "Damn. Nothing in here but paper-clips and other useless stuff."
Ah awesome! Thanks very much :)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]