Page 1 of 1

[SOLVED] Pick one image random from a set of images

Posted: Mon Jan 08, 2018 10:23 am
by Benny
Hi,

I need to randomize the background image from a set of images, this is the code I actually use:

Code: Select all

init -1 python:
    numbers = [1, 2, 3, 4]
    def disco_background(trans, st, at):
        num = renpy.random.choice(numbers)
        renpy.show(("bg disco%d" % num))

init:
    image bg disco:
        function disco_background
        pause 0.50
        repeat

    image bg disco1 = "background/disco/disco1.jpg"
    image bg disco2 = "background/disco/disco2.jpg"
    image bg disco3 = "background/disco/disco3.jpg"
    image bg disco4 = "background/disco/disco4.jpg"
The result is a fixed background with only one image.

How can I randomize the images every 0.50 seconds?

Thanks
Matteo

Re: Pick one image random from a set of images

Posted: Mon Jan 08, 2018 10:53 am
by Ocelot

Code: Select all

image bg disco:
    choice:
        "bg disco1"
    choice:
        "bg disco2"
    choice:
        "bg disco3"
    choice:
        "bg disco4"
    pause 0.5
    repeat

Re: Pick one image random from a set of images

Posted: Mon Jan 08, 2018 11:21 am
by Benny
Thanks, Ocelot for the quick repy :)
Is also possibile to randomize the pause timing?

Thanks

Re: Pick one image random from a set of images

Posted: Tue Jan 09, 2018 5:00 am
by Benny
Quick update,
this is the code I use to randomize the pause (I hope it helps someone :) )

Code: Select all

init python:
    # Generate random floating number between min and max
    def randomFloatPause(min, max):
        return renpy.random.uniform(min, max)
    
    # Generate random integer number between min and max  
    def randomIntPause(min, max):
    	return renpy.random.randint(min,max)

init:
    image bg disco:
        choice:
            "bg disco1"
    	choice:
            "bg disco2"
    	choice:
            "bg disco3"
    	choice:
            "bg disco4"
        pause (randomFloatPause(0.5, 2))
        # pause (randomIntPause(1, 2))
        repeat