Page 1 of 1

[SOLVED] Randomising the location of an object

Posted: Thu Aug 11, 2016 11:19 am
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.

Re: Randomising the location of an object

Posted: Thu Aug 11, 2016 11:23 am
by Iylae

Code: Select all

$ key_location = renpy.random.choice(["coatpocket", "pottable", "baghook", "bowltable"])

Re: Randomising the location of an object

Posted: Thu Aug 11, 2016 11:48 am
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!"
?

Re: Randomising the location of an object

Posted: Thu Aug 11, 2016 12:21 pm
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."

Re: Randomising the location of an object

Posted: Thu Aug 11, 2016 12:28 pm
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 :)